167. Two Sum II - Input array is sorted (Array)
Given an array of integers that is already sorted in ascending order, 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.
Note:
Your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution and you may not use the same element twice.
Example:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
for (int i = ; i < numbers.size(); i++){
if(target - numbers[i] < numbers[i]) break; //second data smaller than current data
if(binarySearch(numbers, i+, numbers.size()-, target-numbers[i])){
vector<int> ret;
ret.push_back(i+);
ret.push_back(pos+);
return ret;
}
}
}
bool binarySearch(vector<int>& numbers, int start, int end, int target){
if(start > end) return false;
int mid = start + (end - start )/;
if(numbers[mid] > target) return binarySearch(numbers, start, mid-, target);
else if (numbers[mid] < target) return binarySearch(numbers, mid+, end, target);
else{
pos = mid;
return true;
}
}
private:
int pos; //second data index
};
167. Two Sum II - Input array is sorted (Array)的更多相关文章
- 29. leetcode 167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 167. Two Sum II - Input array is sorted@python
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- 167. Two Sum II - Input array is sorted
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- (双指针 二分) leetcode 167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode&Python] Problem 167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
随机推荐
- [Java] Thread -- 避免Race Condition (Synchronized)
public class TestRaceCondition implements Runnable{ public static int counter = 0; public static voi ...
- nginx 文档链接
https://www.cnblogs.com/wcwnina/p/8728391.html NGINX简介 http://www.nginx.cn/doc/ ...
- MySQL Event--Event and EventScheduler
MySQL Event 创建EVENT语法: CREATE [DEFINER = { user | CURRENT_USER }] EVENT [IF NOT EXISTS] event_name O ...
- Go并发编程实战 (郝林 著)
第1章 初识Go语言 1.1 语言特性 1.2 安装和设置 1.3 工程构造 1.3.1 工作区 1.3.2 GOPATH 1.3.3 源码文件 package main import ( " ...
- java_初始化器
1. 执行的顺序 package java20180129_1; public class Demo { // instance variable initializer 实例变量初始化器 Strin ...
- Javascript var 和 let 的区别
Javascript var 和 let 的区别 var 是函数块的全局变量. let 是代码块的局部变量. let 变量不会提升,如果先使用后定义会 undefind. 参考: https://de ...
- 使用tcpdump测试反向代理和lvs的nat区别
关于反向代理,一个请求过来,实际反向代理服务器要和两个对象做3次握手 客户端到反向代理服务器,是一个3次握手 反向代理服务器请求后端web服务器,是一个3次握手 lvs的nat和反向代理不同. lvs ...
- ubuntu18关闭系统自动更新
ubuntu18.04关闭系统自动更新有两个方法:1.修改配置文件 修改配置文件/etc/apt/apt.conf.d/10periodic#0是关闭,1是开启,将所有值改为0vi etc/apt/a ...
- babelrc 中的 presets 字段(env, react)和 plugins 字段(dynamic-import-webpack, transform-object-rest-spread, ...)
一.presets 字段 目前用到 presets: [ 'env', 'react' // react 转码规则 ]: 只有 env 时,作用和 latest 相同,包括 es5.es6.es7 ...
- STS启动时卡在loading加载 dashboard.ui
如果你在用STS 3.4或3.5,启动时可能会卡在 解决方法:打开STS安装目录下的plugins目录,删除文件 org.springsource.ide.eclipse.dashboard.ui_3 ...