LeetCode 數(shù)字 題目匯總

LeetCode解題鏈接

干貨!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.

思路

要考慮的問題:

  • 負數(shù)
  • 溢出

對于溢出問題,因為是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.

思路

  • 可以根據(jù) (n & (n - 1)) == 0 來判斷,這是因為2的冪僅有1位是1,其余位全部是0;如果-1,那么1的那位會變成0,后面的位全部為1。
  • 可以根據(jù) (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的最大冪數(shù)
        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的冪的基礎上,位數(shù)為1的位置僅出現(xiàn)在奇數(shù)的位置上,例如

  • 0000 0001 = 1,是4的冪
  • 0000 0010 = 2,不是4的冪
  • 0000 0100 = 4,是4的冪

那么如何判斷位數(shù)為1的位置僅出現(xiàn)在奇數(shù)的位置上呢?與上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進制,結尾有多少個連續(xù)的0?

第一次做的話,感覺沒有思路,但是換個角度想,轉換成3進制,那么十進制中的1~30,哪些因子相乘,才會貢獻出三進制結尾的0呢?當然是:3的倍數(shù)。

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!中,結尾有多少個連續(xù)的0

不能像上題一樣,直接除以10……因為10可以拆分成兩個因子,2和5。但是也不能除以2,因為在任何情況下,2的個數(shù)都會多余5的個數(shù),所以,最終除以5就好啦!

100!中,結尾有多少個連續(xù)的0?

100/5 + 100/25 + 100/125 = 20 + 4 + 0 = 24

計算公式

在代碼中,一定要注意溢出的問題,如下代碼(我的第一個代碼)就不能通過測試。因為在n很大時,比如Integer.MAX_VALUE,i *= 5溢出了,i一直是小于等于n,所以是死循環(huá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;
    }
}

總結

對于數(shù)字的問題,本質上還是要回歸到對數(shù)字本身的分析上,考察了數(shù)學的基本功。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,779評論 0 33
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,993評論 19 139
  • 壓抑會限制人的天性,常常覺得低落而找不到自己的定位和目標在哪里!今晚看到一則心理短片,有些觸動和幫助,同時也分享給...
    曹明金閱讀 955評論 0 2
  • 據(jù)NBA記者Ian Begley報道,消息人士透露,尼克斯內部的一些人士希望在今年休賽季再一次嘗試交易來森林狼控衛(wèi)...
    Sandy體育風云閱讀 130評論 0 0