leetcode-algorithms-1 two sum

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

直接对数组进行两个循环匹配,相等返回.

class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> t;
for (int one = 0; one < nums.size() - 1; ++one)
{
for (int two = one + 1; two < nums.size(); ++two)
{
if (nums[one] + nums[two] == target)
{
t.push_back(one);
t.push_back(two);
return t;
}
}
}
return t;
}
};

时间复杂度: 两个循环都是n个元素遍历,即O($n^2$).

空间复杂度: O(1).

解法2

class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> t;
std::unordered_map<int,int> m;
for (int one = 0; one < nums.size(); ++one)
{
int result = target - nums[one];
auto fiter = m.find(result);
if (fiter != m.end())
{
t.push_back(fiter->second);
t.push_back(one);
return t;
}
m[nums[one]] = one;
}
return t;
}
};

时间复杂度: 一个循环加上一个查找,由于unordered_map是hash实现,其查找效率O(1),所以最后的复杂度O(n).

空间复杂度: O(n).

链接: leetcode-algorithms 目录

leetcode-algorithms-1 two sum的更多相关文章

  1. LeetCode 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  2. LeetCode 64. Minimum Path Sum(最小和的路径)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  3. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  4. leetcode 练习1 two sum

    leetcode 练习1  two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...

  5. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  6. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  8. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  9. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  10. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

随机推荐

  1. 1.2成员变量+类变量+static关键字

    成员变量和类变量的区别 由static修饰的变量称为静态变量,其实质上就是一个全局变量.如果某个内容是被所有对象所共享,那么该内容就应该用静态修饰:没有被静态修饰的内容,其实是属于对象的特殊描述. 不 ...

  2. it做形式主语的句子

    1. it was considerate of you to visit my mother every day and (to) bring me your notes to help me wi ...

  3. GPIO实验之c语言

    上一章节进行实验使用的是汇编进行编程的,本次实验是使用c语言进行编写的. (1)点亮一个led灯   1)启动文件:    crt.S   .text   .global _start   _star ...

  4. WEB 安全学习 一、mysql 注入漏洞

    转载: https://www.cnblogs.com/cui0x01/p/8620524.html 一.Mysql数据库结构 数据库A 表名 列名 数据 数据库B 表名 列名 数据 Mysql5.0 ...

  5. HDU 5782 Cycle(KMP+哈希)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5782 题意:给出两个长度相等的字符串,输出两个字符的每个前缀是否循环相同. 思路: 如果连个串循环相 ...

  6. 键盘控制div移动并且解决停顿问题(原生js)

    <html> <head> <title>键盘控制div移动,解决停顿问题</title> <meta charset="utf-8&q ...

  7. React native 之设置IOS的图标,名称和启动图(下篇文章会讲到RN的android的相关设置)

    1.首先,app的名称: 如图所示:我的工程名叫BOOk 在BOOk下面的info.plist的文件里设置app的相关信息:比如Bundle name就是设置APP的名称 2.App的图标:(这里注意 ...

  8. 项目Alpha冲刺--4/10

    项目Alpha冲刺--4/10 1.团队信息 团队名称:基于云的胜利冲锋队 成员信息 队员学号 队员姓名 个人博客地址 备注 221500201 孙文慈 https://www.cnblogs.com ...

  9. 报名 | 蚂蚁金服ATEC科技大会 · 上海:数字金融新原力

    小蚂蚁说: 2019年1月4日,蚂蚁金服ATEC城市峰会将以“数字金融新原力(The New Force of Digital Finance)”为主题,在中国上海举办.蚂蚁金服ATEC(Ant Te ...

  10. 动态LINQ(Lambda表达式)

    1.准备数据实体 public class Data { public string AccountNO { get; set; } public int Count { get; set; } } ...