【leetcode】Two Sum (easy)
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
跟以前做过的都差不多,就是要定位下标。AC代码O(nlogn)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> copyNumbers = numbers;
vector<int> AddNum;
vector<int> Result;
sort(copyNumbers.begin(), copyNumbers.end()); //升序排序
//确定两个加数
for(int i = , j = copyNumbers.size() - ; i < j; )
{
if(copyNumbers[i] + copyNumbers[j] == target)
{
AddNum.push_back(copyNumbers[i]);
AddNum.push_back(copyNumbers[j]);
break;
}
else if(copyNumbers[i] + copyNumbers[j] < target)
{
i++;
}
else
{
j--;
}
}
//定位两个加数
for(int i = ; i < numbers.size(); i++)
{
if(numbers[i] == AddNum[] || numbers[i] == AddNum[])
{
Result.push_back(i + );
}
} return Result;
}
}; int main()
{
Solution sol;
vector<int> vec;
vec.push_back();
vec.push_back();
vec.push_back();
vec.push_back(); vector<int> ans = sol.twoSum(vec, ); return ;
}
看答案,发现有O(n)的解法。用hash表,从头到尾判断 target-当前值 在vector中是否存在O(1),因为用了hash.
O(n) runtime, O(n) space – Hash table:
We could reduce the runtime complexity of looking up a value to O(1) using a hash map that maps a value to its index.
public int[] twoSum(int[] numbers, int target)
{
Map<Integer, Integer> map = new HashMap<>();
for (int i = ; i < numbers.length; i++) {
int x = numbers[i];
if (map.containsKey(target - x)) {
return new int[] { map.get(target - x) + , i + };
}
map.put(x, i);
}
throw new IllegalArgumentException("No two sum solution");
}
【leetcode】Two Sum (easy)的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【leetcode】Path Sum IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】Path Sum(路径总和)
这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...
- 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)
这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...
随机推荐
- Access应用笔记<二>
关于access的应用笔记 20140822 基本完成access数据库的搭建,并且尝试了查重,不匹配项目查找,以及上传新数据等功能,表现良好. 记录一下目前研究出来的sql语句: 1)去除重复项 S ...
- 编译CDH Spark源代码
如何编译CDH Spark源代码 经过漫长的编译过程(我编译了2个半小时),最终成功了,在assembly/target/scala-2.10目录下面有spark-assembly-1.0.0-cdh ...
- Mac 下用IDEA时maven,ant打包 (mr 入库hbase)
现在非常喜欢IDEA,之前在mac 上用的eclipse 经常出现无缘无故的错误.所以转为IDEA. 不过新工具需要学习成本,手头上的项目就遇到了很多问题,现列举如下: 背景描述 在hadoop 开 ...
- ICloud没有密码怎么注销?
忘了密码的用这方法就可以啦1.现在我说你做 . 先进入 iCloud 点击 某某账户(要删的账户)2.第二栏密码那里删除原来的再输入ios(任意密码)3.然后点完成 之后会出现错误.请点 好.然后左上 ...
- Ubuntu 14 修改默认打开方式
通过研究,有三种修改方式. 方式一: 修改路径:右上角“系统设置” -> 详细信息 -> 默认应用程序 但是,有个缺陷,可修改的项比较少. 方式二: 例如,修改pdf的打开方式,只要查看任 ...
- Mac Pro 解压安装MySQL二进制分发版 mysql-5.6.30-osx10.11-x86_64.tar.gz(不是dmg的)
没有mac的root密码,当前用户有sudo权限,所以想以root身份执行的命令都加了sudo. 是否存在 _mysql 用户和用户组,并查看用户 _mysql 是不是用户组 _mysql 的成员. ...
- java之google style
Google的Java编码规范英文版: http://google-styleguide.googlecode.com/svn/trunk/javaguide.html Google的Java编码规范 ...
- MySQL Python教程(3)
Class cursor.MySQLCursor 具体方法和属性如下:Constructor cursor.MySQLCursorMethod MySQLCursor.callproc(procnam ...
- css3常用标签
30个最常用css选择器解析 你也许已经掌握了id.class.后台选择器这些基本的css选择器.但这远远不是css的全部.下面向大家系统的解析css中30个最常用的选择器,包括我们最头痛的浏览器 ...
- cocos2d事件处理机制之我见
cocos2d是使用pyglet事件框架来处理事件的. 其中,包括分发器(发射器)和监听器两部分.下面形象的来打个比方. 这个机制就好比一把枪,三步:扣扳机(触发),上弹(注册),给子弹上***(这个 ...