0

0

在一个范围内没有重复数字的总数

PHPz

PHPz

发布时间:2023-09-10 23:25:06

|

1474人浏览过

|

来源于tutorialspoint

转载

在一个范围内没有重复数字的总数

在本文中,我们将讨论计算给定范围 Low 到 high 之间没有重复数字的正整数数量的不同方法。第一种方法是暴力方法,它迭代范围内的所有数字并检查它们是否包含重复的数字。在第二种方法中,我们使用前缀数组计算所需的计数,而在最后一种方法中,我们使用动态编程中的记忆概念来获得所需的结果。

问题陈述:给定两个数字,从低到高,我们必须找到从低到高之间的所有数字的计数,使得该数字不包含任何重复的数字。

方法 1

这是蛮力方法,我们只是从低到高迭代所有数字并检查它们是否包含任何重复的数字。这是解决我们的问题的最简单方法。

示例

下面给出了相同的代码解决方案:

#include <bits/stdc++.h>
using namespace std;
// function that checks whether or not the number contains any repeated digits
int count(int number){
	int arr[10] = {0};
	while(number != 0) {
		int digit = number % 10;
		if(arr[digit]>=1)
		{
			return 0;
		}
		arr[digit]++;
		number = number / 10;
	}
	return 1;
}
// this function iterates over all the numbers in the range from low to high and adds the count of numbers having no repeated digits to the result
int numberofnums(int l , int h)
{
	int res = 0;
	for(int iterator = l; iterator < h + 1; ++iterator)
	{
		res = res + count(iterator);
	}

	return res ;
}
int main()
{
	int low = 1, high = 90;
	cout << "The count of numbers with no repeated digits from " << low << " to "<< high << " is "<<numberofnums(low, high);
	return 0;
}

输出

The count of numbers with no repeated digits from 1 to 90 is 82

方法2

在这种方法中,我们将使用一个前缀数组来存储直到索引“迭代器”为止没有重复数字的整数的计数。

此方法涉及的步骤是:

  • 定义一个函数来检查数字是否具有重复的数字。

  • 用零初始化前缀数组。前缀数组将存储直到给定索引“迭代器”为止的有效数字的数量。

  • 从低到高遍历每个数字,检查是否有重复的数字。如果没有重复数字,则将相应索引处的前缀数组加1。

  • 计算前缀数组的前缀和。前缀总和将为您提供该范围内有效数字的总数。

    度加创作工具
    度加创作工具

    百度出品的、人人可用的AIGC创作平台

    下载
  • 返回前缀和。

示例

下面给出了这种方法的代码 -

#include <bits/stdc++.h>
using namespace std;
bool isvalid(int number)
{
	int arr[10] = {0};
	while(number != 0)
	{
		int digit = number % 10;
		if(arr[digit]>=1)
		{
			return false;
		}
		arr[digit]++;
		number = number / 10;
	}
	return true;
}

int count(int low, int high) {
    vector<int> prefarray(high+1, 0);
    for (int iterator = low; iterator <= high; iterator++) {
        if (isvalid(iterator)) {
            prefarray[iterator] = 1;
        }
    }
    for (int iterator = 1; iterator <= high; iterator++) {
        prefarray[iterator] += prefarray[iterator-1];
    }
    return prefarray[high] - prefarray[low-1];
}

int main() {
    int low = 21, high = 200;
    int c = count(low, high);
    cout << "The count of numbers with no repeated digits from " << low << " to "<< high << " is "<< c;
    return 0;
}

输出

The count of numbers with no repeated digits from 21 to 200 is 143

时间复杂度 - O(nlogn),其中 n 为(高 - 低)。

空间复杂度 - O(n)

方法3动态规划方法

在这种方法中,我们将问题分解为子问题,并将子问题的结果存储在记忆表中

程序计算给定范围内有效数字的总数,即没有重复数字的数字。它使用动态编程方法,其中函数 dp(“iterator”,used) 返回可以从位置“iterator”开始且数字“used”中形成的有效数字的数量。

我们使用记忆表来存储 dp 函数的结果,并迭代数字范围以对每个数字调用 dp 函数。 dp函数对所有起始位置“迭代器”的结果之和就是该范围内有效数字的总数。

示例

#include <bits/stdc++.h>
using namespace std;
int dp(int iterator, set<int>& used, unordered_map<string, int>& memo, const string& high_str) {
    if ( memo.count(to_string(iterator) + "|" + to_string(used.size() ))) {
        return memo[to_string(iterator) + "|" + to_string(used.size())];
    }
    if (iterator == high_str.length())
    {
        return 1;
    }
    int count = 0;
    for (int digit = 0; digit < 10; digit++) {
        if (digit == 0 && iterator == 0) {
            continue;
        }
        if (!used.count(digit)) {
            used.insert(digit);
            count += dp(iterator+1, used, memo, high_str);
            used.erase(digit);
        }
    }
    memo[to_string(iterator) + "|" + to_string(used.size())] = count;
    return count;
}

int count_valid_numbers(int low, int high) {
    unordered_map<string, int> memo;
    string high_str = to_string(high);
    int count = 0;
    for (int num = low; num <= high; num++) {
        set<int> used;
        count += dp(0, used, memo, high_str);
    }
    return count;
}

int main() {
    int low = 21, high = 200;
    int count = count_valid_numbers(low, high);
        cout << "The count of numbers with no repeated digits from " << low   <<  " to " << high << " is "<< count;
    return 0;
}

输出

The count of numbers with no repeated digits from 21 to 200 is 116640

结论 - 在这段代码中,我们讨论了三种方法来计算从低到高范围内没有重复数字的总数。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
bootstrap安装教程
bootstrap安装教程

本专题整合了bootstrap安装相关教程,阅读专题下面的文章了解更多详细操作教程。

3

2026.03.18

bootstrap框架介绍
bootstrap框架介绍

本专题整合了bootstrap框架相关介绍,阅读专题下面的文章了解更多详细内容。

4

2026.03.18

vscode 格式化
vscode 格式化

本专题整合了vscode格式化相关内容,阅读专题下面的文章了解更多详细内容。

2

2026.03.18

vscode设置中文教程
vscode设置中文教程

本专题整合了vscode设置中文相关内容,阅读专题下面的文章了解更多详细教程。

0

2026.03.18

vscode更新教程合集
vscode更新教程合集

本专题整合了vscode更新相关内容,阅读专题下面的文章了解更多详细教程。

3

2026.03.18

Gemini网页版零基础入门:5分钟上手Gemini聊天指南
Gemini网页版零基础入门:5分钟上手Gemini聊天指南

本专题专为零基础用户打造,5分钟快速掌握Gemini网页版核心用法。从账号登录到界面布局,详解如何发起对话、优化提示词及利用多模态功能。通过实战案例,教你高效获取信息、创作内容与分析数据。无论学习还是工作,轻松开启AI辅助新时代,让Gemini成为你的得力智能助手。

9

2026.03.18

Python WebSocket实时通信与异步服务开发实践
Python WebSocket实时通信与异步服务开发实践

本专题聚焦 Python 在实时通信场景中的开发实践,系统讲解 WebSocket 协议原理、长连接管理、消息推送机制以及异步服务架构设计。内容包括客户端与服务端通信实现、连接稳定性优化、消息队列集成及高并发处理策略。通过完整案例,帮助开发者构建高效稳定的实时通信系统,适用于聊天应用、实时数据推送等场景。

8

2026.03.18

Java Spring Security权限控制与认证机制实战
Java Spring Security权限控制与认证机制实战

本专题围绕 Java 后端安全体系建设展开,重点讲解 Spring Security 在权限控制与认证机制中的应用实践。内容涵盖用户认证流程、权限模型设计、JWT 鉴权方案、OAuth2 集成以及接口安全防护策略。通过实际项目案例,帮助开发者构建安全可靠的后端认证体系,提升系统安全性与可扩展能力。

26

2026.03.18

抖漫入口地址合集
抖漫入口地址合集

本专题整合了抖漫入口地址相关合集,阅读专题下面的文章了解更多详细地址。

176

2026.03.17

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号