classSolution: defcalculateTax(self, brackets: List[List[int]], income: int) -> float: m = 0 j = [0, 0] for i in brackets: if income >= i[0]: m += (i[0] - j[0]) * (i[1] / 100) j = i else: m += (income - j[0]) * (i[1] / 100) break return m
classSolution: defcrackSafe(self, n: int, k: int) -> str: defdfs(u): for x inrange(k): e = u * 10 + x if e notin vis: vis.add(e) v = e % mod dfs(v) ans.append(str(x))
mod = 10 ** (n - 1) vis = set() ans = [] dfs(0) ans.append("0" * (n - 1)) return"".join(ans)
classSolution: defprefixCount(self, words: List[str], pref: str) -> int: ans=0 f=0 for word in words: f=0 iflen(word)>=len(pref): for i inrange(len(pref)): if word[i]!=pref[i]: f=1 if f!=1: ans+=1 return ans
1 2 3 4
classSolution: defprefixCount(self, words: List[str], pref: str) -> int: returnsum(w.startswith(pref) for w in words)
classSolution: defminOperations(self, nums: List[int], x: int) -> int: x = sum(nums) - x vis = {0: -1} ans = inf s, n = 0, len(nums) for i, v inenumerate(nums): s += v if s notin vis: vis[s] = i if s - x in vis: j = vis[s - x] ans = min(ans, n - (i - j)) return -1if ans == inf else ans
classSolution: defminimumLength(self, s: str) -> int: i, j = 0, len(s) - 1 while i < j and s[i] == s[j]: while i + 1 < j and s[i] == s[i + 1]: i += 1 while i < j - 1and s[j - 1] == s[j]: j -= 1 i, j = i + 1, j - 1 returnmax(0, j - i + 1)
classSolution: defaddTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: head = tree = ListNode() val = temp = 0 while temp or l1 or l2: val = temp if l1: val = l1.val + val l1 = l1.next if l2: val = l2.val + val l2 = l2.next temp = val // 10 val = val % 10 tree.next = ListNode(val) tree = tree.next return head.next
classSolution: defclosetTarget(self, words: List[str], target: str, startIndex: int) -> int: m = inf l = len(words) for i, value inenumerate(words): if target == value: m = min(abs(i - startIndex), m) if startIndex > i: m = min(l - startIndex + i, m) if startIndex < i: m = min(l - i + startIndex, m) return -1if m == inf else m
classSolution: defminimumBoxes(self, n: int) -> int: m, a, ans = 1, 1, 1 while m < n: a = a + 1 ans += a m += a * (a + 1) / 2 while m > n: ans = ans - 1 m -= a a = a - 1 if m < n: ans = ans + 1 return ans
我们先将 edges 转换成图 g,然后使用 DFS,判断是否存在从 source 到 destination 的路径 过程中,用数组 v 记录已经访问过的顶点,避免重复访问。 时间复杂度 O(n + m)O(n+m),其中 nn 和 mm 分别是节点数和边数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution: defvalidPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: defdfs(i): if i == destination: returnTrue v.add(i) for j in g[i]: if j notin v and dfs(j): returnTrue returnFalse
g = defaultdict(list) for a, b in edges: g[a].append(b) g[b].append(a) v = set() return dfs(source)
classSolution: deflongestCommonPrefix(self, strs: List[str]) -> str: pre = strs[0] l = len(strs) for i inrange(1, l): pre = self.cmp(pre, strs[i]) ifnot pre: break return pre
defcmp(self, str1, str2): index = 0 l_m = min(len(str1), len(str2)) while index < l_m and str1[index] == str2[index]: index += 1 return str1[:index]
classSolution: defcanChoose(self, groups: List[List[int]], nums: List[int]) -> bool: k=0 for g in groups: while k+len(g)<=len(nums): if nums[k:k+len(g)]==g: k+=len(g) break k+=1 else: returnFalse returnTrue
classSolution: defrotate(self, nums: List[int], k: int) -> None: l = len(nums) n = list(range(l)) for i inrange(l): n[(i + k) % l] = nums[i] for i inrange(l): nums[i] = n[i]
classSolution: deffindMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: defgetKthElement(k): i, j = 0, 0 whileTrue: if i == m: return nums2[j + k - 1] if j == n: return nums1[i + k - 1] if k == 1: returnmin(nums1[i], nums2[j]) newi = min(i + k // 2 - 1, m - 1) newj = min(j + k // 2 - 1, n - 1) pivot1, pivot2 = nums1[newi], nums2[newj] if pivot1 <= pivot2: k -= newi - i + 1 i = newi + 1 else: k -= newj - j + 1 j = newj + 1
m, n = len(nums1), len(nums2) l = m + n if l % 2 == 1: return getKthElement((l + 1) // 2) else: return (getKthElement(l // 2) + getKthElement(l // 2 + 1)) / 2
classSolution: defminElements(self, nums: List[int], limit: int, goal: int) -> int: s=sum(i for i in nums) ans=abs(s-goal)//limit return ans ifabs(s-goal)%limit==0else ans+1
classSolution: defgetLucky(self, s: str, k: int) -> int: temp = '' for i in s: temp += str(ord(i) - 96) ans = 0 num = int(temp) for i inrange(k): ans=0 if num < 10: ans = num break while num > 0: t = num % 10 num = num // 10 ans += t num = ans
return ans
简洁代码
1 2 3 4 5 6 7
classSolution: defgetLucky(self, s: str, k: int) -> int: s = ''.join(str(ord(ch) - ord('a') + 1) for ch in s) for _ inrange(k): t = sum(int(ch) for ch in s) s = str(t) returnint(s)
classSolution: defnearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: ans,mi=-1,inf for i ,(X,Y) inenumerate(points): if X==x or Y==y: d=abs(x-X)+abs(y-Y) if mi>d: ans=i mi=d return ans
classSolution: deflengthOfLongestSubstring(self, s: str) -> int: mx = 0 dq = [] for i in s: while i in dq: del dq[0] dq.append(i) mx = max(mx, len(dq)) return mx
classSolution: defminOperations(self, nums: List[int]) -> int: ans=0 for i inrange(0,len(nums)-1): if nums[i+1]<=nums[i]: ans=ans+nums[i]-nums[i+1]+1 nums[i+1]=nums[i]+1 return ans
classSolution { public: intsearch(vector<int>& nums, int target){ int left = 0, right =nums.size()- 1; while (left <= right) { int mid = left +(right-left) / 2; if (nums[mid] == target) return mid; if (target < nums[mid]) right = mid - 1; elseif (target > nums[mid]) left = mid + 1; } return-1; } };
classSolution: deftwoSum(self, nums: List[int], target: int) -> List[int]: hashtable = {} for i, value inenumerate(nums): if target - value in hashtable.keys(): return [hashtable[target - value], i] else: hashtable[nums[i]] = i