LeetCode解題鏈接
7. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
思路
要考慮的問題:
- 負數
- 溢出
對于溢出問題,因為是int型,在計算的過程中,我們只需要將計算的臨時變量定義成long即可!這樣在判斷溢出時會非常簡單。
代碼
public class Solution {
public int reverse(int x) {
long ans = 0;
while(x != 0) {
ans = ans * 10 + x % 10;
x /= 10;
if(ans > Integer.MAX_VALUE || ans < Integer.MIN_VALUE)
return 0;
}
return (int)ans;
}
}
231. Power of Two
Given an integer, write a function to determine if it is a power of two.
思路
- 可以根據
(n & (n - 1)) == 0
來判斷,這是因為2的冪僅有1位是1,其余位全部是0;如果-1,那么1的那位會變成0,后面的位全部為1。 - 可以根據
(n & -n) == n
來判斷,這是 JDK 中 Ingeger 中使用的判斷方法。因為 -n 的二進制表示,恰好相當于(n - 1)取反,而符號位本來就是不同的。本質上和上一種解放是一樣的。
代碼
代碼1:
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n <= 0) return false;
return (n & (n - 1)) == 0;
}
}
代碼2:
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n <= 0) return false;
return (n & -n) == n;
}
}
326. Power of Three
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
思路
如果n是3的冪,那么 n % 3 == 0
代碼
public class Solution {
public bool IsPowerOfThree(int n) {
// 1162261467 是小于 Integer.MAX_VALUE 的3的最大冪數
return n > 0 && (1162261467 % n == 0);
}
}
342. Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example:
Given num = 16, return true. Given num = 5, return false.
Follow up: Could you solve it without loops/recursion?
思路
可以和上一題:Power of Three
一樣,使用作弊的方法……但是這道題顯然是根Power of Two
有關。思考一下,4的冪相對于2的冪,有哪些特點呢?
答案就是:在2的冪的基礎上,位數為1的位置僅出現在奇數的位置
上,例如
- 0000 0001 = 1,是4的冪
- 0000 0010 = 2,不是4的冪
- 0000 0100 = 4,是4的冪
那么如何判斷位數為1的位置僅出現在奇數的位置
上呢?與上0x55(0101 0101)就好了!
代碼
public class Solution {
public boolean isPowerOfFour(int n) {
if(n <= 0) return false;
return ((n & (n - 1)) == 0) && ((n & 0x55555555) != 0);
}
}
172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
分析
在面試時,曾遇到這樣的一道題:
30!結果轉換成3進制,結尾有多少個連續的0?
第一次做的話,感覺沒有思路,但是換個角度想,轉換成3進制,那么十進制中的1~30,哪些因子相乘,才會貢獻出三進制結尾的0呢?當然是:3的倍數。
3, 6, 9, 12, 15 ,18, 21, 24, 27, 30
那么,每一個因子貢獻了多少個0呢?
貢獻了1個0的因子
3 = 3 * 1
6 = 3 * 2
12 = 3 * 4
15 = 3 * 5
21 = 3 * 7
24 = 3 * 8
30 = 3 * 10
貢獻了2個0的因子
9 = 3 * 3
18 = 3 * 3 * 2
貢獻了3個0的因子
27 = 3 * 3 * 3
30/3+30/9+30/27所代表的,就是最終結果。
這是因為:30/3把所有貢獻了0的因子都算了一次,9、18、27已經被算過一次了,但是9和18還有一個因子沒有算,27中還有兩個因子沒有算。
30/9則計算了一次9、18、27,但是27中還有一個因子沒有算。
30/27計算了一次27,至此,所有的因子都計算完畢。
答案就是
30/3+30/9+30/27=10+3+1=14
分析本題
n!中,結尾有多少個連續的0
不能像上題一樣,直接除以10……因為10可以拆分成兩個因子,2和5。但是也不能除以2,因為在任何情況下,2的個數都會多余5的個數,所以,最終除以5就好啦!
100!中,結尾有多少個連續的0?
100/5 + 100/25 + 100/125 = 20 + 4 + 0 = 24
計算公式

在代碼中,一定要注意溢出的問題,如下代碼(我的第一個代碼)就不能通過測試。因為在n很大時,比如Integer.MAX_VALUE,i *= 5溢出了,i一直是小于等于n,所以是死循環!
public int trailingZeroes2(int n) {
int rt = 0;
for (int i = 5; i <= n; i *= 5) {
rt += n / i;
}
return rt;
}
解決方法,把n定義成long型。注意i也要定義成long型,否則在n很大時,主要是i * 5 > Integer.MAX_VALUE后會出錯。
代碼
public class Solution {
public int trailingZeroes(int n) {
int rt = 0;
for (long i = 5; i <= n; i *= 5) {
rt += n / i;
}
return rt;
}
}
總結
對于數字的問題,本質上還是要回歸到對數字本身的分析上,考察了數學的基本功。