应该是常数 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. freemarker1 一些内建函数和用法

    ${"   green mouse"?cap_first} -->   Green mouse  //字符串中的第一个单词的首字母大写 ${"ABCDF" ...

  2. C++之运行时类型识别RTTI

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  3. tcp断开时分几步

    连接时是三次握手 断开时是四次握手,因为它是半关闭造成的

  4. iOS UIWebView 获取内容实际高度,关闭滚动效果

    本文转载至 http://my.oschina.net/Khiyuan/blog/341535   iOS UIWebView 获取内容实际高度,关闭滚动效果 近期做东西,将 UIWebView 嵌套 ...

  5. iOS 7 新特性:视图控制器切换API

    本文转载至 http://blog.jobbole.com/51588/ 本文由 伯乐在线 - studentdeng 翻译自 Chris Eidhof.欢迎加入技术翻译小组.转载请参见文章末尾处的要 ...

  6. Android XListView下拉刷新、上拉载入更多

    source code: https://github.com/Maxwin-z/XListView-Android 提供了两个接口: a) IXListViewListener:  触发下拉刷新.上 ...

  7. vue 阻止表单默认的提交事件

    form <form autocomplete="off" @submit.prevent="onSubmit"> <input type=& ...

  8. document.compatMode介绍

    来自:http://www.cnblogs.com/fullhouse/archive/2012/01/17/2324706.html 问题描述:看到阮一峰的博客里面的代码使用到了document.c ...

  9. Android Activity 去掉标题栏及全屏显示

    默认生成的活动(Activity)界面中包含标题栏,并带有状态栏.有时不需要这两个控件. 1.去掉标题栏 (三种方法) a:在setContentView()方法前 添加:requestWindowF ...

  10. Python overall structer

    在C/C++/Java中,main是程序执行的起点,Python中,也有类似的运行机制,但方式却截然不同:Python使用缩进对齐组织代码的执行,所有没有缩进的代码(非函数定义和类定义),都会在载入时 ...