由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 讨论一道所有人做的都不好的lintcode题目
相关主题
leetcode,lintcode各两遍,还是没找到工作lintcode delete digits怎么做?
最近所有onsite的题都做出来了FB电面面经,顺便求各种referral
lintcode题有270道An interview question from a well-known small company
问个google面试题twitter online test 面筋
leetcode 挂了求看代码 Plus One
lintcode不让下载submissions了??请教一道算法题
一道数组deduplicate变种题,求个思路。讨论下lintcode上的两道题吧,其中一题fb onsite碰到过
弱问一下lintcode里有一道“Delete Digits”是什么意思求解lintcode Wood Cut 问题
相关话题的讨论汇总
话题: powerof10话题: res话题: int话题: left话题: 10
进入JobHunting版参与讨论
1 (共1页)
S*******C
发帖数: 822
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;
}
这答案该怎么改?
S*******C
发帖数: 822
2
想出来了
最优解是
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];
}
S*******C
发帖数: 822
3
有些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;

x*******1
发帖数: 28835
4
恭喜楼主了。 太了不起了。
1 (共1页)
进入JobHunting版参与讨论
相关主题
求解lintcode Wood Cut 问题leetcode 挂了
Lintcode 和 Leetcode的区别是啥lintcode不让下载submissions了??
lintcode Majority Number II 怎么做?一道数组deduplicate变种题,求个思路。
求解lintcode Majority Number III弱问一下lintcode里有一道“Delete Digits”是什么意思
leetcode,lintcode各两遍,还是没找到工作lintcode delete digits怎么做?
最近所有onsite的题都做出来了FB电面面经,顺便求各种referral
lintcode题有270道An interview question from a well-known small company
问个google面试题twitter online test 面筋
相关话题的讨论汇总
话题: powerof10话题: res话题: int话题: left话题: 10