题目 :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.  (Easy)

解法1: Two pointers

拷贝一份,将数组排序,两根指针分别从前后向中间扫描,找到解为止。再遍历原数组寻找下标添加到结果内。

复杂度: O(nlogn)

注意:比如0,3,4,0  target = 0这组数据,需要让result第二个参数从后向前扫描才能保证答案正确。

代码:

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> v(nums);
vector<int> result;
sort(nums.begin(), nums.end());
int i = ,j = nums.size() - ;
while (nums[i] + nums[j] != target) {
if (nums[i] + nums[j] > target) {
j--;
}
else {
i++;
}
}
for (int k = ; k < nums.size(); ++k) {
if (v[k] == nums[i]) {
result.push_back(k);
break;
}
}
for (int k = nums.size() - ; k >= ; --k) { //从后向前扫描
if (v[k] == nums[j]) {
result.push_back(k);
break;
}
}
return result;
}
};

解法2:

利用hash表建立数值与下标的一一对应,扫描一遍即得解。

注: 本以为是O(n),但运行时间比解法1居然慢,查看discuss得知没有注意find()方法在最差情况下执行效率是O(n)的。

代码:

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//数值 与 下标一一对应
unordered_map<int,int> hash;
vector<int> result;
for (int i = ; i < nums.size(); ++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;
}
};

LeetCode1 Two Sum的更多相关文章

  1. leetcode-1 Two Sum 找到数组中两数字和为指定和

     问题描写叙述:在一个数组(无序)中高速找出两个数字,使得两个数字之和等于一个给定的值.如果数组中肯定存在至少一组满足要求. <剑指Offer>P214(有序数组) <编程之美& ...

  2. Leetcode--1. Two Sum(easy)

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

  3. [Swift]LeetCode1 .两数之和 | Two Sum

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

  4. LeetCode-1:Two Sum

    [Problem:1-Two Sum] Given an array of integers, return indices of the two numbers such that they add ...

  5. LeetCode1:Two Sum

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

  6. leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List

    """ Given the head of a linked list, we repeatedly delete consecutive sequences of no ...

  7. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  8. 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 ...

  9. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

随机推荐

  1. Tagged Pointers

    [Tagged Pointers] 1.what is tagged pointer? 2.原理剖析

  2. mysql中间件研究(Atlas,cobar,TDDL)

    mysql-proxy是官方提供的mysql中间件产品可以实现负载平衡,读写分离,failover等,但其不支持大数据量的分库分表且性能较差.下面介绍几款能代替其的mysql开源中间件产品,Atlas ...

  3. 项目视频讲解_[HeyJava][尚学堂][CMS文章内容管理系统]

    [HeyJava][尚学堂][CMS文章内容管理系统] http://pan.baidu.com/s/1c0imHrE

  4. 配置Redis主从复制

    [构建高性能数据库缓存之redis主从复制][http://database.51cto.com/art/201407/444555.htm] 一.什么是redis主从复制? 主从复制,当用户往Mas ...

  5. 【原创】[PS技巧]如何制作翘角纸条的阴影效果

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-05-19) 在网页设计中经常会用到翘角纸条的效果.如:百度空间首页: pic.1 下面我详细讲解一下这个效果使用PS如何制 ...

  6. NSNotificationCenter需要注意的几个问题

    NSNotificationCenter是iOS中常用的消息通知机制,不过在使用过程中有几点需要注意的问题. 直接贴Apple 的官方文档吧: A notification center delive ...

  7. java中string与byte[]的转换

    1.string 转 byte[] byte[] midbytes=isoString.getBytes("UTF8"); //为UTF8编码 byte[] isoret = sr ...

  8. ThinkPHP模板(一)

    如何关闭ThinkPHP的模板缓存 ThinkPHP的模板缓存是无奈关闭的,因为内置的模板引擎是一个编译型的模板引擎,必须经过编译后生成一个可执行的缓存文件才能被执行.但是可以设置缓存的有效期,例如设 ...

  9. C# DataGridView中合并单元格

    /// 合并GridView列中相同的行 /// /// GridView对象 /// 需要合并的列 public static void GroupRows(GridView GridView1, ...

  10. Android-Chat-Widget

    Original: https://github.com/ijarobot/Android-Chat-Widget Backup: https://github.com/eltld/Android-C ...