问题描述:

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.

Example:

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

思路1:最简单的思路就是暴力求解,即循环遍历所有的可能情形,复杂度为o(n^2). 耗时未知,leetcode: Time Limit Exceeded

  1. class Solution {
  2. public:
  3. vector<int> twoSum(vector<int>& nums, int target) {
  4. vector<int> result;
  5. int n = nums.capacity();
  6. for(int i=;i<n;++i)
  7. {
  8. int i1 = nums.at(i);
  9. for(int j=i+;j<n;++j)
  10. {
  11. if(i1+nums.at(j)==target)
  12. {
  13. result.push_back(i);
  14. result.push_back(j);
  15. return result;
  16. }
  17. }
  18. }
  19. return result;
  20. }
  21. };

思路2:使用hash map,查询等各种操作都是常数级别的时间复杂度(最好不要使用map,map实现大都是使用红黑树,查询的时间复杂度是O(logn)),复杂度为O(n),leetcode: AC,16ms

  1. class Solution {
  2. public:
  3. vector<int> twoSum(vector<int>& nums, int target) {
  4. vector<int> result;
  5. unordered_map<int,int> hash;
  6. int n = nums.size();
  7. for(int i=;i<n;i++)
  8. {
  9. if(hash.find(target-nums[i])!=hash.end())
  10. {
  11. result.push_back(hash[target-nums[i]]);
  12. result.push_back(i);
  13. return result;
  14. }
  15. hash[nums[i]] = i;
  16. }
  17. return result;
  18. }
  19. };

思路3(参考论坛中4ms的c代码):别出机杼,逆向思考,第一步找出数组中与目标值的差值(这一步可以筛选部分明显不可能得到目标值的数),第二步则是遍历数组找到哪个数是差值。复杂度:O(n). leetcode:AC,6ms,超过99.57%的结果!

  1. class Solution {
  2. public:
  3. vector<int> twoSum(vector<int>& nums, int target) {
  4. vector<int> result;
  5. int n = nums.size();
  6. int max=, min=;
  7. for (int i = ; i < n; i++)
  8. {
  9. if (nums[i] > max)
  10. max = nums[i];
  11. if (nums[i] < min)
  12. min = nums[i];
  13. }
  14.  
  15. int* repeat_tag = new int[max - min + ]();
  16. int diff;
  17. for (int i = ; i < n; i++)
  18. {
  19. diff = target - nums[i];
  20. if (diff <= max && diff >= min)
  21. {
  22. repeat_tag[diff - min]= i;
  23. }
  24. }
  25. for (int i = ; i < n; i++)
  26. {
  27. if (repeat_tag[nums[i] - min] != )
  28. {
  29. if (i != repeat_tag[nums[i] - min])
  30. {
  31. result.push_back(i);
  32. result.push_back(repeat_tag[nums[i] - min]);
  33. return result;
  34. }
  35. }
  36. }
  37. return result;
  38. }
  39. };

1.Two Sum(c++)(附6ms O(n) accepted 思路和代码)的更多相关文章

  1. 10分钟理解Android数据库的创建与使用(附具体解释和演示样例代码)

    1.Android数据库简单介绍. Android系统的framework层集成了Sqlite3数据库.我们知道Sqlite3是一种轻量级的高效存储的数据库. Sqlite数据库具有以下长处: (1) ...

  2. 纯前端实现词云展示+附微博热搜词云Demo代码

    前言 最近工作中做了几个数据可视化大屏项目,其中也有用到了词云展示,以前做词云都是用python库来生成图片显示的,这次用了纯前端的实现(Ctrl+V真好用),同时顺手做个微博热搜的词云然后记录一下~ ...

  3. 【年终分享】彩票数据预测算法(一):离散型马尔可夫链模型实现【附C#代码】

    原文:[年终分享]彩票数据预测算法(一):离散型马尔可夫链模型实现[附C#代码] 前言:彩票是一个坑,千万不要往里面跳.任何预测彩票的方法都不可能100%,都只能说比你盲目去买要多那么一些机会而已. ...

  4. [LeetCode] Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  5. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  6. leetcode problem sum

    2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  7. Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. (step4.3.4)hdu 1258(Sum It Up——DFS)

    题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...

  9. 【leetcode】363. Max Sum of Rectangle No Larger Than K

    题目描述: Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the ma ...

随机推荐

  1. gerrit 为每个工程设置提交的reviewer

    尝试安装了 https://gerrit-ci.gerritforge.com/job/plugin-reviewers-stable-2.13/lastSuccessfulBuild/artifac ...

  2. KD-tree(2维)

    用于动态插入以及求某点的最近点的距离(BZOJ2648,BZOJ2716) #include <cstdio> #include <cmath> #include <al ...

  3. mysql的explain学习

    explain是用来获取sql执行计划的信息. 上面是一个最简单的sql分析.下面来分析每列的意思. ①id  ②select_type ③ table ④type ⑤possible_key ⑥ke ...

  4. 用 ElementTree 在 Python 中解析 XML

    用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...

  5. jQuery遍历checkbox

    $("input[type='checkbox']").each(function(){ var value = $(this).val(); //获得值 $(this).attr ...

  6. HAProxy的日志配置以及ACL规则实现负载均衡

    HAProxy配置日志策略 默认情况下,HAProxy是没有配置日志的在centos6.3下默认管理日志的是rsyslog,可以实现UDP日志的接收,将日志写入文件,写入数据库先检测rsyslog是否 ...

  7. c++转载系列 std::vector模板库用法介绍

    来源:http://blog.csdn.net/phoebin/article/details/3864590 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作 ...

  8. Shiro-集成Spring

    集成Spring 加入Spring 和Shiro的jar 包 配置Spring 及SpringMVC 参照:1.3.2\shiro-root-1.3.2-source-release\shiro-ro ...

  9. BZOJ 1041: [HAOI2008]圆上的整点

    1041: [HAOI2008]圆上的整点 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3621  Solved: 1605[Submit][Sta ...

  10. php 使用函数中遇到的坑之----list

    1. list 把数组中的值赋给一些变量 <?php $info = array('coffee', 'brown', 'caffeine'); // 列出所有变量 list($drink, $ ...