zoukankan      html  css  js  c++  java
  • LeetCode

    题目:
    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
    Note:

        Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
        The solution set must not contain duplicate triplets.

        For example, given array S = {-1 0 1 2 -1 -4},

        A solution set is:
        (-1, 0, 1)
        (-1, -1, 2)

    思路:
    1)用递归,先排序,确定第一个,然后确定第二个,再寻找第三个。

    package sum;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class ThreeSum {
        
        public List<List<Integer>> threeSum(int[] nums) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            int len;        
            if (nums == null || (len = nums.length) < 3) return res;
            Arrays.sort(nums);
            for (int i = 0; i < len - 2; ++i) {
                int rem = 0 - nums[i];
                List<Integer> subRes = new ArrayList<Integer>();
                subRes.add(nums[i]);
                GetTwo(rem, i + 1, nums, len, subRes, res);
                // Move forward if next element is the same as current element
                while (i < len - 1 && nums[i+1] == nums[i]) ++i;
            }
            
            return res;
        }
        
        private void GetTwo(int rem, int start, int[] nums, int len, List<Integer> subRes, List<List<Integer>> res) {
            for (int i = start; i < len - 1; ++i) {
                int last = rem - nums[i];
                List<Integer> cpySubRes = new ArrayList<Integer>(subRes);
                cpySubRes.add(nums[i]);
                GetLast(last, i + 1, nums, len, cpySubRes, res);
                // Move forward if next element is the same as current element
                while (i < len - 1 && nums[i+1] == nums[i]) ++i;
            }
        }
        
        private void GetLast(int rem, int start, int[] nums, int len, List<Integer> subRes, List<List<Integer>> res) {
            for (int i = start; i < len; ++i) {
                if (rem == nums[i]) {
                    subRes.add(nums[i]);
                    res.add(subRes);
                    break;
                }
            }
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ThreeSum t = new ThreeSum();
            int[] S = { -1, 0, 1, 2, -1, -4 };
            List<List<Integer>> res = t.threeSum(S);
            for (List<Integer> subRes : res) {
                for (int i : subRes)
                    System.out.print(i + "	");
                System.out.println("
    ");
            }
        }
    
    }

    2)非递归;排完序之后确立第一个元素,然后用两个指针指向剩下元素的头和尾,两边一夹,然后移动。

    package sum;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class ThreeSum {
        
        public List<List<Integer>> threeSum(int[] nums) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            int len;        
            if (nums == null || (len = nums.length) < 3) return res;
            Arrays.sort(nums);
            for (int i = 0; i < len - 2;) {
                int rem = 0 - nums[i];            
                int left = i + 1;
                int right = len - 1;
                
                while (left < right) {
                    if (nums[left] + nums[right] == rem) {
                        List<Integer> subRes = new ArrayList<Integer>();
                        subRes.add(nums[i]);
                        subRes.add(nums[left]);
                        subRes.add(nums[right]);
                        res.add(subRes);
                        
                        // Filter the duplicated elements.
                        do { ++left; } while (left < len && nums[left] == nums[left - 1]);
                        do { --right; } while (right >= 0 && nums[right + 1] == nums[right]);
                    } else if (nums[left] + nums[right] < rem) {
                        left++;
                    } else {
                        right--;
                    }
                }
                
                // Move forward if the next element is the same as current element.
                do { ++i; } while (i < len && nums[i] == nums[i - 1]);
            }
            
            return res;
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ThreeSum t = new ThreeSum();
            int[] S = { -2,0,1,1,2 };
            List<List<Integer>> res = t.threeSum(S);
            for (List<Integer> subRes : res) {
                for (int i : subRes)
                    System.out.print(i + "	");
                System.out.println("
    ");
            }
        }
    
    }
  • 相关阅读:
    Session的使用与Session的生命周期
    Long-Polling, Websockets, SSE(Server-Sent Event), WebRTC 之间的区别与使用
    十九、详述 IntelliJ IDEA 之 添加 jar 包
    十八、IntelliJ IDEA 常用快捷键 之 Windows 版
    十七、IntelliJ IDEA 中的 Maven 项目初体验及搭建 Spring MVC 框架
    十六、详述 IntelliJ IDEA 创建 Maven 项目及设置 java 源目录的方法
    十五、详述 IntelliJ IDEA 插件的安装及使用方法
    十四、详述 IntelliJ IDEA 提交代码前的 Code Analysis 机制
    十三、IntelliJ IDEA 中的版本控制介绍(下)
    十二、IntelliJ IDEA 中的版本控制介绍(中)
  • 原文地址:https://www.cnblogs.com/null00/p/5025767.html
Copyright © 2011-2022 走看看