题目:

最开始采用暴力解法,两个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. 【JavaScript】创建命名空间,Class,LOG

    JxUnderscore(function (J, _, root) { var isWindow, deepObject, Namespace, Class, LOG; /** * 一个对象是否为w ...

  2. AD按键-矩阵按键:

    原理:利用数组分压+AD采集: 优点:一个IO口可以做成多个按键,节省IO口(矩阵键盘在>4时优点才能体现出来):可备用作为AD基准输入. 缺点:不能做成组合按键(或者电阻要精确选择):且离IO ...

  3. JavaScript判断字符串是否含有中文(实用)

    引用页: http://javasam.iteye.com/blog/1465048 UTF-8有点类似于Haffman编码,它将Unicode编码为:0x00-0x7F的字符,用单个字节来表示:0x ...

  4. C++学习40 抛出自己的异常

    throw 是C++中的关键字,用来抛出异常.如果不使用 throw 关键字,try 就什么也捕获不到:上节提到的 at() 函数在内部也使用了 throw 关键字来抛出异常. throw 既可以用在 ...

  5. C++学习12 友元函数和友元类

    友元函数和友元类在实际开发中较少使用,想快速学习C++的读者可以跳过本节. 一个类中可以有 public.protected.private 三种属性的成员,通过对象可以访问 public 成员,只有 ...

  6. editplus格式化xml文档

    使用editplus打开xml文档,发现文件内容没有格式化,非常难看,使用IE9打开这个xml文档是格式化的,但是IE9打开不能编辑. 在网上找了一些资料发现能够通过一个小插件来是editplus格式 ...

  7. [HDU 2546]饭卡 (动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 题意:见题目 刚开始怎么写都写不对,后来吃饭的时候想了想记忆化搜索的办法 就是说设dp(now, ...

  8. java static 方法使用笔记

    有入参的static方法,可以正常使用 static的作用是申明:这是类的静态方法,什么时候都可以调用,可以传入入参,也可以不传. 上代码: 1.带静态方法的类: public class MakeP ...

  9. 学习Webservice测试

    2014-04-01 可用Myeclipse10自带工具生成客户端, 也可用CXF生成,注意,不要用CXF3.0.0milestone,该版本不能生成,请用CXF2.2.8 2015-01-12 下载 ...

  10. Oracle Profile 使用详解--zhuanzai

    一.目的: Oracle系统中的profile可以用来对用户所能使用的数据库资源进行限制,使用Create Profile命令创建一个Profile,用它来实现对数据库资源的限制使用,如果把该prof ...