断更了许久,今天开始重新学习:打算跟着视频学习了
今天一共写6题:都是相向双指针的题目
1.167 2.15 3.2824 4.16 5.18 6.611
1.两数之和题目复习,构建相向双指针
python
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
n = len(numbers)
left, right = 0, n-1
while 1:
if numbers[left] + numbers[right] == target:
return [left+1,right+1]
elif numbers[left] + numbers[right] > target:
right -= 1
else:
left += 1
2.三数之和题目复习,难点1.定一个数然后变成两数之和题目。还有就是ijk的更新。
python
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
n = len(nums)
res =[]
for i in range(n-2):
x = nums[i]
if i>0 and x == nums[i-1]:
continue
j = i+1
k = n-1
while k > j:
if x + nums[j] + nums[k] >0:
k -= 1
elif x + nums[j] + nums[k] <0:
j += 1
else:
res.append([x,nums[j],nums[k]])
j += 1
while k>j and nums[j] == nums[j-1]:
j += 1
k -= 1
while k>j and nums[k] ==nums[k+1]:
k -= 1
return res
3.2824 有暴力解法:两个for循环可以解决

暴力解法:
python
class Solution:
def countPairs(self, nums: List[int], target: int) -> int:
count = 0
n = len(nums)
for i in range(n-1):
j = i+1
while j <n:
if nums[i] +nums[j] <target:
count +=1
j +=1
return count
相向双指针的解法:主要是需要理清楚count到底应该加几的关系
python
class Solution:
def countPairs(self, nums: List[int], target: int) -> int:
count = 0
n = len(nums)
nums.sort()
l,r = 0,n-1
while l < r:
if nums[l] + nums[r] <target:
count = count + r -l
l += 1
else:
r -= 1
return count
4.利用双向指针,参数更新的地方卡了我很久, 先操作再更新参数
python
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums.sort()
ans = nums[0]+nums[1]+nums[2]
n = len(nums)
for i in range(n-2):
if i >0 and nums[i] == nums[i-1]:
continue
j = i+1
k = n-1
while j<k:
if nums[i] +nums[k]+nums[j] == target:
return target
elif nums[i] +nums[k]+nums[j] < target:
if abs(target -ans) > abs(target-(nums[i] +nums[k]+nums[j])):
ans = nums[i] +nums[k]+nums[j]
j += 1
elif nums[i] +nums[k]+nums[j] > target:
if abs(target -ans) > abs(target-(nums[i] +nums[k]+nums[j])):
ans = nums[i] +nums[k]+nums[j]
k -=1
return ans
5.四数之和,我按照三数之和做的,但效率低

python
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
nums.sort()
n = len(nums)
res = []
for i in range(n-3):
if i >0 and nums[i] == nums[i-1]:
i += 1
for j in range(i+1,n-2):
if j>i+1 and nums[j] ==nums[j-1]:
j +=1
c,d=j+1,n-1
while c<d:
if nums[i] +nums[j] +nums[c] +nums[d] ==target:
if [nums[i],nums[j],nums[c],nums[d]] not in res:
res.append([nums[i],nums[j],nums[c],nums[d]])
c += 1
while nums[c] ==nums[c-1] and c<d :
c +=1
d -= 1
while nums[d] ==nums[d+1] and c<d :
d -=1
elif nums[i] +nums[j] +nums[c] +nums[d] <target:
c += 1
elif nums[i] +nums[j] +nums[c] +nums[d] >target:
d -= 1
return res
6.三角形:
这道题我没有做出来,思路有点像统计下标那道题。重写了一遍也没写出来,绷不住了哈哈哈。我再来一遍看看怎么样。ok最后一遍写出来来了
python
class Solution:
def triangleNumber(self, nums: List[int]) -> int:
nums.sort()
ans = 0
for k in range(2, len(nums)):
c = nums[k]
i = 0 # a=nums[i]
j = k - 1 # b=nums[j]
while i < j:
if nums[i] + nums[j] > c:
# 由于 nums 已经从小到大排序
# nums[i]+nums[j] > c 同时意味着:
# nums[i+1]+nums[j] > c
# nums[i+2]+nums[j] > c
# ...
# nums[j-1]+nums[j] > c
# 从 i 到 j-1 一共 j-i 个
ans += j - i
j -= 1
else:
# 由于 nums 已经从小到大排序
# nums[i]+nums[j] <= c 同时意味着
# nums[i]+nums[j-1] <= c
# ...
# nums[i]+nums[i+1] <= c
# 所以在后续的内层循环中,nums[i] 不可能作为三角形的边长,没有用了
i += 1
return ans