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 个人博 ...
随机推荐
- 深度学习课程笔记(十七)Meta-learning (Model Agnostic Meta Learning)
深度学习课程笔记(十七)Meta-learning (Model Agnostic Meta Learning) 2018-08-09 12:21:33 The video tutorial can ...
- Linux(CentOS)上面搭建Nginx环境
总体上来说,Linux 这个系统其实挺好用的 除了看不见界面,但是用起来确实是比Window好用太多了,不废话了,直接说搭建环境的步骤! 安装Nginx 编译运行时的环境 参考博客:http://ww ...
- UI、JS框架----Bootstrap、Metro
Bootstrap Datagrid EasyUI Metro bootstrap Datepicker Editable for Bootstrap:bootstrap-editable.js X- ...
- Long类型转json时前端js丢失精度解决方案
一.问题背景 Java后端开发过程中,尤其是id字段,因数值太大,通过json形式传输到前端后,在js解析时,会丢失精度. 如果对精度丢失没有什么概念,可以看一个知乎的帖子,来感受一下:https:/ ...
- 「BZOJ2153」设计铁路 - 斜率DP
A省有一条东西向的公路经常堵车,为解决这一问题,省政府对此展开了调查. 调查后得知,这条公路两侧有很多村落,每个村落里都住着很多个信仰c教的教徒,每周日都会开着自家的车沿公路到B地去"膜拜& ...
- sql存储过程基本语法
一.定义变量 --简单赋值 declare @a int print @a --使用select语句赋值 declare @user1 nvarchar() select @user1='张三' pr ...
- python 获取进程数据
from multiprocessing import Process, Manager def func(dt, lt): ): key = 'arg' + str(i) dt[key] = i * ...
- 在vscode中,自定义代码片段,例vue组件的模板
1---- 2---- 输入vue, 选 vue.json 3---- 在vue.json中编辑, 有说明 a. tab符,要用空格, 也可以转义 4---- 新建vue文件, 输入自定义 ...
- x1c 2018 莫名卡顿
win10不知更新了什么,x1c非常卡一跳一跳的,很多年没见过了-_-!! CPU占用低,但是特别之卡…… (也许是Lenovo的更新,反正是在window update里一起的 —————————— ...
- Qt基本布局(QLayout)
概述 Qt提供了QHBoxLayout类(水平排列布局),QVBoxLayout类(垂直排列布局),QGridLayout类(网格排列布局)等基本布局管理.它们之间的继承关系如下图 布局中常用的方法有 ...