leetcode-1-two sum
一個很好的leetcode題解部落格:點選開啟連結
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].
思路:遍歷每個數得到target-數之後的結果看有沒有對應,如果有的話返回其索引。所以想到建立數與下標之間的索引。空間換時間。
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> m; //建立索引表 for(int i=0;i<nums.size();i++){ m[nums[i]]=i; } vector<int > ret(2); for(int i=0;i<nums.size();i++){ int now = target-nums[i]; if(m[now]){ ret[0]=i; ret[1]=m[now]; break; } } return ret; } };
相關推薦
LeetCode #1 Two Sum
問題 code 後來 如果 默認 key-value 行存儲 方式 另一個 Given an array of integers, return indices of the two numbers such that they add up to a specific t
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 1. Two Sum
size_t 反向 back 數組 rst 如果 end 分享 solution class Solution { public: vector<int> twoSum(vector<int>& nums, int target)
[LeetCode] 1 Two Sum
.cn des www. 時間復雜度 pre ret tco ++ rip 原題地址: https://leetcode.com/problems/two-sum/description/ 題目: Given an array of integers, return i
[leetcode] 1. Two Sum
example solution cau 簡單 應該 pub ecif span code Given an array of integers, return indices of the two numbers such that they add up to a sp
Leetcode -- 1. two sum
ice sum because integer pub class sta 索引 use Given an array of integers, return indices of the two numbers such that they add up to a spe
Leetcode:1-Two Sum
n) 一個 indices code dice ict cau nbsp hat Given an array of integers, return indices of the two numbers such that they add up to a specifi
LeetCode 1. Two Sum
所有 specific assume alloc hat put integer have each Given an array of integers, return indices of the two numbers such that they add up to
Leetcode 1. Two Sum (Easy)
[1] get add each cte ger ber amp desc Description Given an array of integers, return indices of the two numbers such that they add up to
leetcode 1. Two Sum(兩數之和)
解題方案 思路 1 ******- 時間複雜度: O(N^2)******- 空間複雜度: O(1)****** 暴力解法,兩輪遍歷 beats 27.6% class Solution(object): def twoSum(self, nums, targe
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 1. Two Sum 兩數之和
LeetCode 1. Two Sum 兩數之和 標籤: Leetcode 題目描述 給定一個整數陣列 nums 和一個目標值 target,請你在該陣列中找出和為目標值的兩個整數。 你可以假設每種輸入只會對應一個答案。但是,你不能重複利用這個陣列中同樣的元素。
leetcode 1-Two Sum python
one-pass Hash Table class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type tar
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
Leetcode[1]Two Sum Python
Two Sum Go to Discuss Given an array of integers, return indices of the two numbers such that they add up to a specific target. You m
【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 兩數之和
題目: 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 i
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 i
【LeetCode easy專題】leetcode 1 Two Sum
題目描述如下: 此題目比較簡單的一點是給定的測試用例中,一定會有兩個數,使得這個數的和等於給定的target。 常用的思路是 利用兩個for迴圈遍歷陣列,程式碼如下: class Solution { public: vecto
leetcode: 1. Two Sum
Difficulty Easy Discription /** * Given an array of integers, return indices of the two numbers such that they add up to a specific target.