[LintCode] 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.
Notice
You may assume that each input would have exactly one solution
numbers=[2, 7, 11, 15]
, target=9
return [1, 2]
Either of the following solutions are acceptable:
- O(n) Space, O(nlogn) Time
- O(n) Space, O(n) Time
LeetCode上的原题,请参见我之前的博客Two Sum。
class Solution {
public:
/*
* @param numbers : An array of Integer
* @param target : target = numbers[index1] + numbers[index2]
* @return : [index1+1, index2+1] (index1 < index2)
*/
vector<int> twoSum(vector<int> &nums, int target) {
unordered_map<int, int> m;
for (int i = ; i < nums.size(); ++i) m[nums[i]] = i;
for (int i = ; i < nums.size(); ++i) {
int t = target - nums[i];
if (m.count(t) && m[t] != i) {
return {i + , m[t] + };
}
}
return {};
}
};
[LintCode] Two Sum 两数之和的更多相关文章
- LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现
1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...
- 【LeetCode】1. Two Sum 两数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- [LeetCode] Two Sum 两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)
题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- [LeetCode]1.Two Sum 两数之和&&第一次刷题感想
---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...
- [leetcode]1. Two Sum两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific t ...
- 【LeetCode】Two Sum(两数之和)
这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...
- [LeetCode]1.Two Sum 两数之和(Java)
原题地址:two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每 ...
随机推荐
- 传引用 C(转)
转自:http://myturn.blog.hexun.com/15584978_d.html #include <iostream> using namespace std ; void ...
- ORB特征点检测
Oriented FAST and Rotated BRIEF www.cnblogs.com/ronny 这篇文章我们将介绍一种新的具有局部不变性的特征 -- ORB特征,从它的名字中可以看出它 ...
- Codeforces Testing Round #12 C. Subsequences 树状数组
C. Subsequences For the given sequence with n different elements find the number of increasing s ...
- python图像卷积
import cv2import numpy as np #filier 2Dsavepath = "E:\\"image = cv2.imread('E:\\me.jpg');c ...
- AndroidTips:selector的disable状态为什么无效?
正确的姿势: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android=& ...
- css自适应代码-iphone端
@media (device-height:480px) and (-webkit-min-device-pixel-ratio:2) {/* 兼容iphone4/4s */} @media (dev ...
- rman归档删除
rman: delete [all] input 数据库oracle 11g 全备脚本如下:rman target / <<EOFrun {allocate channel t1 typ ...
- 非传统题【A002】
[A002]非传统题[难度A]————————————————————————————————————————————————————————————————————————————————————— ...
- http://blog.sina.com.cn/s/blog_5bd6b4510101585x.html
http://blog.sina.com.cn/s/blog_5bd6b4510101585x.html
- 餐厅系统app7
团队贡献分 杨子健:23 郭志豪:24 谭宇森:22 刘森松:31