15,三数之和 ————双指针
class solution(): def threeSum(self,nums): """ :param nums: list[int] :return: """ nums.sort() res = [] for i in range(len(nums)-2): if nums[i]>0: break left,right = i+1,len(nums)-1 if nums[i] + nums[left] +nums[left+1] >0 or nums[i] + nums[right-1]+nums[right] <0: continue while left < right: ident = nums[i] + nums[left] + nums[right] if ident == 0: res.append([nums[i],nums[left],nums[right]]) left += 1 right -= 1 elif ident <0: left += 1 elif ident >0: right -= 1 return res # [[-2, -1, 3], [-2, 0, 2], [-2, 1, 1]]
7、整数反转,字符反序输出:
class reverse: def reverse(self,x): if x < (-2**31) or (x > 2**31 -1): return 0 elif x >= 0: return int(str(x)[::-1]) else: return int(str(x)[1:][::-1])*-1
3、无重复的字符子串
class solution: def lengthoflongestSubstring(self,s): st = {} i,ans = 0,0 for j in range(len(s)): if s[j] in st: i = max(st[s[j]],i) ans = max(ans,j-i+1) st[s[j]] = j+1 return ans