题目 :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. js 多物体运动

    <!doctype html> <html> <head> <meta charset = "utf-8"> <title&g ...

  2. TransactionScope事务对多个数据库的操作

    .Net 2.0引入了轻量级事务管理器(Lighweight Transaction Manager),即System.Transactions.TransactionManager. 轻量级事务管理 ...

  3. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  4. KextWizard 的使用方法;以及Kext安装的几种工具下载

    a.将你需要安装的Kext拖到非中文的路径中: b.运行该软件,将Kext拖入下图对应的方框里,然后选择位置安装: c.选择修复权限和重建缓存(一个是修复Extra文件夹,一个是修复SLE) Kext ...

  5. Cisco SDM

    SDM连接方式:http+telnet / https+ssh   要使用SDM对CISCO设备实现集中式管理,必须在设备上键入如下命令:   步骤1: 要启用路由器的HTTP/HTTPS 服务器,请 ...

  6. enumerate

    enumerate 函数用于遍历序列中的元素并分配一个序号(序号默认从零开始 可以制定任意值): >>> for i,j in enumerate(('a','b','c')): p ...

  7. ASP.NET MVC- DropDownList绑定

    看一下Controller例子 [ChildActionOnly] public List<SelectListItem> LoadItemStatus() { List<Selec ...

  8. 谈Android四大组件之Service篇

    Service简介 Service是Android系统中的四大组件之一,它是一种长生命周期的,没有可视化界面,运行于后台的一种服务程序.Service必须在AndroidManifest.xml中声明 ...

  9. SQL SERVER 数据库表同步复制 笔记

    SQL SERVER 数据库表同步复制 笔记 同步复制可运行在不同版本的SQL Server服务之间 环境模拟需要两台数据库192.168.1.1(发布),192.168.1.10(订阅) 1.在发布 ...

  10. Openfire开发配置,Openfire源代码配置,OpenFire二次开发配置(eclipse)

    首先去官网把openfire的源码下下来: http://www.igniterealtime.org/downloads/source.jsp 1.下载后放到你的workspace当中,我的woek ...