[LeetCode] TwoSum
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的更多相关文章
- leetcode — two-sum
package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...
- leetCode:twoSum 两数之和 【JAVA实现】
LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...
- [leetcode]TwoSum系列问题
1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int ...
- LeetCode——TwoSum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- 【C语言工具】AddressSanitizer - 内存检测工具
Github 地址:https://github.com/google/sanitizers Wiki 地址:https://github.com/google/sanitizers/wiki/Add ...
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
- LeetCode #1 TwoSum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- Leetcode 1——twosum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode题解 1.TwoSum
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
随机推荐
- BZOJ 3907: 网格
Description 求不跨过直线 \(y=x\) ,到达 \((n,m)\) 的方案数. Sol 组合数学+高精度. 这个推导过程跟 \(Catalan\) 数是一样的. 答案就是 \(C^{n+ ...
- 9.5---所有字符串的排列组合(CC150)
1,这个是自己写的.一直LTE. public static ArrayList<String> getPerms(String str) { if (str == null) { ret ...
- phpcms新闻详情页上一篇下一篇的实现
在新闻详情页(show.html或show_*.html) 只需要添加类似如下代码即可: <div>上一篇:<a href="{$previous_page[url]}&q ...
- C# 非托管内存使用时的注意事项
调用Marshal.AllocHGlobal必须调用 Marshal.FreeHGlobal(ptr)来手动释放内存,即使调用GC.Collect();方法也无法释放,导致内存泄露!!
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- keepalived+haproxy构建高可用负载均衡
一.环境介绍 我用的是centos6.7,内核版本为2.6.32-573.el6.x86_64,keepalived版本为keepalived-1.2.22,haproxy版本为haproxy-1.6 ...
- adb 服务端口2037被占,导致adb和appium无法工作
症状1: 命令行运行 adb 相关命令,提示如下: adb server is out of date. killing...ADB server didn't ACK* failed to star ...
- 添加或修改ssh服务的端口
通常ssh远程登录的默认端口是22,这个端口一般是可以更改或者添加的,配置文件位置在:/etc/ssh/sshd_config通过编辑文件可以修改sshd服务的相关配置,以下新增端口2223,即除了2 ...
- java web 学习 --第一天(Java三级考试)
1.Servlet servlet是运行在web server或 application server端的Java程序,主要用于在服务器端产生动态内容. servlet 在服务器端主要有以下作用 读取 ...
- yum简单安装salt master与minion
首先得先安装epel的yum源: rpm -ivh http://mirrors.skyshe.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm 1.SaltS ...