当前位置:网站首页>LeetCode每日两题02:两数之和 II - 输入有序数组 (均1200道)
LeetCode每日两题02:两数之和 II - 输入有序数组 (均1200道)
2022-08-10 01:54:00 【那人独钓寒江雪.】
题目如下:

解题思路:一般给定的数组或链表是有序的话就要相当双指针了,因为此时左指针向右,右指针向左的移动都代表着数字的增大或减小,这样的移动s 包含信息的。
class Solution {
public int[] twoSum(int[] numbers, int target) {
int left = 0, right = numbers.length - 1;
while(left < right){
int sum = numbers[left] + numbers[right];
if(sum < target){
left++;
}else if(sum > target){
right--;
}else{
return new int[]{
left + 1,right + 1};
}
}
return new int[]{
-1,-1};
}
}

边栏推荐
猜你喜欢
随机推荐
grafana9配置邮箱告警
[Swoole Series 3.5] Process Pool and Process Manager
sql实战积累
Go语言JSON文件的读写操作
OpenSSF的开源软件风险评估工具:Scorecards
C# winform 单选框
深度学习(五) CNN卷积神经网络
FusionCompute产品介绍
Janus实际生产案例
实操|风控模型中常用的这三种预测方法与多分类场景的实现
Open3D 网格均匀采样
夏克-哈特曼波前传感器
UXDB现在支持函数索引吗?
2022杭电多校联赛第七场 题解
Nacos源码分析专题(五)-Nacos小结
Database management tool: dynamic read-write separation
首次在我们的centos上安装MySQL
The shell specifies the parameter name to pass the parameter
桌面云组件介绍与安装
墨西哥大众VW Mexico常见的几种label



![[论文阅读] Diverse Image-to-Image Translation via Disentangled Representations](/img/b8/891b8a8e7e70a1abd2016337ebc744.jpg)





