leetcode Two Sum II - Input array is sorted <面试常考题>
题目描述

//二分查找的变形 用头尾两个指针进行 面试考察题
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> ret;
if(numbers.size() == )
return ret;
int i = ;
int j = numbers.size() -;
while(i < j){
if(numbers[i] + numbers[j] == target){
ret.push_back(i+);
ret.push_back(j+);
return ret;
}
if(numbers[i] + numbers[j] > target){
j--;
}
if(numbers[i] + numbers[j] < target){
i++;
}
}
return ret;
}
};
leetcode Two Sum II - Input array is sorted <面试常考题>的更多相关文章
- [LeetCode] 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 Two Sum II - Input array is sorted
原题链接在这里:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 题目: Given an array of intege ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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 ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- LeetCode_167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sorted Easy Given an array of integers that is already sorted in as ...
- leetcode2 Two Sum II – Input array is sorted
Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...
- 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 ...
随机推荐
- Period UVALive - 3026(next数组)
题意: 给出一个长度不超过1000000的字符串S, 对于该字符串的所有前缀求其周期, 如果周期K >= 2输出起始位置是第几个字符和其周期K 解析: 先求next数组 对于每一个位置如果i % ...
- 【数组】- ArrayList自动扩容机制
不同的JDK版本的扩容机制可能有差异 实验环境:JDK1.8 扩容机制: 当向ArrayList中添加元素的时候,ArrayList如果要满足新元素的存储超过ArrayList存储新元素前的存储能力, ...
- 聊聊flink的CsvTableSource
序 本文主要研究一下flink的CsvTableSource TableSource flink-table_2.11-1.7.1-sources.jar!/org/apache/flink/tabl ...
- xpose修改手机imei码,注入广告
何为hook Hook英文翻译过来就是“钩子”的意思,那我们在什么时候使用这个“钩子”呢? 我们知道,在Android操作系统中系统维护着自己的一套事件分发机制.应用程序,包括应用触发事件和后台逻 ...
- 洛谷5月月赛T30212 玩游戏 【分治NTT + 多项式求ln】
题目链接 洛谷T30212 题解 式子很容易推出来,二项式定理展开后对于\(k\)的答案即可化简为如下: \[k!(\sum\limits_{i = 0}^{k} \frac{\sum\limits_ ...
- 洛谷 P4066 [SHOI2003]吃豆豆 解题报告
P4066 [SHOI2003]吃豆豆 题目描述 两个PACMAN吃豆豆.一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方.PACMAN走到豆豆处就会吃掉它.PACMAN行走的路线很奇怪 ...
- vim在行首和 行尾加
在每行开始加入“<a href=” vim 命令: :%s/^/<a href=/g 在每行尾加入 “</a>” vim命令 : ...
- Linux(五)shell编程基础
一.Linux shell简介 1.shell概述 Shell 是用户与内核进行交互操作的一种接口,目前最流行的 Shell 称为 bash Shell Shell 是一门编程语言& ...
- bzoj 1004 组合
代码: //根据Burnside定理:有m个置换k钟颜色,所有本质不同的染色方案数就是每种置换的不变元素的个数的平均数.所谓不变元素就是一种染色方案 //经过置换变换后和之前一样.所以现在就是要求不变 ...
- P4752 Divided Prime
P4752 Divided Prime 题目描述 给定一个数字 AA ,这个 AA 由 a_1,a_2,\cdots,a_Na 1 ,a 2 ,⋯,a N 相乘得到. 给定一个数字 BB ...