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. BZOJ 4547: Hdu5171 小奇的集合

    Sol 首先,考虑这个要怎么搞...让总和最大的方法就是选出当前集合中最大的两个数相加放入集合中就可以了,证明非常简单,当前集合的和为x,它的和只会一直往后增加,所以只需要找到最大的两个数的和加入便是 ...

  2. 26 BasicUsageEnvironment基本使用环境——Live555源码阅读(三)UsageEnvironment

    26 BasicUsageEnvironment基本使用环境--Live555源码阅读(三)UsageEnvironment 26 BasicUsageEnvironment基本使用环境--Live5 ...

  3. 表单验证神器——jquery.validate插件

    jquery.validate.js插件应用举例,ajax方式提交数据. html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  4. 通过代理连接go01ge

     日本:https://pac.mcplay.cn/jp.pac台湾:https://pac.mcplay.cn/tw.pac如果想上Google可以试试简单的方法,在ie的代理自动配置脚本设置这个p ...

  5. 安装CentOS 7时出现No Caching mode page found问题的解决

    将CentOS 7镜像刻到U盘之后,向服务器安装时,使用U盘启动会出现两种启动选项,一种是UEFI启动选项,一种是默认的启动选项,如果不使用UEFI方式安装,那么一般是没有问题的,如果选择UEFI方式 ...

  6. Effective C++ -----条款33:避免遮掩继承而来的名称

    derived classes内的名称会遮掩base classes内的名称.在public继承下从来没有人希望如此. 为了让被遮掩的名称再见天日,可使用using声明式或转交函数(forwardin ...

  7. tomcat浏览器地址支持中文方法

  8. 【python】datetime获取日期,前一天日期

    1.获取字符串型当前日期 2016-10-09格式 import datetime today = datetime.date.today() #datetime.date类型当前日期 str_tod ...

  9. 【XLL API 函数】xlCoerce

    将 XLOPER/XLOPER12 转换为另一种类型,或是查询表格中的单元格值. 函数原型 Excel12(xlCoerce, LPXLOPER12 pxRes, 2, LPXLOPER12 pxSo ...

  10. php正则表达式、数组

    <?php $s = "he8llo5wor6ld"; $s = preg_replace("/\d/","#",$s);按照正则表达 ...