LeetCode 167 Two Sum II - Input array is sorted
Problem:
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. 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
Summary:
有序数组,找到两数之和等于target的数字下标。
Solution:
两个指针i和j分别指向数组头和尾,若nums[i] + nums[j] > target,则j--,反之则i++,直至找到目标数。
 class Solution {
 public:
     vector<int> twoSum(vector<int>& numbers, int target) {
         int len = numbers.size();
         int i = , j = len - ;
         vector<int> res;
         while (i < j) {
             if (numbers[i] + numbers[j] > target) {
                 j--;
             }
             else if (numbers[i] + numbers[j] < target) {
                 i++;
             }
             else {
                 return {i + , j + };
             }
         }
         return {};
     }
 };
LeetCode 167 Two Sum II - Input array is sorted的更多相关文章
- 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] 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 ...
 - 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 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  167. Two Sum II - Input array is sorted  求两数相加等于一个数的位置 --------- java
		
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
 - Java [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 th ...
 - LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告
		
1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...
 - 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 ...
 - 167. Two Sum II - Input array is sorted - LeetCode
		
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
 
随机推荐
- Gc.Db之循序渐进
			
距离上次写Gc.Db框架已经有一段时间了,最近默默对框架代码已经做了不少优化和功能,且已经提交至nuget,大家如果想使用此框架,可以通过nuget搜索:Gc.Db进行下载和安装包. 本篇文章主要是介 ...
 - Genymotion安装问题
			
今天安装虚拟机各种报错,网上收了各种资料,然而并不能解决问题.啥也没干整整一天总算找到解决方案解决. 解决方法︰ 删除所有以前的虚拟框适配器 转到设备管理器中,单击"操作" ...
 - 8 HTML&JS等前端知识系列之jquery的自定义方法
			
preface 有时候我们在前端写jquery的时候,会自己自定义些方法,这样可以不需要重复造轮子.先说说2种自定义方法的区别: 不跟在选择器后面的 跟在选择器后面的. 那下面说说如何自定义jquer ...
 - JavaScript中的静态成员
			
静态:共享 一.公有静态成员(作为函数的属性即可): var Gadget = function(price) { this.price = price; } Gadget.isShiny = fun ...
 - mybatis- spring 批量实现数据导入数据库
			
终于实现了ibatis的批量插入,此方法插入3000条数据,比单条插入可以节省一半的时间XML代码: <insert id="insertBatch" parameterTy ...
 - adobe dreameaver cs5 禁止更新
			
需要修改系统的host文件,将官方验证服务器全指向本机 用记事打开 C:\WINDOWS\system32\drivers\etc 下面的 host (没扩展名) 然后在后面添加 127.0.0.1 ...
 - DX系列之TreeList
			
参考资料: DevXpress控件: 第三篇: 将 父子 关系进行到底
 - java初始化
			
一.成员初始化 1.成员变量没有赋值,则被初始化成默认值. 2.局部变量没有赋值,编译时报错. 二.构造器初始化 1.成员变量在构造器初始化之前,已经被初始化. 2.变量定义的顺序决定了初始化的顺序. ...
 - 【Matlab】特征值
			
特征值 clc;clear; %[V,D]=eig(A) //求取特征值 A=[ 1 2 4; 4 0 7; 9 1 3 ]; [V,D]=eig(A) 结果如下: 求解特征值与特征向量时矩阵必须是方 ...
 - c/c++ long long 和__64int区别
			
在C/C++中,64为整型一直是一种没有确定规范的数据类型.现今主流的编译器中,对64为整型的支持也是标准不一,形态各异.一般来说,64位整型的定义方式有long long和__int64两种(VC还 ...