[array] leetCode-1-Two Sum-Easy
leetCode-1-Two Sum-Easy
descrition
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
example
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
解析
- 方法 1 : 2 重循环去检查两个数的和是否等于 target。时间复杂度-O(n^2),空间复杂度 O(1)
- 方法 2 : 以空间换时间,使用 hash 表存储已访问过的数,实际上是省去了方法 1 中内层循环的查找时间,时间复杂度 O(n),空间复杂度 O(n)
注意:题目的假设,输入保证有且只有一个解;返回的是下标。
code
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
class Solution{
public:
vector<int> twoSum(vector<int>& nums, int target){
return twoSumByMap(nums, target);
}
// time-O(n), space-O(n)
vector<int> twoSumByMap(vector<int>& nums, int target){
vector<int> ans;
unordered_map<int, int> hash; // <num, index>
for(int i=0; i<nums.size(); i++){
int another = target - nums[i];
if(hash.find(another) != hash.end()){
// then complexity of unordered_map.find() is
// average case: constant
// worst case: linear in container size
ans.push_back(hash[another]);
ans.push_back(i);
return ans;
}
hash[nums[i]] = i;
}
return ans;
}
};
int main()
{
freopen("in.txt", "r", stdin);
vector<int> nums;
int target;
int cur;
cin >> target;
while(cin >> cur){
nums.push_back(cur);
}
vector<int> ans = Solution().twoSum(nums, target);
if(!ans.empty())
cout << ans[0] << " " << ans[1] << endl;
else
cout << "no answer" << endl;
fclose(stdin);
return 0;
}
[array] leetCode-1-Two Sum-Easy的更多相关文章
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- 【leetcode】Two Sum (easy)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [leetcode] #112 Path Sum (easy)
原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- LeetCode--Array--Two sum (Easy)
1.Two sum (Easy)# Given an array of integers, return indices of the two numbers such that they add u ...
- LeetCode #303. Range Sum Query
问题: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
随机推荐
- Sql Server 基础语法
来自:http://www.cnblogs.com/AaronYang/archive/2012/04/24/2468093.html Sql Server 基础语法 -- 查看数据表 select ...
- vuejs实现表格分页
http://www.cnblogs.com/landeanfen/p/6054654.html#_label3_8 <html xmlns="http://www.w3.org/19 ...
- zookeeper提供了什么
简单的说,zookeeper=文件系统+通知机制. 每个子目录项如 NameService 都被称作为 znode,和文件系统一样,我们能够自由的增加.删除znode,在一个znode下增加.删除子z ...
- gullo.me 的 natvps
gullo.me 的 natvps 1. 在下面的地址中输入内网 IP,查看许可的网络资源 https://hosting.gullo.me/plugin/support_manager/knowle ...
- thinkphp内置标签简单讲解
thinkphp内置标签简单讲解 1.volist循环 name 需要遍历的数据 id 类似于foreach中 value offset 截取数据起始位置 length 截取数据的个数 mod 奇偶数 ...
- 27.mutex跨进程通信
创建互斥量mutex HANDLE mutex = CreateMutexA(NULL, TRUE, name); 根据id打开mutex HANDLE mutex = OpenMutexA(MUTE ...
- WSDL生成dll
--生成代理类wsdl /l:cs /n:OAWebService /out:D:OAWebService.cs D:\OAWebService.WSDL--生成dllcsc /out:D:OAWeb ...
- golang sync.Mutex(2)
package main import ( "fmt" "sync" "time" ) type User struct { Name st ...
- 笔记四:onsubmit和onclick的区别
今天碰到关于表单提交的问题,我是用submit还是用onclick好呢,然后我去百度了一下两者的区别: onsubmit只能表单上使用,提交表单前会触发, onclick是按钮等控件使用, 用来触发点 ...
- BZOJ2733: [HNOI2012]永无乡(线段树合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...