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 ...
随机推荐
- SQL 入门了解
SQL 随着应用程序的功能越来越复杂,数据量越来越大,如何管理这些数据就成了大问题: 读写文件并解析出数据需要大量重复代码: 从成千上万的数据中快速查询出指定数据需要复杂的逻辑. 如果每个应用程序都各 ...
- 在linux系统下运行jar包的命令如下
1.java -jar xxxxx.jar // 当前ssh窗口被锁定,可按CTRL + C打断程序运行,或直接关闭窗口,程序退出 2.java -jar xxxxx.jar & //当 ...
- 加载XML文件到系统中
using System;using System.Data;using System.IO;using System.Xml;using System.Collections.Generic; na ...
- 安卓控制LED驱动编写
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 打开Android Stud ...
- python 保存对象文件
之前用 R 语言一直感觉 .Rdata 格式的文件很好用,可以把每次执行的中间文件保存便于下次调用,刚熟悉 Python 还没接触这块知识,所以有时候做项目不太顺手,索性上网搜了下,整理如下: 模型存 ...
- Oauth2.0客户端服务端示例
https://blog.csdn.net/qq_28165595/article/details/80459185 前言前面的理解OAuth2.0认证与客户端授权码模式详解,我们大致了解了Oauth ...
- DokuWiki 命名空间管理
为了更好的组织结构,Dokuwiki提供了命名空间这个功能,那怎么管理命名空间的,其实可以安装插件去管理 Add New Page Plugin:新建界面 https://www.dokuwiki.o ...
- 微信小程序精品demo
http://www.jianshu.com/p/0ecf5aba79e1 感谢笔者的分享!
- mysqldump备份与恢复笔记
mysql> show databases; +--------------------+ | Database | +--------------------+ | inf ...
- [sql]sql函数coalesce返回第一个非空的值
下面来看几个比较有用的例子: 首先,从MSDN上看看这个函数的使用方法,coalesce函数(下面简称函数),返回一个参数中非空的值.如: SELECT COALESCE(NULL, NULL, G ...