应该是常数 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 5900 QSC and Master

    题目链接:传送门 题目大意:长度为n的key数组与value数组,若相邻的key互斥,则可以删去这两个数同时获得对应的两 个value值,问最多能获得多少 题目思路:区间DP 闲谈: 这个题一开始没有 ...

  2. centos7 install k8s centos 安装 kubernetes 详细

    参考: http://blog.opskumu.com/k8s-cluster-centos7.html https://ylinux.org/note/article/1207 http://mub ...

  3. c#基础 第八讲

    static void Main(string[] args) { while (true)//一直循环 { Random r = new Random();//创建随机函数r int[] caipi ...

  4. angular2+ 引用layDate日期选择插件

    layDate日期选择插件使用npm安装好像是行不通的,但angular2+的日期选择控件库又不能够支持时分秒的选择 在angular项目中引用layDate 1. 首先官网下载layDate独立版, ...

  5. Vue基础-匿名插槽与作用域插槽的合并和覆盖行为

    Vue 测试版本:Vue.js v2.5.13 Vue 文档: <slot> 元素可以用一个特殊的特性 name 来进一步配置如何分发内容.多个插槽可以有不同的名字.具名插槽将匹配内容片段 ...

  6. java重构、js与接口的实现

    一.接口 接口的方法不一定必须实现的!!! 加入default,这样的方法可以不实现,如图所示 二.关于语言的特性 1.C++多继承 2.很多编程语言JavaScript.Python支持混入(Mix ...

  7. HQL的第一个程序

    使用HQL查询数据库: 分为以下几个步骤 1获取query对象 //1获取query对象 String hql="FROM Employee e where e.salary>?&qu ...

  8. 1. Action 实现 ModelDriven 接口后的运行流程

    1). 先会执行 ModelDrivenInterceptor 的 intercept 方法. public String intercept(ActionInvocation invocation) ...

  9. Tornado @tornado.gen.coroutine 与 yield

    在使用 Tornado 的过程中产生了以下疑问: 什么时候需要给函数增加 @tornado.gen.coroutine 什么时候调用函数需要 yield @tornado.gen.coroutine ...

  10. idea破解方法

    1.http://idea.lanyus.com/ 下载破解文件 2.将下载的JetbrainsIdesCrack-3.4-release-enc.jar破解文件放在idea安装目录下的bin中: 3 ...