zoukankan      html  css  js  c++  java
  • 15. 3Sum

    和那个4 SUM没啥区别。。。

    排序,遍历固定左边,右边2个滑窗从最大往中间滑。

    主要就是重复的处理:
    一个是找到一个有效组合后,要继续滑,不能BREAK,然后继续滑之前跳过重复;

    while(L < R && nums[L] == nums[L+1]) L++;
                        while(L < R && nums[R] == nums[R-1]) R--;
                        L++;R--;
    

    一个是固定值遇到相同是要直接跳过的。

    while(i+1 < nums.length && nums[i+1] == nums[i])i++;
    

    最后还要注意,别做错了。

    public class Solution 
    {
        public List<List<Integer>> threeSum(int[] nums) 
        {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            if(nums.length < 3) return res;
            
            Arrays.sort(nums);
            
            for(int i = 0; i < nums.length-2;i++)
            {
                int L = i + 1;
                int R = nums.length-1;
                int need = 0 - nums[i];
                while(L < R)
                {
                    int total = nums[L] + nums[R];
                    if(total > need) R--;
                    else if(total < need) L++;
                    else
                    {
                        List<Integer> tempList = new ArrayList<>();
                        tempList.add(nums[i]);tempList.add(nums[L]);tempList.add(nums[R]);
                        res.add(new ArrayList<>(tempList));
                        
                        while(L < R && nums[L] == nums[L+1]) L++;
                        while(L < R && nums[R] == nums[R-1]) R--;
                        L++;R--;
                    }
                }
                
                while(i+1 < nums.length && nums[i+1] == nums[i])i++;
            }
            return res;
        }
        
    
    }
    

    二刷。

    按面试刷。。

    经典的3SUM,固定一个,然后夹逼。 需要注意要求是SET,所以去重。

    Time: O(nlgn) + O(n²)
    Space: O(n)

    public class Solution {
        public List<List<Integer>> threeSum(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
            if (nums.length < 3) return res;
            
            Arrays.sort(nums);
            
            for (int i = 0; i < nums.length - 2; i++) {
                if (i != 0 && nums[i] == nums[i-1]) continue;
                int temp = nums[i];
                int left = 0 - temp;
                int l = i + 1;
                int r = nums.length - 1;
                while (l < r) {
                    if (nums[l] + nums[r] == left) {
                        List<Integer> tempList = new ArrayList<>();
                        tempList.add(nums[i]);
                        tempList.add(nums[l++]);
                        tempList.add(nums[r--]);
                        res.add(tempList);
                        while(l < r && nums[l] == nums[l-1])
                            l ++;
                        while(l < r && nums[r] == nums[r+1])
                            r --;
                    } else if (nums[l] + nums[r] < left) {
                        l ++;
                    } else {
                        r --;
                    }
                }
            }
            
            return res;
            
        }
    }
    
  • 相关阅读:
    Kill Processes in Linux
    How to Setup Chroot SFTP in Linux (Allow Only SFTP, not SSH)
    156 Useful Run Commands
    6
    pandas groupby合并列字符串
    一个ROS配置的脚本
    Mybatis 学习记录
    Android搭建code server
    CF 1616D. Keep the Average High
    第七章:(1)Redis 的发布订阅
  • 原文地址:https://www.cnblogs.com/reboot329/p/5877848.html
Copyright © 2011-2022 走看看