twoSum
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的更多相关文章
- JavaScript的two-sum问题解法
一个很常见的问题,找出一个数组中和为给定值的两个数的下标.为了简单一般会注明解只有一个之类的. 最容易想到的方法是循环遍历,这里就不说了. 在JS中比较优雅的方式是利用JS的对象作为hash的方式: ...
- [LeetCode] TwoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [LeetCode_1] twoSum
LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...
- [Lintcode two-sum]两数之和(python,双指针)
题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
- LeetCode——TwoSum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- LeetCode #1 TwoSum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- Leetcode 1——twosum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode — two-sum
package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...
随机推荐
- ubuntu16.04+cuda7.5
0 安装了ubuntu16.04 GT980的显卡,安装了nvidia340的驱动 1 下载cuda7.5的.run文件 2 进入tty1,service stop lightdm 3 sudo sh ...
- 【BZOJ-1096】仓库建设 斜率优化DP
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3719 Solved: 1633[Submit][Stat ...
- 自动完成--autoComplete插件
js下载地址:https://github.com/devbridge/jQuery-Autocomplete 1.引入js,引入css --start------------------------ ...
- POJ 2236 Wireless Network(并查集)
传送门 Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 24513 Accepted ...
- [Android]Volley源码分析(三)
上篇看了关于Request的源码,这篇接着来看下RequestQueue的源码. RequestQueue类图:
- 演示get、post请求如何算sn,算得sn如何使用
import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncoding ...
- zabbix监控系列(3)之zabbix触发器格式配置
前言 今天公司线上环境新添一个监控项,监控 一些日志的是否出现错误,有错误就及时报警,那么问题来了 ,报警必须告诉我们哪个日志有错误,这样才能够快速定位到哪个日志有问题. 配置 zabbix-agen ...
- Notepad++ 开启「切分窗口」同时检视、比对两份文件
Notepad++ 是个相当好用的免费纯文本编辑器,除了内建的功能相当多之外,也支持外挂模块的方式扩充各方面的应用.以前我都用 UltraEdit 跟 Emeditor,后来都改用免费的 Notepa ...
- lunix的查看Tomcat目录下日志的快速操作
可以使用cd命令,cd命令的功能是切换到指定的目录: 命令格式:cd [目录名] 有几个符号作为目录名有特殊的含义: "/"代表根目录. ".."代表上一级目录 ...
- logback 详解
原创文章,转载请指明出处:http://aub.iteye.com/blog/1103685, 尊重他人即尊重自己 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透 ...