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 ...
随机推荐
- 发布在IIS上的Web程序,调用服务器的COM组件
场景大致是这样的,在工厂中分布着许多的PDA点,这些PDA点都要进行实时的扫描--打印操作.实现方法是采用网络打印机,然后服务器安装驱动,管理着所有的打印机.然后服务器,发布一个WebService, ...
- Yii2.0 实现三级联动 [ 2.0 版本 ]
view中代码 <?php use yii\bootstrap\ActiveForm; /* @var $this yii\web\View */ /* @var $form yii\boots ...
- (C) Windows 32 API程序设计目录
(一)第一个窗口程序 01 创建第一个窗口. (二)输出文本 01 获取Windows图形构件大小信息
- 日期选择控件-laydate
laydate控件非常简单易用,只需要调用一个个函数就可以轻松实现日期时间选择. <%@ page language="java" import="java.uti ...
- nyoj 93 汉诺塔(三)
点击打开链接 汉诺塔(三) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝 ...
- SQLServer 索引以及视图
业务 主键: 索引: 锁: ========================================= 一个页分为 页头 页体--存储行 页尾 页是有固定大小的,但是数据不一定存满,有可能产生 ...
- 解决PHP生成校验码时“图像因其本身有错无法显示”的错误
今天使用http://crazymud.iteye.com/blog/452293给出的代码进行PHP生成校验码功能的实现,发现firefox一直提示“图像.......因其本身有错无法显示”的问题, ...
- 【Unity Shaders】学习笔记——SurfaceShader(二)两个结构体和CG类型
[Unity Shaders]学习笔记——SurfaceShader(二)两个结构体和CG类型 转载请注明出处:http://www.cnblogs.com/-867259206/p/5596698. ...
- Java高级软件工程师面试考纲(转)
如果要应聘高级开发工程师职务,仅仅懂得Java的基础知识是远远不够的,还必须懂得常用数据结构.算法.网络.操作系统等知识.因此本文不会讲解具体的技术,笔者综合自己应聘各大公司的经历,整理了一份大公司对 ...
- C#中的委托,匿名方法和Lambda表达式
简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=.答案是6个Firs ...