LeetCode //C - 335. Self Crossing
ength<=105 1 < = d i s t a n c e [ i ] < = 1 0 5 1 <= distance[i] <= 10^5 1<=distance[i]<=105 From: LeetCode Link: 335. Self Crossing Solution: Ideas: Case 1: When the current move makes a cross with the move...
[LeetCode]139.单词拆分(C++)
1.代码 class Solution {public: bool wordBreak(string s, vector<string>& wordDict) { int length = s.size(); bool *count1 = new bool[length+1]; fill(count1, count1 + length + 1, false); unordered_map<string, boo...
点名(LeetCode)
题目 解题 def takeAttendance(records): """ 找出缺席学生的学号。 参数: records (list of int): 点名记录的升序数组 返回: int: 缺席学生的学号 """ left, right = 0, len(records) - 1 while left <= right: mid = left + (right - left) // 2 # 如果 records...
LeetCode //C - 330. Patching Array
<=104nums is sorted in ascending order. 1 < = n < = 2 31 − 1 1 <= n <= 2^{31} - 1 1<=n<=231−1 From: LeetCode Link: 330. Patching Array Solution: Ideas: miss: This variable represents the smallest sum that can...
搜索二维矩阵 II(LeetCode)
题目 解题 """时间复杂度为 O(m + n),其中 m 是矩阵的行数,n 是矩阵的列数。""" def searchMatrix(matrix, target) -> bool: if not matrix or not matrix[0]: return False # 从右上角开始搜索 row, col = 0, len(matrix[0]) - 1 while row < len(matrix) a...
搜索二维矩阵(LeetCode)
题目 解题 """时间复杂度为 O(log(m * n)),其中 m 是矩阵的行数,n 是矩阵的列数。""" def searchMatrix(matrix, target): if not matrix or not matrix[0]: return False m, n = len(matrix), len(matrix[0]) left, right = 0, m * n - 1 while left...
爱吃香蕉的珂珂(LeetCode)
题目 解题 """时间复杂度: O(n log m),其中 n 是堆的数量,m 是香蕉堆中最大数量。空间复杂度: O(1),只使用了常数空间。""" import math def minEatingSpeed(piles, h): # 二分查找的初始范围 left, right = 1, max(piles) while left < right: # 取中间速度 mid = (left + right)...
至少有k个重复字符的最长子串(LeetCode)
题目 解题 def longestSubstring(s, k): # 如果字符串长度为0或者字符串长度小于k,返回0 if len(s) == 0 or len(s) < k: return 0 # 如果字符串中所有字符的出现次数都大于等于k,返回字符串的长度 if all(s.count(char) >= k for char in set(s)): return len(s) # 否则进行分治 for ch...
LeetCode //C - 316. Remove Duplicate Letters
t h < = 1 0 4 1 <= s.length <= 10^4 1<=s.length<=104s consists of lowercase English letters. From: LeetCode Link: 316. Remove Duplicate Letters Solution: Ideas: 1. lastIndex[]: This array stores the last occ...
三数之和(LeetCode)
题目 解题 def threeSum(nums): nums.sort() # 首先对数组进行排序 result = [] # 用于存储最终结果的列表 for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i - 1]: # 避免重复的三元组 continue left, right = i + 1, len(nums) - 1 while lef...