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. Graph Convolutional Networks (GCNs) 简介

    Graph Convolutional Networks 2018-01-16  19:35:17 this Tutorial comes from YouTube Video:https://www ...

  2. 【C#】C#学习笔记_1

    C#的程序入口为某一个类里面的static void Main(string[] args){}方法,如果一个工程有多个Main方法,那么需要在工程配置中选择一个作为程序入口. C#的输入.输出操作在 ...

  3. Twitter OAuth

    新浪微博和 Twitter 的 Oauth API 为什么感觉流程不一样 新浪: 开发者引导用户到新浪授权页面, 页面链接中需要带上自己的 apikey : 用户授权后新浪跳转到开发者指定指定的页面, ...

  4. python学习 day07打卡 文件操作

    本节主要内容: 初识文件操作 只读(r,rb) 只读(w,wb) 追加(a,ab) r+读写 w+写读 a+追加写读 其他操作方法 文件的修改以及另一种打开文件句柄的方法 一. 初识文件操作 使用py ...

  5. sprinf sprintf_s 的用法

    函数功能: 将数据格式化输出到字符串 函数原型: int sprintf( char *buffer, const char *format [,argument] ... ) 注意这里的buffer ...

  6. RN返回navigation方法

    RN官方指定的路由管理是navigation  通过打印我们可以得到navgation的相关属性 1:dispatch ,Redux的事件发起 2:goback()返回 3:navigate(rout ...

  7. bzoj 2243: [SDOI2011]染色 线段树区间合并+树链剖分

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 7925  Solved: 2975[Submit][Status ...

  8. AjaxHandler

    概要 AjaxHandler组件是在ASP.NET MVC Web应用程序中实现ajax功能的一系列扩展方法,该组件的最初的实现方法借鉴了网上流行的部分源代码, ,经过博主不断完善和改进后推出的比较成 ...

  9. java反编译器

    一时手残,把java工程中的源文件给删了,幸亏还有.class文件,想起java可以反编译,所以试一试. JD-Eclipse 如果是使用Eclipse的话,可以用Eclipse插件JadClipse ...

  10. python获取子进程的返回值

    用subprocess popen 的communicate   比如,用gdalinfo命令查看一个tiff文件的信息 x = subprocess.Popen(["gdalinfo&qu ...