Leetcode涔?5. 3Sum (medium)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode涔?5. 3Sum (medium)相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%9a%b4%e5%8a%9b%e7%a0%b4%e8%a7%a3' title='鏆村姏鐮磋В'>鏆村姏鐮磋В   list   鐩殑   strong   lis   绱㈠紩   break   .so   姝ラ   

15. 3Sum (medium)

鎻忚堪

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

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

鍒嗘瀽

棣栧厛鏄渶瀹规槗鎯冲埌鐨勬毚鍔涚牬瑙o紝閫氳繃涓夐噸閬嶅巻鏁扮粍nums锛屼緷娆$‘瀹?code>nums[i]锛?code>nums[j]锛?code>nums[k]骞惰绠椾笁鍏冪礌涔嬪拰鏄惁涓?銆傝繖鏄渶绮楁毚鐨勮В娉曪紝浣嗘槸瀛樺湪鍘婚噸鐨勯棶棰橈紝濡俒-1, 0, 1]鍜孾0, 1, -1]杩欑鎯呭喌銆?/p>

鍏舵锛岃繖涓鐩綔涓?Sum鐨勮繘闃堕鐩紝寰堝鏄撹仈鎯冲埌灏?Sum杞寲涓烘眰target鍊间负0 - nums[i]锛屽苟鍦ㄦ暟缁勫墿浣欏厓绱犱腑鎵惧嚭涓や釜鍏冪礌涔嬪拰涓簍arget鐨?Sum闂銆備絾鏄悓鏍峰瓨鍦ㄥ幓閲嶉棶棰樸€?/p>

鍏充簬鍘婚噸锛岀敱浜庢槸涓€涓狶ist

鏈€鍚庯紝浠ヤ笂涓ょ鎬濊矾閮藉瓨鍦ㄥ幓閲嶉棶棰橈紝闂闇€瑕佺殑鏄壘鍑烘暟缁勪腑涓変釜鍏冪礌涔嬪拰涓?鐨勬墍鏈夌粍鍚堛€傚幓閲嶈繃绋嬪緢鏄庢樉鏄拰缁撴灉鏃犲叧锛屼絾鏄嵈闈炲父楹荤儲锛屽洜姝よ浼樺寲绠楁硶灏辫鐫€鐪间簬绉婚櫎鍘婚噸杩欎釜姝ラ銆?/p>

鍦ㄦ毚鍔涚牬瑙g殑鏃跺€欏氨璇ユ剰璇嗗埌杈圭晫闂銆?/p>

鍦ㄥ仛绗竴灞傚惊鐜椂鍙互杩欐牱鍐欙細for(int i = 0; i < nums.length - 2; i++)銆傚嵆绗竴灞傚惊鐜殑缁撴潫鏉′欢鏄?code>nums.length - 2锛屽苟涓嶉渶瑕佸埌nums.length銆?/p>

鍚屾牱绗簩灞傚惊鐜椂锛?code>for(int j = i + 1; j < nums.length - 1; j++)銆傚紑濮嬬储寮曚笉闇€瑕佷粠0寮€濮嬶紝鍙互鐩存帴浠?code>i + 1寮€濮嬶紝鑰岀粨鏉熶负nums.length - 1銆?/p>

绗笁灞傦細for(int k = j + 1; k < nums.length; k++)銆?/p>

鍙互鐪嬪埌鍦ㄦ毚鍔涚牬瑙g殑鏃跺€欙紝鎴戜滑宸茬粡鏈夋剰璇嗗湴閫氳繃杈圭晫鏉′欢杩囨护鎺変竴浜涙儏鍐碉紝杩涜浜嗗垵姝ヤ紭鍖栥€傛敞鎰忓埌鏁扮粍鏈韩鏄棤搴忕殑锛屾墍浠ュ湪纭畾鍏冪礌鐨勬椂鍊欓毦浠ョ晫瀹氬綋鍓嶉亶鍘嗗厓绱犳槸鍚﹀凡缁忚閫変腑杩囥€傚鏋滄暟缁勬槸鏈夊簭鐨勶紝閭d箞涓夐噸閬嶅巻鐨勬椂鍊欏氨鍙互鏈夋剰璇嗗湴璺宠繃閲嶅鍏冪礌銆傚埌杩欓噷宸茬粡瀵规毚鍔涚牬瑙g殑瑙i鎬濊矾杩涜浜嗕紭鍖栵紝浣嗘槸涓夐噸閬嶅巻鏃犵枒鏄鑷存椂闂村鏉傚害涓篛(N^3)锛岃繖涔堥珮鐨勬椂闂村鏉傚害鑲畾鏄琚姏寮冪殑銆傞偅涔堣濡備綍缁х画浼樺寲鍛紵

灏濊瘯浼樺寲鎬濊矾浜屻€傞鍏堜娇鐢ㄦ帓搴忚В鍐冲幓閲嶉棶棰樸€傞亶鍘嗘帓搴忓悗鐨勬暟缁勶紝鍥哄畾绗竴涓厓绱犱负nums[i]锛屾帴涓嬫潵鍦ㄧ储寮曚綅i + 1鑷?code>nums.length涔嬮棿鎵惧嚭涓や釜鍏冪礌nums[j]鍜?code>nums[k]锛屼簩鑰呬箣鍜屼负0 - nums[i]銆傚浐鐒惰繖鍙互鍋氶亶鍘嗕袱娆¤揪鍒扮洰鐨勶紝鐩镐俊鍩烘湰涓?Sum閮芥槸杩欐牱瀹屾垚鐨勩€備絾鏄拡瀵逛竴涓湁搴忔暟缁勶紝澶归€兼硶鍙互灏嗚繖涓繃绋嬬殑鏃堕棿澶嶆潅搴﹂檷涓篛(N)銆傚洜姝わ紝浣跨敤澶归€兼硶鎵惧嚭鍓╀綑涓や釜鍏冪礌銆俻s锛屽埆蹇樹簡鍚屾椂瀵?Sum浣跨敤澶归€兼硶杩涜浼樺寲銆?/p>

浠g爜

public List<List<Integer>> threeSum(int[] nums) {
    List<List<Integer>> result = new LinkedList<List<Integer>>();
    if (nums == null || nums.length < 3) {
        return result;
    }
    // 瀵规暟缁勬帓搴?    Arrays.sort(nums);
    //鍥哄畾绗竴涓厓绱爊ums[i]
    for (int i = 0; i < nums.length - 2; i++) {
        //榛樿鏄粠灏忓埌澶х殑鎺掑簭锛屾墍浠ュ綋nums[i]澶т簬0鐨勬椂鍊欙紝灏卞彲浠ョ粨鏉?        if (nums[i] > 0) {
            break;
        }
        //nums[i - 1] != nums[i]鎵ц浜嗗幓閲嶏紝娉ㄦ剰杩欓噷鍦ㄧ悊瑙g殑鏃跺€欒鎰忚瘑鍒版鏃舵搷浣滅殑鏁扮粍宸茬粡鏄湁搴忔暟缁?        if (i == 0 || nums[i - 1] != nums[i]) {
            //浣跨敤鍔犻€兼硶
            int j = i + 1;
            int k = nums.length - 1;
            while (j < k) {
                int sum = nums[i] + nums[j] + nums[k];
                if (sum == 0) {
                    result.add(Arrays.asList(nums[i], nums[j], nums[k]));
                } 
                if (sum <= 0) {
                    while (j < k && nums[j] == nums[++j]);
                }
                if(sum >= 0){
                    while (j < k && nums[k] == nums[--k]);
                }
            }
        }
    }
    return result;
}

涓婇潰鐨勪唬鐮佹槸浼樺寲涔嬪悗鐨勪唬鐮侊紝瀵逛簬鐞嗚В鍔犻€肩殑杩囩▼鏈夌偣涓嶄究锛屼笅闈㈡槸鍔犻€肩殑鍘熷鍐欐硶锛?/p>

while (j < k) {
    int target = 0 - nums[i];
    if(target == (nums[j] + nums[k])){
        result.add(Arrays.asList(nums[i], nums[j], nums[k]));
        j++;
        while(nums[j] == nums[j - 1] && j < k){ //鍘婚噸锛屾敞鎰忚繖鏄竴涓湁搴忔暟缁?            j++;
        }
        k--;
        while(nums[k] == nums[k + 1] && j < k){ //鍘婚噸锛屾敞鎰忚繖鏄竴涓湁搴忔暟缁?            k--;
        }
    }else if(target < (nums[j] + nums[k])){
        k--;
        while(nums[k] == nums[k + 1] && j < k){ 
            k--;
        }
    }else if(target > (nums[j] + nums[k])){
        j++;
        while(nums[j] == nums[j - 1] && j < k){ 
            j++;
        }
    }  
}

以上是关于Leetcode涔?5. 3Sum (medium)的主要内容,如果未能解决你的问题,请参考以下文章

15. 3Sum - Medium

LeetCode15题: 寻找三数和,附完整代码

Leetcode 259. 3Sum Smaller

[Leetcode] 3Sum

LeetCode(16):3Sum Closest

Leetcode 15. 3Sum