leetcode-algorithms-1 two sum
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-1 two sum的更多相关文章
- 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 ...
- 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 ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- leetcode 练习1 two sum
leetcode 练习1 two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
随机推荐
- Awesome Torch
Awesome Torch This blog from: A curated list of awesome Torch tutorials, projects and communities. T ...
- Hadoop【单机安装-测试程序WordCount】
Hadoop程序说明,就是创建一个文本文件,然后统计这个文本文件中单词出现过多少次! (MapReduce 运行在本地 启动JVM ) 第一步 创建需要的文件目录,然后进入该文件中进行编辑 ...
- 所有JTAG集成电路都应该支持菊花链
菊花链 在电气和电子工程中,菊花链是一种布线方案,其中多个设备按顺序或环形连接在一起.相邻设备才能通信.菊花链可用于电源,模拟信号,数字数据或其组合. 但是由于菊花链的串联特性,如果任何一个设备从链路 ...
- 2.在Jenkins中配置及执行 maven + selenium + testng项目
1. 在Jenkins中配置Maven与Git 1)在系统管理>管理插件>可选插件 页面分别下载Git plugin 与 Maven Integration plugin插件,安装完成后再 ...
- 【Python】【有趣的模块】【requests】【二】快速上手
[一]参数及结果 [二]响应内容 >>> r = requests.get('https://github.com/timeline.json') >>> prin ...
- 四: 使用vue搭建网站前端页面
---恢复内容开始--- 在搭建路由项目的时候的基本步骤 一:创建项目 安装好vue 搭好环境 (步骤在上篇博客中) 进入项目目录 cd 目录路径/ 目录名 创建项目 ...
- 快排+java实现
import java.util.Arrays; public class QuickSort { //三数取中法.取出不大不小的那个位置 public static int getPivotPos( ...
- Linux学习3-yum安装java和Tomcat环境
前言 linux上安装软件,可以用yum非常方便,不需要下载解压,一个指令就能用yum安装java和tomcat环境. 前面一篇已经实现在阿里云服务器上搭建一个禅道系统的网站,算是小有成就,但并不是每 ...
- 最长连续子序列 Longest Consecutive Sequence
2018-11-25 16:28:09 问题描述: 问题求解: 方法一.如果不要求是线性时间的话,其实可以很直观的先排序在遍历一遍就可以得到答案,但是这里明确要求是O(n)的时间复杂度,那么就给了一个 ...
- python+opencv 运行环境搭建
1:安装pycharm,验证码你懂的 2:安装python3.5以上,或3.6,python2和3 的版本差异还蛮大 3:安装opencv,如下图 以上是方法一,还有之中方法是下载whl文件再手动安装 ...