class Solution: def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ ss = list(s) tt = list(t) ss.sort() tt.sort() return ss == tt """ 輸入:"anagram"、"nagaram" 輸出:true Runtime: 32 ms """
排序方法簡寫以下:python
class Solution: def isAnagram(self, s, t): return sorted(list(s)) == sorted(list(t))
class Solution: def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ dict1 = {} # 用字典來維護字符的數量 dict2 = {} for ch in s: dict1[ch] = dict1.get(ch, 0) + 1 # 沒有就新建,有就加1 for ch in t: dict2[ch] = dict2.get(ch, 0) + 1 return dict1 == dict2 """ 輸入:"anagram","nagaram" 輸出:true Runtime: 32 ms """
列表有下列特性:算法
(1)解法一:線性查找查找,O(mn)設計
class Solution: def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ for line in matrix: if target in line: return True return False
(2)解法二:二分查找O(logn)code
class Solution: def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ h = len(matrix) # 高 if h == 0: return False w = len(matrix[0]) # 列 if w == 0: return False left = 0 right = w * h - 1 while left <= right: mid = ((left + right)) // 2 i = mid // w j = mid % w if matrix[i][j] == target: return True elif matrix[i][j] > target: right = mid - 1 else: left = mid + 1 else: return False
保證確定僅有一個結果。blog
leetcode地址:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/排序
例如,列表[1,2,5,4]與目標整數3,1+2=3,結果爲(0,1).ip
(1)方法一:經過二分查找,找到須要的數字。時間複雜度:O(nlogn)leetcode
首先肯定第一個數,再經過給定整數肯定要查找的數,經過二分查找到須要的數。字符串
class Solution: def binary_search(self, li, left, right,val): """ 二分查找 :param li: 輸入的列表 :param val: 輸入的待查找的值 :return: """ while left <= right: # 說明候選區有值 mid = (left + right) // 2 # 由於是下標, 所以要整除2 if li[mid] == val: # 找到待查找的值返回index return mid elif li[mid] > val: # 待查找的值在mid左側 right = mid - 1 # 更新候選區 else: # li[mid] < val # 待查找的值在mid右側 left = mid + 1 # 更新候選區 else: # 沒有找到 return None def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ for i in range(len(nums)): a = nums[i] b = target - a if b >= a: j = self.binary_search(nums, i+1, len(nums)-1, b) else: j = self.binary_search(nums, 0, i-1, b) if j: break return sorted([i+1,j+1])
(2)方法二:針對已經排好序的列表get
leetcode地址:https://leetcode.com/problems/two-sum/description/
class Solution: def binary_search(self, li, left, right,val): """ 二分查找 :param li: 輸入的列表 :param val: 輸入的待查找的值 :return: """ while left <= right: # 說明候選區有值 mid = (left + right) // 2 # 由於是下標, 所以要整除2 if li[mid][0] == val: # 找到待查找的值返回index return mid elif li[mid][0] > val: # 待查找的值在mid左側 right = mid - 1 # 更新候選區 else: # li[mid] < val # 待查找的值在mid右側 left = mid + 1 # 更新候選區 else: # 沒有找到 return None def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ new_nums = [[num, i] for i,num in enumerate(nums)] new_nums.sort(key=lambda x:x[0]) for i in range(len(new_nums)): a = new_nums[i][0] # 數 b = target - a if b >= a: j = self.binary_search(new_nums, i+1, len(new_nums)-1, b) else: j = self.binary_search(new_nums, 0, i-1, b) if j: break return sorted([new_nums[i][1], new_nums[j][1]])