leetcode 1
题目:

最开始采用暴力解法,两个for循环遍历所有组合形式,时间复杂度为O(n2),代码省略。
进一步学习,采用hash表存储,空间换时间,时间复杂度为O(n),空间复杂度为O(n);
将数组放入hash表中,利用for循环遍历数字中元素并从hash表中找到对应的数。因为从hash表中取数的时间复杂度为O(1),所以整体的复杂度为O(n);
代码如下:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
int n = nums.size();
vector<int> v;
map<int, int> m;
for(int i = ; i < n; ++ i)
{
if(m.find(target - nums[i]) != m.end())
{
v.push_back(m[target - nums[i]]);
v.push_back(i);
break;
}
m[nums[i]] = i;
}
return v;
}
};
leetcode 1的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- [Python] UTF-8最好不要带BOM
一.问题回顾: 问题: 在写一个脚本读入IP分区表文件到list并做比较的时候,发现该成立的语句总是不成立,经调试后发现开头是这样:\xef\xbb\xbf1.0.3.0,故比较不成功. 解决办法:经 ...
- 读《java核心技术卷一》有感
过去一个多月了吧.才囫囵吞枣地把这书过了一遍.话说这书也够长的,一共706页.我从来不是个喜欢记录的人,一直以来看什么书都是看完了就扔一边去,可能有时候有那么一点想记录下来的冲动,但算算时间太紧,很多 ...
- Npoi导出xlsx
xlsx文件可以超出,excel2003行数限制.必要的dll文件. public void Export() { string filename = Request["searchStri ...
- 查询oracle中所有用户信息
1.查看所有用户:select * from dba_users; select * from all_users; select * from user_users; 2.查看用户或角色系统 ...
- Linux 链接(转载)
来源:http://www.cnblogs.com/sonic4x/archive/2011/08/05/2128543.html 1.Linux链接概念Linux链接分两种,一种被称为硬链接(Har ...
- Yii2.0 实现三级联动 [ 2.0 版本 ]
view中代码 <?php use yii\bootstrap\ActiveForm; /* @var $this yii\web\View */ /* @var $form yii\boots ...
- chkconfig : No such file or directory
sys_version:12.04LTS For example: #chkconfig --level mysql on /sbin/insserv:No such file or director ...
- 怎么查询局域网内全部电脑IP和mac地址..
在局域网内查询在线主机的IP一般比较简单,但局域网内全部电脑的IP怎么才能够查到呢?查询到IP后我还要知道对方的一些详细信息(如MAC地址.电脑名称等)该怎么查询呢??? 工具/原料 Windows ...
- [ActionScript 3.0] AS3 对XML的操作,创建、删除、增加节点方法
package { import flash.display.Sprite; /** * @author:Frost.Yen * @E-mail:871979853@qq.com * @create: ...
- jquery on off 方法
$("p").on("click",function(){alert("The paragraph was clicked.");}); $ ...