LeetCode 902. Numbers At Most N Given Digit Set
应该是常数 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的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- Leetcode: Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
1. 题目名称 Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...
- 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'}. ...
- [LeetCode] Lexicographical Numbers 字典顺序的数字
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
随机推荐
- python 发送email
pyton smtplib发送邮件 在邮件中设置并获取到 smtp域名 在脚本中执行命名,收件人可以是 多个,在列表中 import smtplib from email.mime.text impo ...
- 【C++缺省函数】 空类默认产生的6个类成员函数
1.缺省构造函数. 2.缺省拷贝构造函数. 3. 缺省析构函数. 4.缺省赋值运算符. 5.缺省取址运算符. 6. 缺省取址运算符 const. <span style="font-s ...
- RF中空列表变量不能直接赋至为[]
RF中空列表正确定义方法为:
- iOS7Status bar适配
一 Status bar重叠问题: - Zherui if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 7.0) { sel ...
- SQL ALTER TABLE 命令
SQL ALTER TABLE 命令 SQL ALTER TABLE 命令用于添加.删除或者更改现有数据表中的列. 你还可以用 ALTER TABLE 命令来添加或者删除现有数据表上的约束. 语法: ...
- GO函数总结(转)
GO语言函数与C,JAVA等有点很大的区别,大致如下: 一.函数的定义 例如: func max(a, b int) int{//返回a,b里面最大的一个,而且要注意函数的参数和返回值类型是怎么定义的 ...
- ubuntu squid 代理服务器安装配置
安装: 下载安装包 http://pan.baidu.com/s/1mitvwpE 解压 tar -xzvf file.tar.gz 编译: 进入sbin目录 执行 ./configure --pr ...
- linux dpdk DDOS清洗和流量行为分析
http://www.linuxidc.com/Linux/2014-09/106285.htm http://www.th7.cn/system/lin/201403/51652.shtml DDO ...
- Apache Kafka源码分析 – Controller
https://cwiki.apache.org/confluence/display/KAFKA/Kafka+Controller+Internalshttps://cwiki.apache.org ...
- 爬虫之Xpath详解
XPath介绍 XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 ...