
左右指针
一杯小熊
这个作者很懒,什么都没留下…
展开
-
剑指Offer 57- 和为s的连续整数序列
class Solution: def findContinuousSequence(self, target: int) -> List[List[int]]: #双指针 i, j, s, res = 1, 2, 3, [] while i < j: if s == target: res.append(list(range(i, j+1))) ...原创 2021-03-11 21:06:47 · 76 阅读 · 0 评论 -
剑指Offer 48 最长不含重复字符的子字符串
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if not s: return 0 n = len(s) #动态规划 dic = {} res, tmp = 0, 0 for j in range(n): i = dic.get(s[j], -1) dic...原创 2021-03-07 22:12:44 · 91 阅读 · 0 评论 -
剑指Offer 21. 调整数组顺序使奇数位于偶数前面
class Solution: def exchange(self, nums: List[int]) -> List[int]: left, right = 0, len(nums)-1 while left < right: while left < right and nums[left] % 2 == 1: left += 1 while left <...原创 2021-02-25 22:49:07 · 86 阅读 · 0 评论 -
三数之和/LeetCode.No.15/类似题目:No.1:两数之和 No.16:最接近的三数之和
思路:1.先排序保证不重复以及减小复杂度 2. 左右双指针,左指针右移,右指针左移 代码实现: class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: n = len(nums) if n < 3: return [] nums.sort()#先排序,保证不重复并且减少时间复杂度 res = []..原创 2021-01-17 16:28:45 · 134 阅读 · 0 评论