题目:

最开始采用暴力解法,两个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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. [复变函数]第06堂课 2.1 解析函数的概念与 Cauchy-Riemann 方程 (续)

    2. 解析函数及其简单性质 (1) 定义: a. 若 $w=f(z)$ 在区域 $D$ 内可微, 则称 $f$ 在 $D$ 内解析; b. 若 $w=f(z)$ 在 $z_0$ 处的某邻域内解析, 则 ...

  2. json字符串转json对象的方法

    在使用$.ajax()方法时,我们可以设置dataType:'json'的参数,便可以拿到后台返回的json数据对应的json对象.但有时,我们拿到的是json字符串,需要将它再转成json对象来使用 ...

  3. Date.UTC日期格式

    日期格式 %a: 简短型星期,比如‘Mon’. %A: 完整型星期, 比如‘Monday’. %d: 两位的日期, 从01到31. %e: 数字型日期,从 1 到 31. %b: 简短型月份, 比如  ...

  4. [SQL]把同一字段里的多行数据用一行显示

    declare @t table(id int,num int) insert @t , union all , union all , --select * from @t ----查询 decla ...

  5. [DataTable]C# datatable取最大值最小值

    ArrayList al = new ArrayList(); DataTable dt = new DataTable(); dt.Columns.Add("A",typeof( ...

  6. 【Oracle经典】132个oracle热门精品资料——下载目录

    电子书为网友wglzaj精心整理,这批资料下载量好评率都非常高,广受oracle学习者欢迎.文档共整理了12个精品专题和120个热门资料的下载地址,推荐给大家希望大家喜欢. 目录0豆下载地址:http ...

  7. SQL语句的执行计划(oracle表的三种链接方式)

    SQL语句我们写完之后,就是分析其优化,这就要求我们了解到底数据是怎么存储. 首先我们需要了解,表链接的几种方式 nested loop join sort merge join hash join ...

  8. 在UltraEdit的查找和替换中使用正则表达式 (转)

    很多朋友都用过或者正在用UltraEdit,这个编辑器陪伴我也好几年了,从很多地方影响着我写代码的快捷键习惯,Ultraedit提供了非常丰富的编辑功能,其中非常重要的查找和替换功能一定大家都用过,U ...

  9. LED驱动简单设计

    1.步骤 2.核心代码 #define GPKCON 0X7F008800 #define GPKDAT 0X7F008808 light_led: ldr r0,=GPKCON ldr r1,=0x ...

  10. oracle的sqlldr并行导入表不要加索引

    ORA-26002: Table string has index defined upon it. Cause: Parallel load was specified into a table w ...