随笔分类 - 二分查找
摘要:方法一:二分查找。 class Solution(object): # 二分法 def missingNumber(self, nums): """ :type nums: List[int] :rtype: int """ i, j = 0, len(nums) - 1 while i <= j:
阅读全文
摘要:方法一:二分查找。 class Solution(object): # 二分法 def countNegatives(self, grid): """ :type grid: List[List[int]] :rtype: int """ ans = 0 for nums in grid: if n
阅读全文
摘要:class Solution(object): def kWeakestRows(self, mat, k): """ :type mat: List[List[int]] :type k: int :rtype: List[int] """ power = [sum(line) for line
阅读全文
摘要:class Solution(object): def maxDepthAfterSplit(self, seq): """ :type seq: str :rtype: List[int] """ res = [] if not seq: return res depth, max_depth =
阅读全文
摘要:思路:二分法。 class Solution(object): def nextGreatestLetter(self, letters, target): letters = list(set(letters)) letters.sort() if target in letters: index
阅读全文
摘要:class Solution(object): def findRadius(self, houses, heaters): """ :type houses: List[int] :type heaters: List[int] :rtype: int """ ans = [] heaters.s
阅读全文
摘要:class Solution(object): def arrangeCoins(self, n): """ :type n: int :rtype: int """ return int(2 ** 0.5 * (n + 1 / 8) ** 0.5 - 1 / 2) if __name__ == '
阅读全文
摘要:class Solution(object): def isSubsequence(self, s, t): """ :type s: str :type t: str :rtype: bool """ if len(s) > len(t): return False elif len(s) ==
阅读全文
摘要:方法一:二分查找。 class Solution(object): # 二分查找 def kthSmallest(self, matrix, k): """ :type matrix: List[List[int]] :type k: int :rtype: int """ n = len(matr
阅读全文
摘要:题意:即nums长为n+1,里面的元素值范围:[1,n],有且仅有一个重复值,但该值可以重复多次,所以[1,n]有的数可以不在nums里。 方法一:二分查找。 class Solution(object): # 二分查找 def findDuplicate(self, nums): low = 1
阅读全文
摘要:class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ # 处理target不在nums中的情况 if
阅读全文
摘要:思路: eg:11÷3 ∵11>3 ∴res>=1 又∵11>3+3=6 ∴res>=2 (1+1) 又∵11<6+6=12 ∴res<4 (2+2) 即2<=res<4 递归部分: 又11-6=5>3 ∴restemp >= 1 又∵5<6 (3+3) ∴restemp<2 (1+1) 即1<=r
阅读全文