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

第一次的代码,思路很简单:

#include <iostream>
#include <algorithm>
#include <vector> using namespace std; class Solution{
public:
vector<int> twoSum(vector<int>& nums, int target){
int i = 0;
int j = 0;
vector<int> result; for(i=0; i<nums.size()-1; i++){
for(j=i+1; j<nums.size(); j++){
if(nums[i] + nums[j] == target){
result.push_back(i+1);
result.push_back(j+1);
}
}
}
return result;
}
}; int main(){
vector<int> src;
int input;
int target;
int i =0; vector<int> result; do{
cin>>input;
src.push_back(input);
if(cin.get() == '\n'){
break;
}
}while(1); cin>>target; Solution s;
result = s.twoSum(src, target); for(i=0; i<result.size(); i++){
cout<<result[i]<<endl;
} return 0;
}

最后发现,由于复杂度是O(n^2)系统无法接受,需要降低算法复杂度。。。

现在看到别人用map来解决这个问题

#include <iostream>
#include <algorithm>
#include <vector>
#include <map> using namespace std; class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> result;
map<int, int> m;
if (numbers.size() < 2)
return result;
for (int i = 0; i < numbers.size(); i++)
m[numbers[i]] = i; map<int, int>::iterator it;
for (int i = 0; i < numbers.size(); i++) {
if ((it = m.find(target - numbers[i])) != m.end())
{
if (i == it->second) continue;
result.push_back(i+1);
result.push_back(it->second+1);
return result;
}
}
return result;
}
}; int main(){
vector<int> src;
int input;
int target;
int i =0; vector<int> result; do{
cin>>input;
src.push_back(input);
if(cin.get() == '\n'){
break;
}
}while(1); cin>>target; Solution s;
result = s.twoSum(src, target); for(i=0; i<result.size(); i++){
cout<<result[i]<<endl;
} return 0;
}

twoSum的更多相关文章

  1. JavaScript的two-sum问题解法

    一个很常见的问题,找出一个数组中和为给定值的两个数的下标.为了简单一般会注明解只有一个之类的. 最容易想到的方法是循环遍历,这里就不说了. 在JS中比较优雅的方式是利用JS的对象作为hash的方式: ...

  2. [LeetCode] TwoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  3. [LeetCode_1] twoSum

    LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...

  4. [Lintcode two-sum]两数之和(python,双指针)

    题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...

  5. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

  6. LeetCode——TwoSum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  7. LeetCode #1 TwoSum

    Description Given an array of integers, return indices of the two numbers such that they add up to a ...

  8. Leetcode 1——twosum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  9. leetcode — two-sum

    package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...

随机推荐

  1. 【BZOJ-1046】上升序列 DP + 贪心

    1046: [HAOI2007]上升序列 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3723  Solved: 1271[Submit][Stat ...

  2. JSR303注解

    Annotation 属于Bean Validation 规范 应用位置 作用 对Hibernate Core中的元数据的影响 @AssertFalse yes field/property 检查被标 ...

  3. Ubuntu下root密码认证错误

    1.默认root密码是随机的,即每次开机都有一个新的root密码.我们可以在终端输入命令 sudo passwd root,然后输入当前用户的密码,再输入要设置root的密码,我们一般设置成两个都一样 ...

  4. bios中只有windows boot manager下用U盘启动

    在重装系统的时候,很多时候都是先进入bios设置成U盘启动项,然后进行安装,如果年代久远一点的,就设置成光驱启动,再进行:随着时间的推行,光驱已经开始淘汰了,也怀念以前的光驱装机的时光!-:) 开始进 ...

  5. 整站网页doc下载wget (转)

    -x -np -p -m -k -t -X/upload/ http://网址 为了让这个命令行的各选项意义更加明确,它还可以写成: --force-directories --no-parent - ...

  6. hdu 5229 找规律

    假设选择了字符串a和b: 假设两人都按照最聪明的策略,那么观察一下可以找出规律:当a和b的字符串长度之和为奇数的时候zcc会败. 另外当a==b的时候zcc也会败(当时做的时候忘了这个了T^T) 接下 ...

  7. poj2284 欧拉公式

    题意:给出一图形,求该图形把平面分成了几部分 欧拉公式: http://blog.csdn.net/wangxiaojun911/article/details/4586550 对于二维平面上的情况. ...

  8. 有关Java的日期处理的一些杂记

    在企业应用开发中,经常会遇到日期的相关处理,说实话JDK自带的日期方法很难用.就我个人而言我一般都会采用joda-time来替代JDK自身的日期. 这篇文章是杂记,所以写的比较零散,希望大家不要见怪. ...

  9. iOS - 沙盒中,如何判断存在文件、目录

    在iOS开发中,在沙盒中创建沙盒一些存储各个功能的文件目录或者文件. 使用: [NSFileManager defaultManager] 1.判断目录,用她可以. 2.判断文件,用她可以. 3.创建 ...

  10. JS-Date对象

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>D ...