在一个范围内没有重复数字的总数
在本文中,我们将讨论计算给定范围 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。
计算前缀数组的前缀和。前缀总和将为您提供该范围内有效数字的总数。
返回前缀和。
示例
下面给出了这种方法的代码 -
#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
结论 - 在这段代码中,我们讨论了三种方法来计算从低到高范围内没有重复数字的总数。
以上是在一个范围内没有重复数字的总数的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

C语言数据结构:树和图的数据表示与操作树是一个层次结构的数据结构由节点组成,每个节点包含一个数据元素和指向其子节点的指针二叉树是一种特殊类型的树,其中每个节点最多有两个子节点数据表示structTreeNode{intdata;structTreeNode*left;structTreeNode*right;};操作创建树遍历树(先序、中序、后序)搜索树插入节点删除节点图是一个集合的数据结构,其中的元素是顶点,它们通过边连接在一起边可以是带权或无权的数据表示邻

文件操作难题的真相:文件打开失败:权限不足、路径错误、文件被占用。数据写入失败:缓冲区已满、文件不可写、磁盘空间不足。其他常见问题:文件遍历缓慢、文本文件编码不正确、二进制文件读取错误。

C35 的计算本质上是组合数学,代表从 5 个元素中选择 3 个的组合数,其计算公式为 C53 = 5! / (3! * 2!),可通过循环避免直接计算阶乘以提高效率和避免溢出。另外,理解组合的本质和掌握高效的计算方法对于解决概率统计、密码学、算法设计等领域的许多问题至关重要。

C语言函数是代码模块化和程序搭建的基础。它们由声明(函数头)和定义(函数体)组成。C语言默认使用值传递参数,但也可使用地址传递修改外部变量。函数可以有返回值或无返回值,返回值类型必须与声明一致。函数命名应清晰易懂,使用驼峰或下划线命名法。遵循单一职责原则,保持函数简洁性,以提高可维护性和可读性。

C语言函数名定义包括:返回值类型、函数名、参数列表和函数体。函数名应清晰、简洁、统一风格,避免与关键字冲突。函数名具有作用域,可在声明后使用。函数指针允许将函数作为参数传递或赋值。常见错误包括命名冲突、参数类型不匹配和未声明的函数。性能优化重点在函数设计和实现上,而清晰、易读的代码至关重要。

C语言函数是可重复利用的代码块,它接收输入,执行操作,返回结果,可将代码模块化提高可复用性,降低复杂度。函数内部机制包含参数传递、函数执行、返回值,整个过程涉及优化如函数内联。编写好的函数遵循单一职责原则、参数数量少、命名规范、错误处理。指针与函数结合能实现更强大的功能,如修改外部变量值。函数指针将函数作为参数传递或存储地址,用于实现动态调用函数。理解函数特性和技巧是编写高效、可维护、易理解的C语言程序的关键。

C语言多线程编程指南:创建线程:使用pthread_create()函数,指定线程ID、属性和线程函数。线程同步:通过互斥锁、信号量和条件变量防止数据竞争。实战案例:使用多线程计算斐波那契数,将任务分配给多个线程并同步结果。疑难解答:解决程序崩溃、线程停止响应和性能瓶颈等问题。

如何在 C 语言中输出倒数?回答:使用循环语句。步骤:1. 定义变量 n 存储要输出的倒数数字;2. 使用 while 循环持续打印 n 直到 n 小于 1;3. 在循环体内,打印出 n 的值;4. 在循环末尾,将 n 减去 1 以输出下一个更小的倒数。
