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. 兼容MIUI5和MIUI6的开启悬浮窗设置界面

    前一段时间项目中需要对MIUI的悬浮窗开启设置界面进行了引导和跳转,MIUI6中又改变了开启悬浮窗设置的位置,在苦苦寻觅之后,找到了解决的方法,贴出来以方便大家参考和使用. @Override pub ...

  2. 【Django】信号调度

    Django中提供了"信号调度",用于在框架执行操作时解耦. 通俗来讲,就是在某些动作发生时,信号允许特定的发送者去提醒一些接受者. * Django内置信号:** Model s ...

  3. pip 更新安装失败解决方法

    python3 -m ensurepip https://stackoverflow.com/questions/28664082/python-no-module-pip-main-error-wh ...

  4. CTF编程题-三羊献瑞(实验吧)解题随记

    题目如下.解题步骤参考的是https://cloud.tencent.com/developer/news/373865中作者的思路. 1.首先,两个四位数相加等于一个五位数,那么这个五位数的第一位必 ...

  5. 用 runcloud.io 免费部署、优化管理你的多个VPS( 目前支持 Ubuntu 16.04 )

    使用RunCloud.io轻松实现Web部署 使用VPS.云服务器,通常会安装基本的操作系统,之后必须自己安装Apache,MySQL,PHP,尤其是服务器的性能优化,这对大多数人来说可能是非常具有挑 ...

  6. 为RecyclerView添加item的点击事件

    RecyclerView侧重的是布局的灵活性,虽说可以替代ListView但是连基本的点击事件都没有,这篇文章就来详细讲解如何为RecyclerView的item添加点击事件,顺便复习一下观察者模式. ...

  7. F的ACM暑期集训计划

    暑假的知识计划(补充中...) 1.数论相关 (7days) 待完成 多项式同余方程/高次同余方程/欧拉函数/克莱姆法则/高斯消元/莫比乌斯反演/伪素数判定/baby-step-gaint-step ...

  8. 请使劲回答一个关于UNIX/Linux自己主动扩展stack的问题

    有本事就出来,没本事就当鳖! 假设让我回答关于进程栈,线程栈的问题,仅仅要问题不笼统,仅仅要问题明白.我会一五一十地回答,正确率上九成,然而,可悲的是,问题往往他妈的都不是非常明白,因此,游戏到此结束 ...

  9. C# foreach 循环遍历数组

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  10. Spring MVC基础了解

    参考网址:https://www.yiibai.com/spring_mvc/springmvc_overview.html Spring框架相关 Spring Security 一个灵活强大的身份验 ...