leetcode 中等題(2)
50. Pow(x, n) (中等)
double myPow(double x, int n) { double ans = 1; unsigned long long p; if (n < 0) { p = -n; x = 1 / x; } else { p = n; } while (p) { if (p & 1) ans *= x; x*= x; p >>= 1; } return ans; }
相關推薦
leetcode 中等題(2)
50. Pow(x, n) (中等) double myPow(double x, int n) { double ans = 1; unsigned long long p; if (n < 0) { p
【LeetCode 中等題】2-無重複字元的最長子串
宣告: 今天是中等題第2道題。給定一個字串,請你找出其中不含有重複字元的 最長子串 的長度。以下所有程式碼經過樓主驗證都能在LeetCode上執行成功,程式碼也是借鑑別人的,在文末會附上參考的部落格連結,如果侵犯了博主的相關權益,請聯絡我刪除 (手動比心ღ( ´・ᴗ・`
leetcode演算法題2:兩數相加
題目:給出兩個非空的連結串列用來表示兩個非負的整數。其中,它們各自的位數是按照逆序的方式儲存的,並且它們的每個節點只能儲存一位數字。如果,我們將這兩個數相加起來,則會返回一個新的連結串列來表示它們的和。您可以假設除了數字 0 之外,這兩個數都不會以 0 開頭。 示例:輸入:(2 -&g
【LeetCode 中等題】6-盛最多水的容器
宣告: 今天是中等題第6道題。給定 n 個非負整數 a1,a2,...,an,每個數代表座標中的一個點 (i, ai) 。在座標內畫 n 條垂直線,垂直線 i 的兩個端點分別為 (i,
【LeetCode 中等題】5-字串轉換整數(atoi)
宣告: 今天是中等題第5道題。實現一個 atoi 函式,使其能將字串轉換成整數。以下所有程式碼經過樓主驗證都能在LeetCode上執行成功,程式碼也是借鑑別人的,在文末會附上參考的部落格連結,如果侵犯了博主的相關權益,請聯絡我刪除 (手動比心ღ( ´・ᴗ・` )) 正
【LeetCode 中等題】4-Z字形變換
宣告: 今天是中等題第4道題。將一個給定字串根據給定的行數,以從上往下、從左到右進行 Z 字形排列。以下所有程式碼經過樓主驗證都能在LeetCode上執行成功,程式碼也是借鑑別人的,在文末會附上參考的部落格連結,如果侵犯了博主的相關權益,請聯絡我刪除 (手動比心ღ( ´・ᴗ・` )
【LeetCode 中等題】3-最長迴文子串
宣告: 今天是中等題第3道題。給定一個字串 s,找到 s 中最長的迴文子串。以下所有程式碼經過樓主驗證都能在LeetCode上執行成功,程式碼也是借鑑別人的,在文末會附上參考的部落格連結,如果侵犯了博主的相關權益,請聯絡我刪除 (手動比心ღ( ´・ᴗ・` ))
【LeetCode 中等題】1-兩數之和
宣告: 今天是中等題第1道題。求2個連結串列之和。以下所有程式碼經過樓主驗證都能在LeetCode上執行成功,程式碼也是借鑑別人的,在文末會附上參考的部落格連結,如果侵犯了博主的相關權益,請聯絡我刪除 (手動比心ღ( ´・ᴗ・` )) 正文 題目:給出兩個 非空
LeetCode刷題 2.兩數相加
一、題目 給定兩個非空連結串列來表示兩個非負整數。位數按照逆序方式儲存,它們的每個節點只儲存單個數字。將兩數相加返回一個新的連結串列。 你可以假設除了數字 0 之外,這兩個數字都不會以零開頭。 示例: 輸入:(2 -> 4 -> 3) + (5 ->
leetcode 中等題
2. Add Two Numbers(中等) /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo
【LeetCode 中等題】50-不同的二叉搜尋樹II
題目描述:給定一個整數 n,生成所有由 1 ... n 為節點所組成的二叉搜尋樹。 示例: 輸入: 3 輸出: [ [1,null,3,2], [3,2,null,1], [3,1,null,null,2],
【LeetCode 中等題】49-不同的二叉搜尋樹
題目描述:給定一個整數 n,求以 1 ... n 為節點組成的二叉搜尋樹有多少種? 示例: 輸入: 3 輸出: 5 解釋: 給定 n = 3, 一共有 5 種不同結構的二叉搜尋樹: 1 3 3 2
【LeetCode 中等題】48-復原IP地址
題目描述:給定一個只包含數字的字串,復原它並返回所有可能的 IP 地址格式。 示例: 輸入: "25525511135" 輸出: ["255.255.11.135", "255.255.111.35"] 解法1。暴力解法,用4個變數分別從[1,4)遍歷,再由此確定4
【LeetCode 中等題】47-反轉連結串列II
題目描述:反轉從位置 m 到 n 的連結串列。請使用一趟掃描完成反轉。 說明: 1 ≤ m ≤ n ≤ 連結串列長度。 示例: 輸入: 1->2->3->4->5->N
【LeetCode 中等題】46-子集II
題目描述:給定一個可能包含重複元素的整數陣列 nums,返回該陣列所有可能的子集(冪集)。 說明:解集不能包含重複的子集。 示例: 輸入: [1,2,2] 輸出: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
【LeetCode 中等題】45-格雷編碼
題目描述:格雷編碼是一個二進位制數字系統,在該系統中,兩個連續的數值僅有一個位數的差異。給定一個代表編碼總位數的非負整數 n,列印其格雷編碼序列。格雷編碼序列必須以 0 開頭。 示例 1: 輸入: 2 輸出: [0,1,3,2] 解釋: 00 - 0 01 -
【LeetCode 中等題】44-分隔連結串列
題目描述:給定一個連結串列和一個特定值 x,對連結串列進行分隔,使得所有小於 x 的節點都在大於或等於 x 的節點之前。你應當保留兩個分割槽中每個節點的初始相對位置。 示例: 輸入: head = 1->4->3->
【LeetCode 中等題】43-刪除排序陣列中的重複項II
題目描述:給定一個排序陣列,你需要在原地刪除重複出現的元素,使得每個元素最多出現兩次,返回移除後陣列的新長度。不要使用額外的陣列空間,你必須在原地修改輸入陣列並在使用 O(1) 額外空間的條件下完成。 示例 1: 給定 nums = [1,1,1,2,2,3],函式應返回
【LeetCode 中等題】42-單詞搜尋
題目描述:給定一個二維網格和一個單詞,找出該單詞是否存在於網格中。單詞必須按照字母順序,通過相鄰的單元格內的字母構成,其中“相鄰”單元格是那些水平相鄰或垂直相鄰的單元格。同一個單元格內的字母不允許被重複使用。 示例: board = [ ['A','B','C','E'], ['S'
【LeetCode 中等題】41-子集
題目描述:給定一組不含重複元素的整數陣列 nums,返回該陣列所有可能的子集(冪集)。 說明:解集不能包含重複的子集。 示例: 輸入: nums = [1,2,3] 輸出: [ [3], [1], [2], [1,2,3]