训练营刷题第六天_python

today: 哈希表 part2

今天加油,能不能超进度,嘿嘿嘿,这样 假装大佬 到 真的成为大佬 的事情,想想都很快乐~
半天过去——
没有超进度哦,明天争取,今天该休息了

leetcode 四题
454.四数相加II
383. 赎金信
15. 三数之和
● 18. 四数之和 (待完善)
______________________ 刷题详情 _________________________

454 已经写过了

在这里插入图片描述

383 简单题

出现了 dict 初始化用[] ,这是list 用的,字典初始化应该用 {}!
(报错是 dict[char] 中 char 应该为 int ,哈哈哈)
在这里插入图片描述

15
308/313 四射五入也算高分了 咳咳,不嘻嘻了,主要是没学到 哈希表的精髓,所以还是再学一下吧 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/dc7e2fbdad6946919405a0b6d64eb86d.png) 找到一个我的思路的进阶版,人家不循环了,人家用指针,下次时间时间超的情况下,考虑将循环换成指针,人家双指针L、R代替我的 j、k,因为排过序了,能保证 L小于R,数组右侧值大。
            L=i+1
            R=n-1
            while(L<R):
                if(nums[i]+nums[L]+nums[R]==0):
                    res.append([nums[i],nums[L],nums[R]])
                    while(L<R and nums[L]==nums[L+1]):
                        L=L+1
                    while(L<R and nums[R]==nums[R-1]):
                        R=R-1
                    L=L+1
                    R=R-1
                elif(nums[i]+nums[L]+nums[R]>0):
                    R=R-1
                else:
                    L=L+1
作者:吴彦祖
链接:https://leetcode.cn/problems/3sum/solutions/39722/pai-xu-shuang-zhi-zhen-zhu-xing-jie-shi-python3-by/
来源:力扣(LeetCode)

18
写完18再吃饭,很难得,我发现我的博客居然有浏览量,hhh,好开心~~
(这题的时间好像是 所有题的总时间,大于18题的完成时间)
写了半个小时了,感觉这个思路不是很好,还是先放着去吃饭,然后再看人家的思想,我很喜欢从中间往两头走,可能是受到了最开始的二分查找的影响,但是似乎在多元素时,多个元素从中间往两头走想的不是很清楚?一般大家普遍从头/尾开始,是不是从中间开始不太好呢?

放弃了,搞不定,明天去学人家的思路,实现之后再来改自己的。

class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        if len(nums)< 4:
            return []
        nums.sort()
        left_index = len(nums)//2 + len(nums)%2 -1
        l_left_index = left_index -1
        right_index = left_index + 1
        r_right_index = right_index + 1
        ans = []

        while 1:
            ele = [nums[l_left_index],nums[left_index],nums[right_index],nums[r_right_index]]
            if sum(ele) == target:
                
                if ele not in ans:
                    ans.append(ele)

                move_l_double = 0
                move_r_double = 0

                if l_left_index > 0:
                    l_left_index = l_left_index - 1
                elif left_index > 1:
                    left_index = left_index - 1
                    move_l_double +=1
                elif right_index > 2:
                    right_index = right_index - 1
                    move_r_double +=1
                else:
                    return ans

                if r_right_index < len(nums) - 1:
                    r_right_index = r_right_index + 1
                elif right_index < len(nums) - 2:
                    right_index = right_index + 1
                    move_r_double +=1
                elif left_index < len(nums) - 3:
                    left_index = left_index + 1
                    move_l_double +=1
                else:
                    return ans

                if move_l_double == 2 or move_r_double == 2:
                    return ans

            elif sum(ele) > target:
                if l_left_index > 0 :
                    l_left_index = l_left_index - 1
                elif left_index > 1:
                    left_index = left_index -1
                elif right_index >2:
                    right_index = right_index -1
                elif r_right_index >3:
                    r_right_index = r_right_index - 1
                else:
                    return ans
            else:
                if r_right_index < len(nums) - 1:
                    r_right_index = r_right_index + 1
                elif right_index < len(nums) - 2:
                    right_index = right_index + 1
                elif left_index < len(nums) - 3:
                    left_index = left_index + 1
                elif l_left_index < len(nums) - 4:
                    l_left_index = l_left_index + 1
                else:
                    return ans
        return ans
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值