国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode-3Sum

2019-11-14 14:53:17
字體:
來源:轉載
供稿:網友

題目:
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 + "/t");            System.out.println("/n");        }    }}

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 + "/t");            System.out.println("/n");        }    }}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太保市| 彰武县| 彭州市| 株洲市| 临邑县| 隆德县| 大方县| 丰台区| 贺州市| 澳门| 抚宁县| 龙门县| 黄冈市| 大城县| 黑河市| 岱山县| 武穴市| 大化| 汉寿县| 苏州市| 青河县| 旬邑县| 三门峡市| 中阳县| 云阳县| 临洮县| 遵化市| 武安市| 锡林浩特市| 阜平县| 太康县| 涟源市| 伊川县| 惠安县| 朝阳市| 利津县| 西和县| 麟游县| 庄浪县| 调兵山市| 贵阳市|