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

    public class Solution {
        
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        public List<List<Integer>> threeSum(int[] nums) {
            
            if(nums.length < 3 || nums == null)
            return res;
            
            Arrays.sort(nums); 
            
            
            int len = nums.length;  
            for (int i = 0; i < len; i++) 
            {  
                if (i > 0 && nums[i] == nums[i-1]) 
                continue;  
                find(nums, i+1, len-1, nums[i]); 
            }  
            
            
            return res;
            
        }
        
        public void find(int []nums, int st, int end, int tar)
        {
            while(st < end)
            {
                if(nums[st]+nums[end]+tar == 0)
                {
                    List<Integer> ans = new ArrayList<Integer>();  
                    ans.add(tar);  
                    ans.add(nums[st]);  
                    ans.add(nums[end]);  
                    res.add(ans);
                    while(st < end && nums[st] == nums[st+1]) st++;
                    while(st < end && nums[end] == nums[end-1]) end--;
                    st++;
                    end--;
                }
                
                else if(nums[st] + nums[end] + tar < 0)
                    st++;
                else
                    end--;
                
            }
            
        }
        
        
    }
  • 相关阅读:
    CTSC2018滚粗记
    HNOI2018游记
    NOIWC 2018游记
    PKUWC2018滚粗记
    HNOI2017 游记
    NOIP2017题解
    [HNOI2017]抛硬币
    [HNOI2017]大佬
    NOIP难题汇总
    [NOI2013]树的计数
  • 原文地址:https://www.cnblogs.com/zyqBlog/p/5977768.html
Copyright © 2011-2022 走看看