问题描述:

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:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

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

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
int n = nums.capacity();
for(int i=;i<n;++i)
{
int i1 = nums.at(i);
for(int j=i+;j<n;++j)
{
if(i1+nums.at(j)==target)
{
result.push_back(i);
result.push_back(j);
return result;
}
}
}
return result;
}
};

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

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
unordered_map<int,int> hash;
int n = nums.size();
for(int i=;i<n;i++)
{
if(hash.find(target-nums[i])!=hash.end())
{
result.push_back(hash[target-nums[i]]);
result.push_back(i);
return result;
}
hash[nums[i]] = i;
}
return result;
}
};

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

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
int n = nums.size();
int max=, min=;
for (int i = ; i < n; i++)
{
if (nums[i] > max)
max = nums[i];
if (nums[i] < min)
min = nums[i];
} int* repeat_tag = new int[max - min + ]();
int diff;
for (int i = ; i < n; i++)
{
diff = target - nums[i];
if (diff <= max && diff >= min)
{
repeat_tag[diff - min]= i;
}
}
for (int i = ; i < n; i++)
{
if (repeat_tag[nums[i] - min] != )
{
if (i != repeat_tag[nums[i] - min])
{
result.push_back(i);
result.push_back(repeat_tag[nums[i] - min]);
return result;
}
}
}
return result;
}
};

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. 如何解决ajax重复提交的问题

    如下一段代码: 先忽略我没引jquery.js的问题,这是一个案例. 当我们点击提交时,控制台输出两次e,在network里查看,可以看到我们的ajax传输了两次,造成了数据重复提交. 一种解释为bu ...

  2. JS常用正则表达式

    1.IP地址验证 var reg = /^(([1-9])|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([1-9][0 ...

  3. sql 取汉字首字母

    )) ) --用于加密 --WITH ENCRYPTION as begin declare @intLen int ) ) set @intLen = len(@str) set @strRet = ...

  4. 全局变量都是window对象的属性

    var x = "haha"; var test  = function(){ alert(this.x); } 上述,则会弹出 haha的值. 因为在JavaScript的变量作 ...

  5. 【codeforces 148D】 Bag of mice

    http://codeforces.com/problemset/problem/148/D (题目链接) 题意 包中有w个白鼠,b个黑鼠.公主和龙轮流画老鼠,公主先画,谁先画到白鼠谁就赢.龙每画完一 ...

  6. Binary Agents

    FCC题目: 传入二进制字符串,翻译成英语句子并返回. 二进制字符串是以空格分隔的. 示例: binaryAgent("01000001 01110010 01100101 01101110 ...

  7. destoon : 后台无法登录问题解决

    经常有朋友在destoon搬家的时候 , 数据还原之后 , 会出现后台无法登录的情况 . 具体表现为后台帐号密码输入后点击确定 , 页面刷新 .并没有跳转到相应后台页面 . 但是如果帐号密码输入错误 ...

  8. CentOS7清理yum缓存和释放内存方法

    清理yum缓存 清理yum缓存使用yum clean 命令,yum clean 的参数有headers, packages, metadata, dbcache, plugins, expire-ca ...

  9. CSS-用伪类制作小箭头(轮播图的左右切换btn)

    先上学习地址:http://www.htmleaf.com/Demo/201610234136.html 作者对轮播图左右按钮的处理方法一改往常,不是简单地用btn.prev+btn.next的图片代 ...

  10. MVC学习随笔----如何在页面中添加JS和CSS文件

    http://blog.csdn.net/xxjoy_777/article/details/39050011 1.如何在页面中添加Js和CSS文件. 我们只需要在模板页中添加JS和CSS文件,然后子 ...