1. 程式人生 > >leetcode 3Sum. 處理特殊情況+雜湊思想

leetcode 3Sum. 處理特殊情況+雜湊思想

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] ]

和TwoSum總體思想差不多,但是要多一層外迴圈,列舉每一個target的值,target的值為0-nums[i], 我們可以做一些優化在減少遍歷次數:
1.正數直接跳出,因為排序後後面的肯定都是正數
2.和前一個數相同的值可以直接continue

注意幾種特殊情況[-1,0,1,2,-1,-4],排序後為[-4,-1,-1,0,1,2],[-2,0,0,2,2],push的時候判斷下左指標和右指標與上個位置的是否相等,注意這裡的條件是或,如果有一個不相等,且值和為target即可push

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var threeSum = function(nums) {
  let ans = [];
  nums.sort((a,b) => a-b || -1);
  let len = nums.length;
  for(let i = 0; i < len; i++) {
      if(nums[i] > 0)
          break;
      if(i>0 && nums[i]===nums[i-1])
          continue;
      let target = 0 - nums[i];
      let l = i+1,r=len-1;

      while(l<r) {
          if(nums[l]+nums[r]===target && (((l-1>=0)&&nums[l]!==nums[l-1])||(nums[r]!==nums[r+1]&&(r+1<=len))) ) {
              let t = [];
              t.push(nums[i],nums[l],nums[r]);
              ans.push(t);
              l++;
              r--;
          }else if(nums[l]+nums[r]<target) {
              l++;
          }else if(nums[l]+nums[r]>target) {
              r--;
          }else {
              l++;
              r--;
          }
      }
  }
  return ans;
};

console.log(threeSum([-1,0,1,2,-1,-4]));

相關推薦

leetcode 3Sum. 處理特殊情況+思想

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 o

leetcode 1兩數之和以及思想

文章目錄 題目 思想 方法一 暴力法 方法二 雜湊思想 雜湊演算法 1.概念 2.原理 散列表 負載因子 衝突(同義詞)

【一週程式設計學習】--1.用思想實現LeetCode的第1題和第202題

1.LeetCode第一題    兩數之和 以一個數為基準,再用目標數減去基準數得到他們的差值,再在陣列中找這個差值。這個時候以陣列存放的數值為key值,數值對應的陣列下標為value值。利用雜湊查詢演算法查詢相應值的下標。 分別用C++和Pyhton做的:

leetcode 1_TwoSum. 思想

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactl

LeetCode】 705. 706. 設計對映\集合

1.題目 705: 不使用任何內建的雜湊表庫設計一個雜湊集合 具體地說,你的設計應該包含以下的功能 add(value):向雜湊集合中插入一個值。 contains(value) :返回雜湊集合中是否存在這個值。 remove(value):將給定值從雜湊集合中刪除。如果

LeetCode演算法之TwoSum(表 簡單)

首先先給出問題描述, 給定一個整數陣列,返回兩個數字的索引,使它們相加到特定目標。 您可以假設每個輸入只有一個解決方案,並且您可能不會兩次使用相同的元素。 例: 給定nums = [2,7,11,15],target = 9, 因為nums [ 0 ] + nums [ 1 ] =

LeetCode—1—Two Sum(表的使用)

題目 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input wo

LeetCode | 你不得不瞭解的演算法 !

⒈雜湊是什麼 ? 問大家一個問題 。如果手機上儲存了 1000 個聯絡人 ,現在要你給小詹打個電話 ,跟他說 ,他老婆喊他回家吃飯 。你會怎麼做 ? 當然是按姓名搜尋呀 !(假裝你有小詹電話號碼~)言歸正傳 ,那你能想到這和雜湊表有異曲同工之妙嘛 ? 雜湊表簡單說可以理解成一個對映關係

LeetCode】1. Two Sum + 演算法

傳送門:https://leetcode.com/problems/two-sum/#/description 一、題目描述 Given an array of integers, return indices of the two numbers such that they

表的構造方法、衝突處理方法及拉鍊法的簡單程式碼實現

  由於雜湊表的查詢高效性,在平時的演算法中用的也是比較多。例如:字串、單詞個數的統計,只出現一次字元或者數字的統計,兩個集合相同元素的查詢等等,還有插入刪除的高效(鏈地址法)都可以用雜湊表來解決。所以這裡對其做一個小小的總結。缺點可能是需要佔用額外的記憶體空間。 一、雜湊

(PAT 1099) Build A Binary Search Tree(通過思想建立結點與左右子的關係)

099 Build A Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properti

LeetCode oj 383. Ransom Note(


Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
the 
magazines,
 write 
a 
function 
that 
will 
return

leetcode 第一題:使用表解決

題目大意:給定一個整數,試從給定的vector中尋找到兩個成員,使它們的和等於給定的整數。 說明:預設答案具有唯一解,且同一個成員不可以使用兩次。 例子:對於[2,7,11,13],給定整數18,則應當返回成員“7”和“11”在此vector中的位置1和2。 解題思路:  

關於一致性思想的舉一反三

  關於一致性hash演算法的解釋和應用場景,部落格中以已經出現了許多非常優秀的文章解釋,這裡放一個解釋比較清楚的部落格文章如果不太瞭解一致性hash演算法是什麼可以點選瞭解一下,本文主要融合一致性hash演算法思想去簡單解決我們工作中遇到的問題。   在程式設計思想中我們知道要時刻面對變化,而一致性hash

leetcode 49. Group Anagrams【素數相乘處理字串

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate",

Hash(/)表中衝突處理及命中計算

前言   本片部落格主要講的是雜湊表中簡單的衝突處理的方法,以及命中率計算。原理方面基本沒有講解,基本就講個方法,主要用於知識記錄以及幫助一些刷題玩家瀏覽。   簡而言之,不講技術,只講方法。 引言   寫這篇部落格的契機是在刷pat甲級題遇到了一道寫雜湊的題目,結果英文太次被欺負了。之後靠翻譯讀懂題

LeetCode表 hashmap(共88題)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } 【1】Two Sum  【3】Longest Substring Without Repeating Characters  【18】4Sum 

leetcode 706. 設計對映(python)

不使用任何內建的雜湊表庫設計一個雜湊對映 具體地說,你的設計應該包含以下的功能 put(key, value):向雜湊對映中插入(鍵,值)的數值對。如果鍵對應的值已經存在,更新這個值。 get(key):返回給定的鍵所對應的值,如果對映中不包含這個鍵,返回-1。 remov

leetcode 705. 設計集合(python)

不使用任何內建的雜湊表庫設計一個雜湊集合 具體地說,你的設計應該包含以下的功能 add(value):向雜湊集合中插入一個值。 contains(value) :返回雜湊集合中是否存在這個值。 remove(value):將給定值從雜湊集合中刪除。如果雜湊集合中沒

平方探測法處理函式衝突

  平方探測法是一種較好的處理衝突的方法,可以避免出現“堆積”問題,它的缺點是不能探測到散列表上的所有單元,但至少能探測到一半單元。下面通過一個例子來理解:   設Hash函式為 H( key ) = key mod 7,雜湊表的地址空間為0,1,...,10,開始時雜湊表為空,用平方探測法解決衝突,畫出依