题目 :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. 第二百三十二天 how can I 坚持

    早上竟然飘起了大学,可是就下了一会,没有一点学的痕迹. 博客园真不知道怎么回事了,字真的好小了. 晚上回来心情好不好,感觉好累,最近不知道怎么了,约罗娜出来吃个饭怎么都约不出来,咋办呢.哎,愁人. 最 ...

  2. cocos2d-x3.2 使用开关控制按钮 ControlSwitch

    ContolSwitch 控件起到了一个开关的作用类似于现实生活中的开关,直接上代码: .h文件 // // SwitchBtnScene.h // LSWGameIOS // // Created ...

  3. IOC知识

    1.两个基本概念 IOC(Inversion of Control ):反转控制,即将控制权反转出去. DI(Dependency Injection):依赖注入,根据依赖关系进行注入. DI是实现I ...

  4. class bool

    class bool(int): """ bool(x) -> bool Returns True when the argument x is true, Fal ...

  5. [Java线程] Java线程池ExecutorService

    示例 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.u ...

  6. UI:数据持久化

    数据持久化    参考1  参考2  参考3 什么是数据持久化,就是将文件保存在本地的硬盘中,使得应用程序或者机器重启后可以继续访问以前保留的数据.IOS开发中有许多的数据持久化方案. 如下面五种方案 ...

  7. jquery 显示“加载状态 结束”

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Ehcache(07)——Ehcache对并发的支持

    http://haohaoxuexi.iteye.com/blog/2119733 Ehcache对并发的支持 在高并发的情况下,使用Ehcache缓存时,由于并发的读与写,我们读的数据有可能是错误的 ...

  9. wp———图片切换效果

    此篇文章主要是记录一下使用XamlReader加载动画时遇到的一些问题. 首先呢,把源码附上 <phone:PhoneApplicationPage x:Class="PicChang ...

  10. javascript优化

    javaScript是一门解释性的语言.它不像java.C#等程序设计语言.由编译器先进行编译再运行.而是直接下载到用户的客户端进行执行.因此代码本身的优劣就直接决定了代码下载的速度以及执行的效率. ...