[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 ...
随机推荐
- 2017国家集训队作业[agc008f]Black Radius
2017国家集训队作业[agc008f]Black Radius 时隔4个月,经历了省赛打酱油和中考各种被吊打后,我终于回想起了我博客园的密码= = 题意: 给你一棵树,树上有若干个关键点.选中某 ...
- vue脚手架3.0的搭建
一.安装node 打开cmd输入node -v查看是否安装成功 显示node版本号表示安装成功,显示‘node’不是内部或外部命令表示未安装node.node安装地址:http://nodejs.cn ...
- Easy mock - 安装配置和基本使用
Easy-mock easy-mock是一款比较好用的接口模拟工具, 使用之前我们需要安装和配置 需要下载的内容有以下 Node Redis MongoDB Node和Redis一路点下一步就行, M ...
- pgrep---以名称为依据从运行进程队列中查找进程
pgrep命令以名称为依据从运行进程队列中查找进程,并显示查找到的进程id.每一个进程ID以一个十进制数表示,通过一个分割字符串和下一个ID分开,默认的分割字符串是一个新行.对于每个属性选项,用户可以 ...
- 03006_DOS操作数据乱码解决
1.我们在dos命令行操作中文时,会报错 insert into sort(sid,sname) values(2,"电视机"); ERROR 1366 (HY000): Inco ...
- 6. oracle学习入门系列之六 模式
oracle学习入门系列之六 模式 上篇咱们学习记录了ORACLE数据库中的数据库结构.内存结构和进程等.篇幅 蛤蟆感觉偏多了.这次要休整下,每次笔记不宜太多,不然与书籍有何差别. 我们要保证的是每次 ...
- bug 7715339 登录失败触发 ‘row cache lock’ 等待
Bug 7715339 - Logon failures causes "row cache lock" waits - Allow disable of logon delay ...
- ios UITextView 提示文本
定义两个UITextView,一个用于输入文本,一个用于显示提示信息,当输入文本为空时显示提示信息,否则不显示提示信息. //6.3.1文字内容提示 _contentTextViewTip = [[U ...
- Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
if (isset($array[2])){ 抛出错误 Cannot use isset() on the result of an expression (you can use "nu ...
- stm32与arm7比较(经典)
http://wenku.baidu.com/link?url=LIVcT1AQL0IgVF1xan5Zy9rXarCBo66hj7OXSxM1ap7FpssO4c3sd1pZd8azfBPr3PBy ...