1. 程式人生 > >leetcode 1_TwoSum. 雜湊思想

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 exactly one solution, and
you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].

leetcode的oj就是比X客的牛逼,console.log()沒有註釋,照樣ac。
本來是easy的題目,最近看灣區那邊的面經老是看到nSum的字樣,索性就把這個系列的都做一遍吧。

思路就是儲存下index,排序,然後兩邊往中間遍歷(這個思路是之前做過哪道題來著)

/**                                                              
 * @param {number[]} nums                                        
 * @param {number} target                                        
 * @return {number[]}  
 * 其他解題思路:http://www.cnblogs.com/grandyang/p/4130379.htmls                                          
 */                                                              
var twoSum = function(nums, target) {                            
                                                                 
  let newo = nums.map(function(item,index) {                   
    let t = {};                                                
    // console.log(item,index);                                
    t.index = index;                                           
    t.value = item;                                            
    // console.log(newo);                                      
    return t;                                                  
  });                                                          
  //console.log(newo);                                           
  newo.sort((a ,b) => a.value-b.value || -1);                  
  //console.log(newo);                                           
//   console.log(nums);                                        
  let len = newo.length;                                       
  //console.log(len);                                            
  let i = 0,j=len-1;                                           
  let ans = [];                                                
  while(i<j) {                                                 
                                                               
    if(newo[i].value+newo[j].value=== target) {                
      ans.push(newo[i].index,newo[j].index);                   
      i++;                                                     
      j--;                                                     
    }else if(newo[i].value+newo[j].value>target) {             
      j--;                                                     
  }else if(newo[i].value+newo[j].value<target) {               
      i++;                                                     
  }                                                            
                                                               
  // console.log(nums);                                        
}                                                              
  return ans;                                                    
}                                                                

console.log(twoSum([3, 2, 4], 6));;

相關推薦

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 1兩數之和以及思想

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

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

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

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

(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】#表【Python】138. Copy List with Random Pointer 複製帶隨機指標的連結串列

連結: 題目: 給定一個連結串列,每個節點包含一個額外增加的隨機指標,該指標可以指向連結串列中的任何節點或空節點。要求返回這個連結串列的深度拷貝。 解法1:先迴圈一遍,把node建完,把所有的no

LeetCode表 hash_table(共88題)

【1】Two Sum (2018年11月9日,k-sum專題,演算法群衍生題) 給了一個數組 nums, 和一個 target 數字,要求返回一個下標的 pair, 使得這兩個元素相加等於 target 。 題解:我這次最大範圍的優化程式碼, hash-table + one pass,時間複雜度 O(N)

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

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

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

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

leetcode 706. 設計對映(python)

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

leetcode 705. 設計集合(python)

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

LeetCode演算法之TwoSum(表 簡單)

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

leetcode 771. 寶石與石頭【Easy】【表】

題目: 給定字串J 代表石頭中寶石的型別,和字串 S代表你擁有的石頭。 S 中每個字元代表了一種你擁有的石頭的型別,你想知道你擁有的石頭中有多少是寶石。 J 中的字母不重複,J 和 S中的所有字元都是字母。字母區分大小寫,

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

[資料結構][C++] 查詢和排序(表儲存基本思想

雜湊表類概念摘要 雜湊表類SqHash的建立、查詢。設有若干個學生的考試成績,採用除留餘數求雜湊地址,將學生的資訊儲存到該地址空間,並且採用線性探測法解決衝突問題。 雜湊表又稱散列表。 雜湊表儲存的基本思想是:以資料表中的每個記錄的關鍵字 k為自變數,通過一種函式H(

leetcode筆記-資料結構-

同構字串 一對字串 字母間需要唯一的對映關係,使用雜湊表可以找到key-value對映關係 從s到t 和 t到s 都需要唯一對映 需要用一個表儲存s到t的對映,另一個表儲存t中已經被對映過的字母 def isIsomorphic(self, s, t):

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

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