LeetCode【1】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 your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

注意加粗的exactly,有且仅有一个。因此不必考虑太多。想的太多反而成了累赘。直接double循环是不行的,绝对超时啊。不多说,上代码:

vector<int> twoSum(vector<int> &numbers, int target)
{
    vector<int> index;
    map<int, int> m;
    for(int i = 0; i < numbers.size(); ++ i)
    {
        if(m.find(target - numbers[i]) != m.end())
        {
            index.push_back(m[target - numbers[i]] + 1);
            index.push_back(i + 1);
            break;
        }
        m[numbers[i]] = i;
    }
    return index;
}
时间: 2024-10-18 02:19:17

LeetCode【1】Two Sum的相关文章

【HDOJ】4704 Sum

数学题.f(n) = 2^(n-1) mod (1e9+7). 1 #include <cstdio> 2 3 #define MAXN 100005 4 5 char buf[MAXN]; 6 __int64 phi = 1e9+6; 7 __int64 mod = 1e9+7; 8 9 __int64 power2(__int64 n) { 10 __int64 ret = 1, base = 2; 11 12 --n; 13 while (n) { 14 if (n & 1) 1

【UVA】1210 - Sum of Consecutive Prime Numbers

普通的求区间连续和的问题,一开始以为是区间移动,但是怕UVA数据太严,直接打表,后来发现自己的担心是多余的. 14044972 1210 Sum of Consecutive Prime Numbers Accepted C++ 0.049 2014-08-15 10:30:11 打表的话效率可能不是很高. AC代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm&

【Leetcode】【Easy】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

LeetCode刷题记录【001】Two Sum

1.题目及翻译 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,

【转载】Combination Sum

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

LeetCode【9】. Palindrome Number --java的实现

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra spa

LeetCode【5】. Longest Palindromic Substring --java实现

Longest Palindromic Substring 一.题目如下:        Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 题目要求给定字符串的最大对称子字符串,如"aaabccbac

LeetCode【2】. Add Two Numbers--java实现

第二道题 Add Two Numbers 如下:         You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.     Inpu

LeetCode【8】string to integer(atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe