leetcode 刷題(python)之漢明距離
兩個整數之間的漢明距離指的是這兩個數字對應二進位制位不同的位置的數目。
給出兩個整數 x 和 y,計算它們之間的漢明距離。
注意:
0 ≤ x, y < 231.
示例:
輸入: x = 1, y = 4
輸出: 2
解釋:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
上面的箭頭指出了對應二進位制位不同的位置。
class Solution: def hammingDistance(self, x, y): """ :type x: int :type y: int :rtype: int """ n = x ^ y # 兩個十進位制數按位異或 n = bin(n) # 重新複製後的n是一個字串 print("n type",type(n)) return n.count('1')
相關推薦
leetcode 刷題(python)之漢明距離
兩個整數之間的漢明距離指的是這兩個數字對應二進位制位不同的位置的數目。給出兩個整數 x 和 y,計算它們之間的漢明距離。注意:0 ≤ x, y < 231.示例:輸入: x = 1, y = 4輸出: 2解釋:1 (0 0 0 1)4 (0 1 0 0)↑ ↑上面的箭頭
LeetCode 刷題(python) 之
移動零給定一個數組 nums, 編寫一個函式將所有 0 移動到它的末尾,同時保持非零元素的相對順序。例如, 定義 nums = [0, 1, 0, 3, 12],呼叫函式之後, nums 應為 [1, 3, 12, 0, 0]。注意事項:必須在原陣列上操作,不要為一個新陣列分
Leetcode刷題(python)之 兩個陣列的交集 II
給定兩個陣列,寫一個方法來計算它們的交集。例如:給定nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意: 輸出結果中每個元素出現的次數,應與元素在兩個陣列中出現的次數一致。 我們可以不考慮輸出結果的順序。跟進:如果給定的
LeetCode刷題(Python)——每個節點的右向指標
題目描述 給定一個二叉樹 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } 填充它的每個 next 指標,讓這個指標指向其下一個右側節點。
leetcode 刷題(1)--- 兩數之和
給定一個整數陣列和一個目標值,找出陣列中和為目標值的兩個數。 你可以假設每個輸入只對應一種答案,且同樣的元素不能被重複利用。 示例: 給定 nums = [2, 7, 11, 15], target = 9 因為 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0
leetcode 刷題(2)--- 兩數相加
nullptr solution 原因 == 表示 node etc 數字 個數字 給定兩個非空鏈表來表示兩個非負整數。位數按照逆序方式存儲,它們的每個節點只存儲單個數字。將兩數相加返回一個新的鏈表。 你可以假設除了數字 0 之外,這兩個數字都不會以零開頭。 示例: 輸
LeetCode刷題(Java)
第一題 class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<Integer, Integer>()
leetcode 刷題 (599)
最近在刷LeetCode,從簡單的開始刷起,補一下自己code和演算法基礎。 下面是dict方面的題目: 主要用到了dict兩個主要的屬性: ①enumerate() enumerate(sequence, [start=0]) sequence -- 一個序列、迭代器或其他支援
LeetCode刷題(三)
LeetCode 是個著名的程式設計題庫,裡邊有很多面試、演算法題目,多刷刷對提高程式設計水平很有幫助,避免因為天天寫業務程式碼而停滯不前。最近玩了下挺有意思的,決定慢慢的刷起來,在這裡記錄一下我的刷
LeetCode刷題(48)--Remove Duplicates from Sorted List II
cur表示當前所在的Node,對於重複出現的Node會移到最後一個。 pre記錄結果,res用於返回,pre的更改會體現在res上。 如果pre.next == cur,則說明沒有重複,此時pre = pre.next Node進入res; 否則,則重複,只是將pre.next 移到下
LeetCode刷題(46)--Search in Rotated Array
特別處理等於的情況 class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target: int
LeetCode刷題(50)--Word Search
DFS演算法 class Solution(object): def exist(self, board, word): """ :type board: List[List[str]] :type word: str
LeetCode刷題(49)--Minimum Window Substring
class Solution(object): def minWindow(self, s, t): """ :type s: str :type t: str :rtype: str """
LeetCode刷題(二) —— 整數拆分
題目:給定一個正整數 n,將其拆分為至少兩個正整數的和,並使這些整數的乘積最大化。 返回你可以獲得的最大乘積。例如,給定 n = 2,返回1(2 = 1 + 1);給定 n = 10,返回36(10 = 3 + 3 + 4)。注意:你可以假設 n 不小於2且不大於58。分析:
LeetCode刷題(二)
LeetCode 是個著名的程式設計題庫,裡邊有很多面試、演算法題目,多刷刷對提高程式設計水平很有幫助,避免因為天天寫業務程式碼而停滯不前。最近玩了下挺有意思的,決定慢慢的刷起來,在這裡記錄一下我的刷
LeetCode刷題(C++)——Same Tree(Easy)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are c
LeetCode刷題(四)
LeetCode 是個著名的程式設計題庫,裡邊有很多面試、演算法題目,多刷刷對提高程式設計水平很有幫助,避免因為天天寫業務程式碼而停滯不前。最近玩了下挺有意思的,決定慢慢的刷起來,在這裡記錄一下我的刷
leetcode刷題(六)路徑總和I、II、III
(一)112題題目地址:https://leetcode-cn.com/problems/path-sum/description/題目描述:給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上所有節點值相加等於目標和。解決方案:/** * De
lintcode刷題(Python)(2)
11! = 39916800,因此應該返回 2 一開始也沒多想,隨便設計了一個演算法,迴圈遍歷,但時間複雜度不滿足,GG了: class Solution: """ @param: : An integer @return: An integer, denote the numb
lintcode刷題(python)——(4)
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function two