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

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

妈妈我再也不敢装逼了,本以为三分钟可以秒杀的题做了三小时!心想暴力O(n^2)肯定是过不了的,然后脑子里飘来4个字“二分查找”,然后我就写啊写,写好了二分找发现这题返回的不是数啊,是原数组的index啊,这下麻烦了,因为二分找要排序,排完序索引不就丢了嘛,没办法只能copy一次原数组然后用二分找找出值来在遍历一遍原始数组找索引,弄来弄去做了很长时间才AC,最后网上发现简单的解法:排序然后从两头向中间找。。。

要注意几点就是:1.返回的是索引,不是数。 2.允许出现重复

int bfind(vector<int> &numbers, int left, int right, int target, int exclu) {
if (left > right) return -;
int mid = (left + right)/;
if (target > numbers[mid]) {
return bfind(numbers, mid + , right, target, exclu);
}
else if (target < numbers[mid]) {
return bfind(numbers, left, mid - , target, exclu);
}
else {
if (mid != exclu) {
return numbers[mid];
}
return -;
}
} vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> ret;
vector<int> aux = numbers;
sort(numbers.begin(), numbers.end()); // O(nlogn)
for (int i = ; i < aux.size(); i++) {
int j = bfind(numbers,, (int)numbers.size()-, target-numbers[i], i);
if (j >= ) {
for (int k = ; k < aux.size(); k++) {
if (aux[k] == numbers[i]) {
ret.push_back(k+);
}
else if (aux[k] == j) {
ret.push_back(k+);
}
}
return ret;
}
}
return ret;
}

网上有更简单的版本,这个就仅供娱乐吧。

[LeetCode] TwoSum的更多相关文章

  1. leetcode — two-sum

    package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...

  2. leetCode:twoSum 两数之和 【JAVA实现】

    LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...

  3. [leetcode]TwoSum系列问题

    1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int ...

  4. LeetCode——TwoSum

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

  5. 【C语言工具】AddressSanitizer - 内存检测工具

    Github 地址:https://github.com/google/sanitizers Wiki 地址:https://github.com/google/sanitizers/wiki/Add ...

  6. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

  7. LeetCode #1 TwoSum

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

  8. Leetcode 1——twosum

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

  9. leetcode题解 1.TwoSum

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

随机推荐

  1. windows下的getopt/getoptlong函数

    windows下的getopt/getoptlong函数 getopt/getopt_long函数是GNU C中的函数,在linux编程中很常用到.这里就不介绍了. windows下没有找到类似的函数 ...

  2. ubuntu14.04显卡驱动问题(amd5600k集显7650d)

    安装ubuntu的时候,多次莫名其妙地黑屏,起初以为是最新得ubuntu14.04安装包不稳定,所以一直尝试了很多次. 重启-->安装-->黑屏(硬盘仍在转动,屏幕上什么都没有)--> ...

  3. jQuery常用API

    jQuery API查询网址 http://jquery.cuishifeng.cn/ Dom和jquery相互装换 jquery对象[0] => Dom对象 Dom对象 => $(Dom ...

  4. [转]ubuntu 14.04 如何开启和关闭触控板

    转自:http://lxn348567248.blog.163.com/blog/static/1201759252014649443513/ 禁用触摸板的命令:  sudo rmmod psmous ...

  5. 腾讯云环境配置之PHP5.6.3 + redis扩展 稳定版

    腾讯云环境配置之PHP5.6.3 + redis扩展 稳定版 时间:2015-01-18 01:41来源:linux.it.net.cn 作者:IT   #由于上文装过yum groupinstall ...

  6. POCO浅探

    POCO作用类似于POJO.POJO的内在含义是指那些从来没有任何类继承.也没有实现任何接口,更是没有被其他框架侵入的JAVA对象.POCO的含义与此是一致的.POJO的意义就在于它的简单灵活性,能够 ...

  7. oracle:安装笔记

  8. Sublime Text编辑工具带有 PEP 8 格式检测插件

    Sublime Text编辑工具带有 PEP 8 格式检测插件

  9. Sharepoint2013 中想要将网站另存为模板步骤

    Sharepoint2013 中想要将网站另存为模板步骤 第一步:使用SPD打开想要另存为模板的网站,找到网站选项: 第二步:点击打开网站选项,找到一个SaveSiteAsTemplateEnable ...

  10. 配置ubuntu虚拟机备忘

    #1配置minicom sudo minicom -s sudo minicom -w #1.配置网络,嵌入机的ip地址 ifconfig eth0 10.5.52.202 #2.挂载文件,把虚拟主机 ...