【LeetCode】1. Two Sum(Hash Table)
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].
【題目大意】
在一個整陣列成的字串中找到兩個數,使得兩個數的和為目標引數。
使用Hash表,在表中nums[i]儲存i下標,每次檢測target-nums[i] 有沒有在表中。
【Code】
class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ temp = {} for i in range(len(nums)): if nums[i] not in temp: temp[nums[i]] = i if target-nums[i] in temp.keys() and temp[target-nums[i]]!=i: # 防止再一次檢測到自身 return [temp[target-nums[i]], i]
人生苦短,我用python。
相關推薦
【LeetCode】1. Two Sum(Hash Table)
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
【LeetCode】1. Two Sum(兩數之和)-C++實現的兩種方法
本題是一下公司的面試題: 問題描述: 問題求解: 使用無序容器unorder_map實現: #include <iostream> #include <vector> #include <cassert> #inclu
【LeetCode】1.Two Sum 兩數之和
給定 nums = [2, 7, 11, 15], target = 9 因為 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 解題思路1: 兩個迴圈,輕鬆搞定: for i in range(len(nums)): for j
【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
leetcode 刷題題解(c++) 1.Two Sum (hash表,排序+二分查詢)
題目描述: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return i
【LeetCode】167. Two Sum II - Input array is sorted
etc 想法 ice ted integer turned abs ascend 要求 原題鏈接:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/tabs/description/ 題目要求:Gi
【LeetCode】01 Two Sum與HashMap
給定一個整數陣列和一個目標值,找出陣列中和為目標值的兩個數。 你可以假設每個輸入只對應一種答案,且同樣的元素不能被重複利用。 示例: 給定 nums = [2, 7, 11, 15], target = 9 因為 nums[0] + nums[1] = 2 +
【LeetCode】39. Combination Sum(C++)
地址:https://leetcode.com/problems/combination-sum/ 題目: Given a set of candidate numbers (candidates) (without duplicates) and a target number (
【LeetCode]】add Two Numbers(兩單鏈表對應數值之和)
題目 You are given two linked lists representing two non-negative numbers. The digits are stored in re
【Leetcode】之Two Sum問題
前言:之前是計劃先跟著July的部落格看完《程式設計師程式設計藝術》,然後再去做leetcode的題目。現在改變計劃,直接去做leetcode的題目,每天做一題。 本題跟之前寫的部落格【July程式設計師程式設計藝術】之尋找和為定值的兩個或者多個數是同一個問題
【LeetCode】167. Two Sum II
本題是亞馬遜的面試題。 題目描述: 一、第一種方法-暴力解法 當我們沒有頭緒,想不到更好的方法的時候,我們不妨用比較笨的方法來求解。 使用雙重迴圈: for(int i = 0 ; i < numbers.size() ; i ++)
【LeetCode】443. 壓縮字串(String Compression)
【 英文練習 | 中文練習 】 題目描述: 給定一組字元,使用原地演算法將其壓縮。壓縮後的長度必須始終小於或等於原陣列長度,陣列的每個元素應該是長度為 1 的字元(不是 int 整數型別),在完成原地修改輸入陣列後,返回陣列的新長度。 示例 1: 輸入: ["a","a","
【leetcode】67. Add Binary(Python & C++)
67. Add Binary 題目連結 67.1 題目描述: Given two binary strings, return their sum (also a binary string). For example, a = “11” b
【leetcode】Unique Paths II(動態規劃)
63. Unique Paths II 題目描述 Discuss Pick One Follow up for “Unique Paths”: Now consider if some obstacles are added to the gr
【LeetCode】205 Isomorphic Strings (c++實現)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to gett. A
【python3/c++】leetcode 1. Two Sum (easy)
1. Two Sum (easy) Given an array of integers, return indices of the two numbers such that they add up to a specific target. You m
【LeetCode-數組篇】 1 Two Sum
求的值 ole return 兩個 new pub 提升自己 記憶 pan 1 前言 之所以開始刷 LeetCode 上的算法題,一是快面臨秋招,第二點是因為提升自己的編程能力,在博客上爭取每天刷 5 道左右的算法題,堅持兩個月,希望博友們監督。 這個系列打算用 C#
【leetcode日記】1.Two Sum (c語言)
Description: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may ass
LeetCode 1. Two Sum (兩數之和)
ret desc rip twosum 關鍵點 earch pub ++ num Given an array of integers, return indices of the two numbers such that they add up to a specif
【LeetCode】938. Range Sum of BST 解題報告(Python)
作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 遞迴 日期 題目