- 三数之和
-
给你一个包含 n 个整数的数组
nums
,判断nums
中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。注意:答案中不可以包含重复的三元组。
-
class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: nums.sort() res = {} r = 0 for a in range(len(nums)-2): b = a+1 c = len(nums)-1 if a>0 and nums[a] == nums[a-1]: continue if nums[a]>0: break while b<c: if nums[a] + nums[b] + nums[c] == 0: res[r] = [nums[a],nums[b],nums[c]] r += 1 b += 1 while a<b<c and nums[b] == nums[b-1]: b += 1 c -= 1 while a<b<c and nums[c] == nums[c+1]: c -= 1 elif nums[a] + nums[b] + nums[c] > 0: c -= 1 while a<b<c and nums[c] == nums[c+1]: c -= 1 else: b += 1 while a<b<c and nums[b] == nums[b-1]: b += 1 if nums[c] < 0: break x = [res[i] for i in res] return x
通过。指针!!!
-
-
矩阵置零
-
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。
-
class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ # mn额外空间的,应该是类似于flag_matrix的感觉 # n+m的,应该是行列分开记录。 row = [] col = [] r_m = len(matrix) c_m = len(matrix[0]) for r in range(r_m): for c in range(c_m): if matrix[r][c] == 0: row.append(r) col.append(c) for r in range(r_m): for c in range(c_m): if r in row or c in col: matrix[r][c] = 0
通过
-
-
字谜分组
-
给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串.
-
class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: dic = {} for i in strs: tmp = [ord(ii) for ii in i] tmp.sort() #tmp = sum(tmp) tmp = ''.join([chr(i) for i in tmp]) if tmp in dic: dic[tmp].append(i) else: dic[tmp] = [i] res = [] for i in dic: res.append(dic[i]) return res
通过,做了转换。用ascii码排序。
-
-
无重复字符的最长子串
-
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
-
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: res = 0 tmp = '' for i in s: if i not in tmp: tmp += i else: res = max(res,len(tmp)) tmp = tmp.rsplit(i)[-1] + i res = max(res,len(tmp)) return res
通过
-
-
最长回文子串
-
给定一个字符串
s
,找到s
中最长的回文子串。你可以假设s
的最大长度为 1000。 -
class Solution: def longestPalindrome(self, s: str) -> str: if len(s) <= 1: return s elif len(s) == 2: if s[0] == s[-1]: return s else: return s[0] res = s[0] for i in range(1,len(s)): tmp1 = s[:i] tmp2 = s[i:] if tmp1[-1] == tmp2[0]: tmp = '' for j in range(min(len(tmp1),len(tmp2))): if tmp1[-(j+1)] == tmp2[j]: tmp = tmp1[-(j+1)] + tmp + tmp2[j] else: break res = res if len(res) > len(tmp) else tmp tmp1 = s[:i] tmp2 = s[i:] if len(tmp2)>1 and tmp1[-1] == tmp2[1]: tmp = tmp2[0] tmp2 = tmp2[1:] for j in range(min(len(tmp1),len(tmp2))): if tmp1[-(j+1)] == tmp2[j]: tmp = tmp1[-(j+1)] + tmp + tmp2[j] else: break res = res if len(res) > len(tmp) else tmp return res
通过。还有一种动态规划的解法,不会。。。。
-
-
递增的三元子序列
-
给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列。
数学表达式如下:
-
如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1,使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false
-
-
class Solution: def increasingTriplet(self, nums: List[int]) -> bool: # 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) 。 minx = float('inf') maxx = float('inf') for i in nums: if i<minx: minx = i elif minx<i<maxx: maxx = i elif i>maxx: return True return False
通过。遍历数组,找到当前的最小值和最大值。
-