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的更多相关文章

  1. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  2. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  3. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  4. 【leetcode】Two Sum (easy)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  5. [leetcode] #112 Path Sum (easy)

    原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...

  6. [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 ...

  7. [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 ...

  8. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  9. 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 ...

  10. LeetCode #303. Range Sum Query

    问题: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

随机推荐

  1. Checkpoint & cache & persist

    checkpoint checkpoint(检查点)是Spark为了避免长链路,大计算量的Rdd不可用时,需要长时间恢复而引入的.主要就是将通过大量计算而获得的这类Rdd的数据直接持久化到外部可靠的存 ...

  2. BZOJ4652: [Noi2016]循环之美(莫比乌斯反演,杜教筛)

    Description 牛牛是一个热爱算法设计的高中生.在他设计的算法中,常常会使用带小数的数进行计算.牛牛认为,如果在 k  进制下,一个数的小数部分是纯循环的,那么它就是美的.现在,牛牛想知道:对 ...

  3. LAMP+YII框架配置中遇到的问题

    以下列出了不同问题及答案: 1. 在yii框架中,改动数据库信息,主要有main.php和database.php两个文件. 2. 问题: watermark/2/text/aHR0cDovL2Jsb ...

  4. 内存卡(TF或其它)的标准

    内存卡(TF或其它)的标准 来源 https://zh.wikipedia.org/wiki/SDXC 一般的内存卡是SD2.0的规范的,就是 Class10(10M/sec)或低于 Class10 ...

  5. Android 多线程断点续传同时下载多个大文件

    最近学习在Android环境中一些网络请求方面的知识,其中有一部分是关于网络下载方面的知识.在这里解析一下自己写的demo,总结一下自己所学的知识.下图为demo的效果图,仿照一些应用下载商城在Lis ...

  6. 计算两个String 类型的时间相关几个月

    /** * 返回两个时间段相隔几个月 * @param date1 * @param date2 * @return * @throws ParseException * @throws ParseE ...

  7. 记一次搬迁到 OpenShift 并搭建 PHP5.5 环境等

    http://blog.laobubu.net/archives/move-to-openshift/ 记一次搬迁到 OpenShift 并搭建 PHP5.5 环境等 Nov 24, 2014 十一月 ...

  8. 【前端图表】echarts实现散点图x轴时间轴

    话不多说,老规矩,先上图,实现echarts实现散点图,x轴数据为时间年月日. 实现代码如下: <!DOCTYPE html> <html> <head> < ...

  9. MySQL主从复制之Mycat简单配置和高可用

    什么是Mycat 1.Mycat就是MySQL Server,而Mycat后面连接的MySQL Server,就好象是MySQL的存储引擎,如InnoDB,MyISAM等.因此,Mycat本身并不存储 ...

  10. Java对ad操作

    转载:http://blog.csdn.net/binyao02123202/article/details/18697953