
1. A+B problem
- Write a function that add two numbers A and B. You should not use + or any arithmetic operators.
- 加法分两部分:按位异或得到当前位的值,按位与得到当前位产生的进位。
1
2
3
4int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
return b == 0 ? a : aplusb(a^b, ((a&b) << 1));
}
3. Digit Counts
-
Count the number of k’s between 0 and n. k can be 0 - 9.
-
把数字转换成字符串来做,想法很新,值得注意,但是感觉复杂度还是很高。
1 |
int digitCounts(int k, int n) { |
4. Ugly Number II
-
Ugly number is a number that only have factors 2, 3 and 5.
-
这个题还是没理解。下面是别人的解法:http://www.cnblogs.com/grandyang/p/4743837.html
1 |
int nthUglyNumber(int n) { |




近期评论