LeetCode 1、Two Sum
2017-02-25 21:21
344 查看
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:
https://leetcode.com/problems/two-sum/?tab=Description
Hash。
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].
https://leetcode.com/problems/two-sum/?tab=Description
Hash。
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> result; map<int, int> hash; for (int i = 0; i < nums.size(); ++i) { if (hash.count(nums[i]) != 0) { result.push_back(hash[nums[i]]); result.push_back(i); break; } hash[target - nums[i]] = i; } return result; } };
相关文章推荐
- LeetCode 371(Sum of Two Integers)
- leetcode 167: Two Sum II - Input array is sorted
- LeetCode-Two Sum III - Data structure design
- leetcode:数组之Two Sum
- leetcode twoSum
- leetcode解题报告(22):Two Sum II - Input array is sorted
- LeetCode修仙:Two Sum详解
- [LeetCode] Two Sum II - Input array is sorted
- Leetcode_Two Sum II - Input array is sorted
- Leetcode Two Sum
- leetcode[Two Sum IV - Input is a BST]//待整理多种解法
- LeetCode之Two Sum II - Input array is sorted
- Leetcode 371 :Sum of Two Integers
- LeetCode- 1. Two Sum - 思路详解-C++
- LeetCode (1)Two Sum | LeetCode
- [LeetCode]题解(python):001-Two-Sum
- LeetCode 653: Two Sum IV - Input is a BST
- LeetCode_1_TwoSum(Java Code)
- [LeetCode]TwoSum解题报告
- leetcode 167. Two Sum II - Input array is sorted python