力扣日刷26110

断更了许久,今天开始重新学习:打算跟着视频学习了

视频链接

今天一共写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
相关推荐
踩坑记录18 小时前
leetcode hot100 easy 101. 对称二叉树 递归 层序遍历 bfs
算法·leetcode·宽度优先
2501_9403152619 小时前
leetcode182动态口令(将字符的前几个元素放在字符串后面)
算法
老鼠只爱大米20 小时前
LeetCode经典算法面试题 #98:验证二叉搜索树(递归法、迭代法等五种实现方案详解)
算法·leetcode·二叉树·递归·二叉搜索树·迭代
疯狂的喵1 天前
C++编译期多态实现
开发语言·c++·算法
scx201310041 天前
20260129LCA总结
算法·深度优先·图论
2301_765703141 天前
C++中的协程编程
开发语言·c++·算法
m0_748708051 天前
实时数据压缩库
开发语言·c++·算法
小魏每天都学习1 天前
【算法——c/c++]
c语言·c++·算法
智码未来学堂1 天前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
Halo_tjn1 天前
基于封装的专项 知识点
java·前端·python·算法