每日打卡:三数之和

打卡:三数之和

心情

今天又在简书上被安利了一部剧《大明王朝1566》
这才是可怕的历史,陈芊芊遇到这部剧,真活不过3集。

读题

leetcode: 15. 三数之和

描述:
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。

测试用例:
输入:[-1, 0, 1, 2, -1, -4]
输出:[ [-1, 0, 1], [-1, -1, 2]]

答案中不可以包含重复的三元组。

乍一想,三层for循环?仔细一想,三层for循环……

思路

前提:数组先排过序,这样可以控制循环的次数
1:第一层循环,将第一个数固定
2:第二层循环,第二个数比第一个数大
3:第三层循环,找到唯一的解能满足三数之和为0
小技巧:第一个数固定的时候,第二个和第三个可以分别从两端向中间靠拢,直到两个数相遇。这样就算是3层循环,也是O(n2)的时间复杂度

实现

public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        //对数组升序排序
        Arrays.sort(nums);
        //用于标记最后一个数,j++的时候,从index处k--比较迅速
        int index,temp;
        for (int i = 0; i < nums.length - 2 && nums[i] <= 0; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            index = nums.length - 1;
            //j从i+1到index,若nums[i] + nums[j] + nums[index] < 0则让j++
            for (int j = i + 1; j < index && (temp = nums[i] + nums[j]) <= 0; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1] || temp + nums[index] < 0) {
                    continue;
                }
                //k从index到j,当nums[i] + nums[j] + nums[k] >= 0则让k--
                for (int k = index; k > j && (temp = nums[i] + nums[j] + nums[k]) >= 0; k--) {
                    if (temp == 0) {
                        result.add(Arrays.asList(nums[i], nums[j], nums[k]));
                        //标记当i固定时,k的最大下标,等待j再一次++
                        index = k;
                        break;
                    }
                }
            }
        }
        return result;
    }

为了让代码尽可能地行数少,写的比较冗长。只要注释足够就不影响理解。

提交

在这里插入图片描述
在这里插入图片描述
这已经是我优化了很多次的结果了。去掉了Set集合(用了相邻数不重复),去掉了存放结果的list(用了Arrays.asList())。执行效率也提升了很多,就是名次没有提升……
第一次通过的代码如下:

public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> element;
        Set<Integer> trimSet;
        //对数组升序排序
        Arrays.sort(nums);
        //先找出所有的解
        int temp = 0,index;
        //锚定第一个数
        for (int i = 0; i < nums.length - 2; i++) {
            if(nums[i] >0){
                break;
            }else if(i>0 && nums[i]==nums[i-1]){
                continue;
            }
            trimSet = new HashSet<>();
            index = nums.length - 1;
            //第二个数从i+1开始
            for (int j = i + 1; j < nums.length - 1; j++) {
                temp = nums[i] + nums[j];
                if(temp > 0){
                    break;
                }else if(!trimSet.add(nums[j])){
                    continue;
                }
                element = new ArrayList<>();
                element.add(nums[i]);
                element.add(nums[j]);
                //第三个数从后向前遍历
                for (int k = index; k > j; k--) {
                    temp = nums[i] + nums[j] + nums[k];
                    if (temp == 0) {
                        element.add(nums[k]);
                        result.add(element);
                        index = k;
                        break;
                    }else if(temp < 0){
                        break;
                    }
                }
            }
        }
        return result;
    }

标答

public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> lists = new ArrayList<>();
        //排序
        Arrays.sort(nums);
        //双指针
        int len = nums.length;
        for(int i = 0;i < len;++i) {
            if(nums[i] > 0) return lists;
            if(i > 0 && nums[i] == nums[i-1]) continue;
            int curr = nums[i];
            int L = i+1, R = len-1;
            while (L < R) {
                int tmp = curr + nums[L] + nums[R];
                if(tmp == 0) {
                    List<Integer> list = new ArrayList<>();
                    list.add(curr);
                    list.add(nums[L]);
                    list.add(nums[R]);
                    lists.add(list);
                    //两个指针向中间靠拢
                    while(L < R && nums[L+1] == nums[L]) ++L;
                    while (L < R && nums[R-1] == nums[R]) --R;
                    ++L;
                    --R;
                } else if(tmp < 0) {
                    ++L;
                } else {
                    --R;
                }
            }
        }
        return lists;
    }

标答取自评论区某大神
感觉差不多的思路啊,咋就差了呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一种流行的JavaScript框架,用于构建用户界面。要实现每日打卡功能,你可以按照以下步骤进行: 1. 创建一个Vue组件,用于显示打卡相关的信息和按钮。 2. 在组件的data属性中定义一个变量,用于记录打卡状态,比如isPunched。 3. 在组件的methods属性中定义一个方法,用于处理打卡逻辑。当用户点击打卡按钮时,该方法会被调用。 4. 在方法中,你可以使用JavaScript的Date对象获取当前日期,并将其与用户上次打卡的日期进行比较。如果日期不一致,表示用户需要进行打卡操作。 5. 如果需要保存用户的打卡记录,你可以使用浏览器的本地存储(localStorage)或者发送请求到服务器来保存数据。 下面是一个简单的示例代码: ```html <template> <div> <p v-if="isPunched">今日已打卡</p> <p v-else>今日未打卡</p> <button @click="punch">打卡</button> </div> </template> <script> export default { data() { return { isPunched: false }; }, methods: { punch() { const lastPunchDate = localStorage.getItem('lastPunchDate'); const currentDate = new Date().toLocaleDateString(); if (lastPunchDate !== currentDate) { // 执行打卡逻辑 // ... // 更新打卡状态和日期 this.isPunched = true; localStorage.setItem('lastPunchDate', currentDate); } } } }; </script> ``` 在上面的示例中,我们使用了Vue的条件渲染指令(v-if和v-else)来根据打卡状态显示不同的文本。当用户点击打卡按钮时,会调用punch方法进行打卡逻辑的处理。我们使用localStorage来保存用户的上次打卡日期,并在下次打卡时进行比较。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值