应该是常数 N的位数时间级别

我的这个方法超时很严重。。。但是特此记录 费劲巴拉写的。。。

超时:

int atMostNGivenDigitSet(char** D, int DSize, int N) {
int *array = (int *)malloc(sizeof(int) * 10);
for (int i = 0; i < 10; i ++)
array[i] = 0; for (int j = 1; j < 10; j ++) { if (j - 1 < DSize) {
char cc = D[j - 1][0];
int c = cc - 48;
array[c] = c;
}
//printf("\n%d",array[j]);
}
int totalSum = 0;
for (int i = 1; i <= N; i ++) { long sum = 0;
int num = 0;
int x = i;
int isFlag = 1;
while ( x!= 0) {
isFlag = 0;
num = x % 10; //末尾数字
sum = sum * 10;//进位
sum += num;
x = x / 10;
if (array[num] == 0) {
isFlag = 1;
break;
}
}
if (isFlag == 0) {
totalSum ++;
}
}
free(array);
return totalSum;
} int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!\n");
//[-2,-1] ---> -1 char *s[] = {"1","3","5","7"};
int maxNum = atMostNGivenDigitSet(s, 4, 100);
printf("\n%d\n",maxNum);
}
return 0;
}

LeetCode 902. Numbers At Most N Given Digit Set的更多相关文章

  1. [LeetCode] 902. Numbers At Most N Given Digit Set 最大为 N 的数字组合

    We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  (Not ...

  2. 902. Numbers At Most N Given Digit Set

    We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  (Not ...

  3. [Swift]LeetCode902. 最大为 N 的数字组合 | Numbers At Most N Given Digit Set

    We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  (Not ...

  4. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  5. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  6. Leetcode: Count Numbers with Unique Digits

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  7. [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)

    1. 题目名称   Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...

  8. LeetCode902. Numbers At Most N Given Digit Set

    题目: We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  ...

  9. [LeetCode] Lexicographical Numbers 字典顺序的数字

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

随机推荐

  1. hdu 3760(2次bfs求最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3760 思路:首先是建反图,从点n开始做spfa求出n到各点的最短路,然后从1点开始搜最小序列,对于边( ...

  2. M451例程讲解之按键

    /**************************************************************************//** * @file main.c * @ve ...

  3. 四个dos命令检查你的电脑是否中木马

    一些基本的命令往往可以在保护网络安全上起到很大的作用,下面几条命令的作用就非常突出. 命令是再CMD中输入,不是运行框中 一.检测网络连接 如果你怀疑自己的计算机上被别人安装了木马,或者是中了病毒,但 ...

  4. Json数组基础知识

    1.对象是一个无序的“‘名称/值’对”集合. (1)一个对象以“{”(左括号)开始,“}”(右括号)结束. (2)每个“名称”后跟一个“:”(冒号): (3)“‘名称/值’ 对”之间使用“,”(逗号) ...

  5. C/C++ 智能指针简单剖析

    导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...

  6. webpack配置(二)

    在配置webpack json loader的时候报错,如下: 解决方案: 首先,json文件中不能有注释 其次: 这里webpack.consig.js里面,modul下的loaders的loade ...

  7. JZOJ.5326【NOIP2017模拟8.21】LCA 的统计

    Description

  8. 160421、MyBatis批量插入数据

    在程序中封装了一个List集合对象,然后需要把该集合中的实体插入到数据库中,由于项目使用了Spring+MyBatis的配置,所以打算使用MyBatis批量插入,由于之前没用过批量插入,在网上找了一些 ...

  9. Powershell替代和截断——replace and substring

    一:截取一个字符串的尾部,替代字符串中的特定字符或替代字符串中特定位置的特定字符 $a="W.endy.chen.SHAO" $b=$a.Substring(0,$a.Length ...

  10. mongo 统计数据磁盘消耗

    repl_test:PRIMARY> show dbsadmin 0.000GBdirect_vote_resource 16.474GBlocal 14.860GBpersonas 30.77 ...