国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] 3Sum

Sunxb / 1534人閱讀

摘要:雙指針?lè)ǖ慕夥āH缓笥煤蛫A逼找到使三數(shù)和為零的三數(shù)數(shù)列,放入結(jié)果數(shù)組。對(duì)于這三個(gè)數(shù),如果循環(huán)的下一個(gè)數(shù)值和當(dāng)前數(shù)值相等,就跳過(guò)以避免中有相同的解。

Problem

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.

Notice

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.

Example

For example, given array S = {-1 0 1 2 -1 -4}, A solution set is:

(-1, 0, 1)
(-1, -1, 2)
Note

雙指針?lè)ǎ?b>O(n^2)的解法。
遍歷第一個(gè)到倒數(shù)第三個(gè)數(shù)nums[i],作為三數(shù)數(shù)列的隊(duì)首;
nums[i]后面的一個(gè)數(shù)作為三數(shù)數(shù)列中的第二個(gè)數(shù)nums[left]
數(shù)組中最后一個(gè)數(shù)nums[nums.length-1]作為三數(shù)數(shù)列中的第三個(gè)數(shù)nums[right]
然后用leftright夾逼找到使三數(shù)和為零的三數(shù)數(shù)列,放入結(jié)果數(shù)組res
對(duì)于這三個(gè)數(shù),如果循環(huán)的下一個(gè)數(shù)值和當(dāng)前數(shù)值相等,就跳過(guò)以避免res中有相同的解。

Solution
public class Solution {
    public ArrayList> threeSum(int[] A) {
        ArrayList> res = new ArrayList();
        if (A.length < 3) return null;
        Arrays.sort(A);
        for (int i = 0; i <= A.length-3; i++) {
            int left = i+1, right = A.length-1;
            if (i != 0 && A[i] == A[i-1]) continue;
            while (left < right) {
                int sum = A[i]+A[left]+A[right];
                if (sum == 0) {
                    ArrayList temp = new ArrayList();
                    temp.add(A[i]);
                    temp.add(A[left]);
                    temp.add(A[right]);
                    res.add(temp);
                    left++;
                    right--;
                    while (left < right && A[left] == A[left-1]) left++;
                    while (left < right && A[right] == A[right+1]) right--;
                }
                else if (sum < 0) left++;
                else right--;
            }
        }
        return res;
    }
}
update 2018-9
class Solution {
    public List> threeSum(int[] nums) {
        List> res = new ArrayList<>();
        if (nums == null || nums.length < 3) return res;
        
        Arrays.sort(nums);

        for (int i = 0; i < nums.length-2; i++) {
            if (i == 0 || (i > 0 && nums[i] != nums[i-1])) {
                int sum = 0-nums[i];
                int l = i+1, r = nums.length-1;
                
                while (l < r) {
                    int curSum = nums[l]+nums[r];
                    if (curSum == sum) {
                        res.add(Arrays.asList(nums[i], nums[l++], nums[r--]));
                        while (l < r && nums[l] == nums[l-1]) l++;
                        while (l < r && nums[r] == nums[r+1]) r--;
                    } else if (curSum < sum) {
                        l++;
                    } else {
                        r--;
                    }
                }
                
            }
        }
        return res;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/65953.html

相關(guān)文章

  • [LintCode/LeetCode] 3Sum Closest

    摘要:這個(gè)題和的做法基本一樣,只要在循環(huán)內(nèi)計(jì)算和最接近的和,并賦值更新返回值即可。 Problem Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three intege...

    ShevaKuilin 評(píng)論0 收藏0
  • [LintCode/LeetCode] Word Break

    Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...

    dunizb 評(píng)論0 收藏0
  • [LintCode/LeetCode] First Unique Character in a S

    Problem Given a string, find the first non-repeating character in it and return its index. If it doesnt exist, return -1. Example Given s = lintcode, return 0. Given s = lovelintcode, return 2. Tags A...

    Xufc 評(píng)論0 收藏0
  • [LintCode/LeetCode] Find Median From / Data Stream

    摘要:建立兩個(gè)堆,一個(gè)堆就是本身,也就是一個(gè)最小堆另一個(gè)要寫一個(gè),使之成為一個(gè)最大堆。我們把遍歷過(guò)的數(shù)組元素對(duì)半分到兩個(gè)堆里,更大的數(shù)放在最小堆,較小的數(shù)放在最大堆。同時(shí),確保最大堆的比最小堆大,才能從最大堆的頂端返回。 Problem Numbers keep coming, return the median of numbers at every time a new number a...

    zxhaaa 評(píng)論0 收藏0
  • [LintCode/LeetCode] Implement Trie

    摘要:首先,我們應(yīng)該了解字典樹(shù)的性質(zhì)和結(jié)構(gòu),就會(huì)很容易實(shí)現(xiàn)要求的三個(gè)相似的功能插入,查找,前綴查找。既然叫做字典樹(shù),它一定具有順序存放個(gè)字母的性質(zhì)。所以,在字典樹(shù)的里面,添加,和三個(gè)參數(shù)。 Problem Implement a trie with insert, search, and startsWith methods. Notice You may assume that all i...

    付永剛 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<