LintCode: Two Sum

C++

hash map

把查找2个数的过程转换为查找1个数

借用STL容器 unordered_map

 1 class Solution {
 2 public:
 3     /*
 4      * @param numbers : An array of Integer
 5      * @param target : target = numbers[index1] + numbers[index2]
 6      * @return : [index1+1, index2+1] (index1 < index2)
 7      */
 8     vector<int> twoSum(vector<int> &nums, int target) {
 9         // write your code here
10         vector<int> result;
11         int n = nums.size();
12         if (0 == n) {
13             return result;
14         }
15         // first value, second index
16         unordered_map<int, int> hash(n);
17         for (int i = 0; i < n; i++) {
18             if (hash.find(target - nums[i]) != hash.end()) {
19                 result.push_back(hash[target - nums[i]]);
20                 result.push_back(i + 1);
21                 return result;
22             } else {
23                 hash[nums[i]] = i + 1;
24             }
25         }
26     }
27 };
时间: 2024-10-27 22:24:57

LintCode: Two Sum的相关文章

lintcode: k Sum 解题报告

k SumShow Result My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers where sum is target. Calculate how many solutions there are? Ex

[LintCode] Submatrix Sum 子矩阵之和

Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number. Have you met this question in a real interview? Yes Example Given matrix [ [1 ,5 ,7], [3 ,7 ,-8],

Lintcode: Interval Sum II

Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value): For query(start, end), return the sum from index start to index end in the given array. For modify(index, value), modify the number in t

Lintcode: Interval Sum

Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the sum number between index start and end in the given array, return the result l

[LintCode] Subarray Sum II

Given an integer array, find a subarray where the sum of numbers is in a given interval. Your code should return the number of possible answers. (The element in the array should be positive) Example Given [1,2,3,4] and interval = [1,3], return 4. Bra

[LintCode] Two Sum 两数之和

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

LintCode &quot;Submatrix Sum&quot;

Naive solution is O(n^4). But on 1 certain dimension, naive O(n^2) can be O(n) by this well-known equation: sum[i..j] = sum[0..j] - sum[0..i]. And pls take care of several corner cases. class Solution { public: /** * @param matrix an integer matrix *

Lintcode: k Sum II

Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where their sum is target. Example Given [1,2,3,4], k=2, target=5, [1,4] and [2,3] are possible solutions. 这道题同Combination Sum II 1 public class Solution { 2 /** 3

[LintCode] Two Sum - Data Structure Design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. Example add(1); add(3);

LintCode: Combination Sum II

C++ DFS 1 class Solution { 2 public: 3 void help(vector<int> &a, int now, int sum, int target, vector<int> &path, vector<vector<int> > &ans, bool last) { 4 if (sum > target) { 5 return ; 6 } 7 if (now >= a.size())