题目:

最开始采用暴力解法,两个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. ERWin & ERStudio图里的实线和虚线的含义[转]

    注: ERWin 与 ERStudio 中这一点的描述方法是一样的. ERWin里面线代表实体间的三种关系:决定关系(Identifying Relationship),非决定关系(None-Iden ...

  2. java位运算

    Java的位运算(bitwise operators)直接对整数类型的位进行操作,这些整数类型包括long.int.short.char和 byte,位运算符具体如下表: 运算符 说明 << ...

  3. 异步task处理

    public async Task<Customers> GetCustomers() { return await Service.GetCustomersAsync(); } publ ...

  4. C++学习4

    在C++中,定义函数时可以给参数指定一个默认的初始值.调用函数时,可以省略有默认值的参数.也就是说,如果用户指定了参数的值,那么就使用用户指定的值,否则使用参数的默认值. C++规定,默认参数只能放在 ...

  5. [ActionScript 3.0] 通过三角形获得 3D 效果

    Flash Player 10 和更高版本,Adobe AIR 1.5 和更高版本 在 ActionScript 中,使用Graphics.drawTriangles()方法执行位图转换,因为 3D ...

  6. [ActionScript 3.0] as3.0加载as2.0的swf时获取as2.0的实际舞台尺寸

    var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler) ...

  7. relative和absolute的效果

    我对这样几个效果不是特别理解: 1.float的效果: 就是搞不清楚我想要什么效果的时候可以将某个标签设置为float,一直没总结出什么规律. 2.relative和absolute的效果: 也是不清 ...

  8. go 使用模板函数的例子

    代码: package main import (     "bytes"     "fmt"     "text/template"    ...

  9. 无法重新组织表 "ty_wf_ex_local_process_info" 的索引 "idx_prc_act_id" (分区 1),因为已禁用页级锁定。

    无法重新组织表 "ty_wf_ex_local_process_info" 的索引 "idx_prc_act_id" (分区 1),因为已禁用页级锁定. ALT ...

  10. 为什么需要auto_ptr_ref

    这几天开始拜读侯捷先生和孟岩先生的译作<C++标准程序库:自修教程与参考手册> .两位先生确实译功上乘,读得很顺.但是读到P55页关于auto_ptr_ref的讨论,却百思不得其解:为什么 ...