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)的主要内容,如果未能解决你的问题,请参考以下文章