Description


Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

  1. Given nums = [2, 7, 11, 15], target = 9,
  2.  
  3. Because nums[0] + nums[1] = 2 + 7 = 9,
  4. return [0, 1].

思路


  首先我们想到的是暴力法,两个 for 循环求解,时间复杂度为O(n^2)

  

  1. //Runtime: 106 ms
  2. //First thought: BF
  3. class Solution {
  4. public:
  5. vector<int> twoSum(vector<int>& nums, int target) {
  6. int i,j;
  7. vector<int> ret {, };
  8. for (i = ; i< nums.size(); ++i) {
  9. ret[] = i;
  10. for(j = i+; j < nums.size(); ++j){
  11. if (nums[i] + nums[j] == target)
  12. {
  13. ret[] = j;
  14. return ret;
  15. }
  16. }
  17. }
  18. }
  19. };

  但是,我发现时间消耗太多了,所以借鉴当时“换硬币”、“爬楼梯”问题的优化方法,由于num[i]、num[j]、target都是定值,所以在条件判断里不需要每次都进行加运算

  1. //Runtime: 79 ms
  2. //Because nums[i], nums[j], target are fixed values, we do not need to do additions every time
  3. class Solution {
  4. public:
  5. vector<int> twoSum(vector<int>& nums, int target) {
  6. int i,j;
  7. vector<int> ret {, };
  8. for (i = ; i< nums.size(); ++i) {
  9. ret[] = i;
  10. int tar = target - nums[i];
  11. for(j = i+; j < nums.size(); ++j){
  12. if (nums[j] == tar)
  13. {
  14. ret[] = j;
  15. return ret;
  16. }
  17. }
  18. }
  19. }
  20. };

  AC后,看了 Discuss 里头的方法以及娄神的博客,我采用了更复杂的数据结构 散列表(HashTable)以降低时间复杂度,我的想法是这样: 如果想只扫描一遍数组就得出结果,那么肯定就要有一部字典,边扫描边存储值,在这里存储的不是数组当前的值,而是“目标值 - 当前值”,我们称之为对象值。

  也就是说,字典里存储的是每个数据所希望的”另一半“的大小。所以,字典的 Key 是对象值,字典的 Value 是数组索引。然后我们再往后扫描,如果扫描到的值的另一半出现在了字典里,那么说明当前值是”上一半“所需要的”下一半“,此时将它们的索引存储在 ret[0]、ret[1]中并返回;如果没有字典里没有出现它的另一半,那么把对象值和当前索引继续存储字典中。

  该算法的时间复杂度为 O(n)

  1. //Runtime: 6 ms
  2.  
  3. #include<unordered_map>
  4. using std::unordered_map;
  5.  
  6. class Solution {
  7. public:
  8. vector<int> twoSum(vector<int>& nums, int target) {
  9. unordered_map<int,int> um;
  10. vector<int> res();
  11. int i;
  12. int n = nums.size();
  13. for (i = ; i < n; ++i) {
  14. if (um.find(target - nums[i]) != um.end()) {
  15. res[] = um[target - nums[i]];
  16. res[] = i;
  17. return res;
  18. }
  19. else {
  20. um[nums[i]] = i;
  21. }
  22. }
  23. um.clear();
  24. }
  25. };

  补充一种双指针遍历有序数组的方法

  1. TwoNumberSum (S, x)
  2. mergeSort (S, , n)
  3. i =
  4. j = n
  5. while i < j
  6. if A[i] + A[j] == x
  7. return true
  8. if A[i] + A[j] < x
  9. i = i +
  10. if A[i] + A[j] > x
  11. j = j -
  12. return false  

  可以看得出来,查找的时间仅需 Θ(n),所以时间总代价受排序时间代价的影响,为Θ(n lgn)

  扫描的原理很简单,先设 mi,j  : A[i] + A[j] < S,Mi,j : A[i] + A[j] > S 。由于序列已排序,则 mi,j ⇒∀k < j  都有 mi,k  成立,并且 Mi,j ⇒ ∀k > i 都有 Mk,j 成立

  

  

LeetCode #1 TwoSum的更多相关文章

  1. Leetcode 1——twosum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  2. LeetCode 之 TwoSum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  3. leetcode之twosum

    class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector& ...

  4. leetcode ----ARRAY TWOSUM

    代码的(判断nums[i]或者是target-nums[i]都可以):

  5. [LeetCode_1] twoSum

    LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...

  6. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  7. ANDROID学习书单

    Skip to content PersonalOpen sourceBusinessExplore Sign upSign in PricingBlogSupport   This reposito ...

  8. Android技能树

    第一部分:Android(安卓)Android基础知识Android内存泄漏总结Handler内存泄漏分析及解决Android性能优化ListView详解RecyclerView和ListView的异 ...

  9. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

随机推荐

  1. Python 爬虫练习(一) 爬取国内代理ip

    简单的正则表达式练习,爬取代理 ip. 仅爬取前三页,用正则匹配过滤出 ip 地址和 端口,分别作为key.value 存入 validip 字典. 如果要确定代理 ip 是否真的可用,还需要再对代理 ...

  2. php 的开发工具

    通过上篇我们已经配置好了php的开发环境,我们就可以在这个模拟的环境下运行我们编写的php代码了. 在编写代码前,先安装一个自己喜欢的代码编辑器. 1.sublime text Sublime Tex ...

  3. Spring JDBC(一)jdbcTemplate

    前言 最近工作中经常使用Spring JDBC操作数据库,也断断续续的看了一些源码,便有了写一些总结的想法,希望在能帮助别人的同时,也加深一下自己对Spring JDBC的理解. Spring JDB ...

  4. Django contrib Comments 评论模块详解

    老版本的Django中自带一个评论框架.但是从1.6版本后,该框架独立出去了,也就是本文的评论插件. 这个插件可给models附加评论,因此常被用于为博客文章.图片.书籍章节或其它任何东西添加评论. ...

  5. #centos7 创建内网yum源 OpenStack源部署

    #centos7 创建内网yum源#centos7 自动化安装 本地 内网 web源创建.更新 createrepo http OpenStack源部署 Elven原创 http://www.cnbl ...

  6. 微软为.NET程序员带来了最优的跨平台开发体验-WSL

    前言 在前几个Visual Studio Code更新中发现有一个重要得特性,就是nodejs可以使用VS Code在WSL中进行Debug了(WSL是指Win10中的Linux子系统),之前写过一篇 ...

  7. poj 3111 K Best 最大化平均值 二分思想

    poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...

  8. HDU1284--完全背包

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  9. Aurora 论坛图片下载

    Aurora 论坛图片下载是一款快速下载指定网页图片的利器,还可以下载高清原图呢.现支持的网站:①蜂鸟网论坛②中关村摄影论坛③POCO摄影空间④图虫网其他摄影论坛陆续添加中... 效果图: 项目地址: ...

  10. 分布式系统中的必备良药 —— RPC

    阅读目录 前言 成熟的解决方案 剖析 性能测试 结语 一.前言 在上一篇分布式系统系列中<分布式系统中的必备良药 —— 服务治理>中阐述了服务治理的一些概念,那么与服务治理配套的必然会涉及 ...