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 ...
随机推荐
- 华硕笔记本之secure boot
在ubuntu下安装cuda的时候,一直装不好,cuda-7.5.run已经装好了,但是编译cuda的例程时失败,提示cuda的库链接不上. 初步判断是secure boot的问题,因为在开启X的情况 ...
- Android成长日记-ListView
数据适配器:把复杂的数据(数组,链表,数据库,集合等)填充在指定的视图界面上 适配器的类型: ① ArrayAdapter(数组适配器):用于绑定格式单一的数据 数据源:可以是集合或数组 ① Simp ...
- ZOJ2314 Reactor Cooling
Reactor Cooling Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge The terrorist g ...
- MVC5-5 Razor引擎及视图结构
View结构 其实给我们提供了官方的MvcDemo,就是在我们直接去新建一个不为空的MVC项目. 这里就是一个MVC的Demo了,可以看一下这个Demo中View的结构是什么 上图可以发现,有一个Sh ...
- Linux 内核高-低端内存设置代码跟踪(ARM构架)
对于ARM中内核如何在启动的时候设置高低端内存的分界线(也是逻辑地址与虚拟地址分界线(虚拟地址)减去那个固定的偏移),这里我稍微引导下(内核分析使用Linux-3.0): 首先定位设置内核虚拟地址起始 ...
- css编写规范
一.注释规范 1.文件顶部注释(推荐使用) /* * @description: 中文说明 * @author: name * @update: name (2013-04-13 18:32) */ ...
- javascript 代码可读性
可读性的大部分内容都是和代码缩进相关的,必须保证代码有良好的格式.可读性的另一方面就是注释,一般而言,有如下一些地方需要进行注释 1.1.1 函数和方法 每个函数或方法都应该包含一个注释,描述其目的和 ...
- 设计模式-14 MVC模式
一 MVC设计模式 MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式,它是一个存在于服务器 表达层的模型,它将应用分开,改变应用之间的高度耦合 MVC设计模式将 ...
- Android Studio MultiDex 分包碰到的坑
前天准备发包了,测试完毕,打好正式签名包,装到手机上,运行不起来. 网上查了大量资料,都没有解决方案. log显示如下: 04-26 10:07:57.727 1538-1538/? I/MultiD ...
- properties 配置文件中值换行的问题
在使用properties配置文件的时候我们经常碰到如下两个问题 1:当a=b中的b值内容特别长的时候为了阅读方便我们手动换行,但如果我们直接回车那么后面的数据就会丢失.那如何解决呢? 例如: a=a ...