题目 :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. jquerymobile,手机端click无效

    1.直接把<script>放到html代码后面,不要放到@section里面. 2.使用代理.如下所示: <script type="text/javascript&quo ...

  2. [cocos2d-js]chipmunk例子(一)

    initChipmunk:function() { this.space = new cp.Space(); this.setupDebugNode(); //设置空间内刚体间联系的迭代计算器个数和弹 ...

  3. 求n个数中的最大或最小k个数

    //求n个数中的最小k个数        public static void TestMin(int k, int n)        {            Random rd = new Ra ...

  4. POJ 3304 Segments (直线和线段相交判断)

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7739   Accepted: 2316 Descript ...

  5. JAVA大数运算

    java大数是个好东西,用起来方便,代码短. 代码如下: import java.util.*; import java.math.*; public class Main { public stat ...

  6. HNC-全局联想脉络

    全局联想脉络—语义块 和句类 一个语义块包含 核心部分 和说明部分. 语义块分为: 主语义块:有 4 种:特征E.作用者A.对象B.内容C 辅语义块:有 7 种:条件.手段.工具.途径.参照.因.果. ...

  7. hdoj 5373 The shortest problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5373 一开始想到用整除11的性质去做,即奇位数的和和偶位数的和的差为11的倍数,但估不准数据范围没敢去 ...

  8. java去掉jsp标签内容的方法

    //去掉内容的标签   public static String removeTag(String count){    try {    int tagCheck=-1;    do {     i ...

  9. sql中 with rollup 、with cube、grouping 统计函数用法

    with rollup .with cube.grouping CUBE 和 ROLLUP 之间的区别在于: CUBE 生成的结果集显示了所选列中值的所有组合的聚合. ROLLUP 生成的结果集显示了 ...

  10. EF小节

    EF学习笔记——生成自定义实体类 http://blog.csdn.net/leftfist/article/details/24889819 --工具: 1.entity developer 2.D ...