Redian新闻
>
有换房顶的contractor 推荐么?
avatar
有换房顶的contractor 推荐么?# Living
S*C
1
一道career cup、leetcode的原题变种,也是lintcode原题,但目前没有发现无bug的
最优解,所有中国人都做错了,或者明显是非最优解
leetcode上是
Number of Digit One My Submissions Question
Total Accepted: 12345 Total Submissions: 55909 Difficulty: Medium
Given an integer n, count the total number of digit 1 appearing in all non-
negative integers less than or equal to n.
For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12,
13.
https://leetcode.com/problems/number-of-digit-one/
最精简最优解是
public int countDigitOne(int n) {
int res = 0;
for (long powerOf10 = 1; powerOf10 <= n; powerOf10 *= 10) {
long left = n / powerOf10, right = n % powerOf10;
res += (left + 8) / 10 * powerOf10;
if (left % 10 == 1)
res += right + 1;
}
return res;
}
career cup上是8.4 Write a method to count the number of 2s between 0 and n
最优解是
public int countDigitTwo(int n) {
int res = 0;
for (long powerOf10 = 1; powerOf10 <= n; powerOf10 *= 10) {
long left = n / powerOf10, right = n % powerOf10;
res += (left + 7) / 10 * powerOf10;
if(left % 10 == 2)
res += right + 1;
}
return res;
}
lintcode上的变种是
Digit Counts
Count the number of k's between 0 and n. k can be 0 - 9.
Example: if n=12, in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have
FIVE 1's (1, 10, 11, 12)
http://lintcode.com/en/problem/digit-counts/
用同样的方法能通过绝大部分test case,但当k=0时答案错误
public int digitCounts(int k, int n) {
int res = 0;
for (long powerOf10 = 1; powerOf10 <= n; powerOf10 *= 10) {
long left = n / powerOf10, right = n % powerOf10;
res += (left + 9 - k) / 10 * powerOf10;
if(left % 10 == k && k > 0)
res += right + 1;
}
return res;
}
这答案该怎么改?
avatar
s*x
2
房顶过了寿命期了,大家有好的contractor推荐么 ?
avatar
S*C
3
想出来了
最优解是
public int digitCounts(int k, int n) {
int res = k == 0 ? 1 : 0;
for (long powerOf10 = 1; powerOf10 <= n; powerOf10 *= 10) {
long left = n / powerOf10, right = n % powerOf10;
if (k != 0)
res += (left + 9 - k) / 10 * powerOf10;
else
res += left / 10 * powerOf10;
if (left % 10 == k && k > 0)
res += right + 1;
}
return res;
}
你可以用Brute force的解法来测试
public int digitCounts(int k, int n) {
int[] record = new int[10];
for (int i = 0; i <= n; i++) {
String s = Integer.toString(i);
for (int j = 0; j < s.length(); j++)
record[s.charAt(j) - '0']++;
}
return record[k];
}
avatar
i*e
4
您需要找的是roofing contractor
可以通过朋友介绍,或看Local 黄页,或去到BBB 网站,还有一些其它的
Contractors Referral 网站都可查询。
avatar
S*C
5
有些test case还是不对,谁帮忙优化一下
比如
Between 0 and 806471: 401888
Between 0 and 806471: 398360
Between 0 and 1396407: 787881
Between 0 and 1396407: 787879

【在 S*******C 的大作中提到】
: 想出来了
: 最优解是
: public int digitCounts(int k, int n) {
: int res = k == 0 ? 1 : 0;
: for (long powerOf10 = 1; powerOf10 <= n; powerOf10 *= 10) {
: long left = n / powerOf10, right = n % powerOf10;
: if (k != 0)
: res += (left + 9 - k) / 10 * powerOf10;
: else
: res += left / 10 * powerOf10;

avatar
s*x
6
谢谢,我去黄页查查~
avatar
x*1
7
恭喜楼主了。 太了不起了。
avatar
s*x
8
谢谢,我去黄页查查~
avatar
s*s
9
我老爸的一个徒弟的工程队换房顶很好, 瓦面弄得非常整齐, 用料也很好
在浙江省北部, 服务范围应该是所在的市..
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。