当前位置:网站首页>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};
}
}
边栏推荐
猜你喜欢
随机推荐
【UNR #6 B】机器人表演(DP)
Process management and task management
【论文笔记】基于深度学习的机器人抓取虚拟仿真实验教学系统
781. 森林中的兔子
The flask to add and delete
【二叉树-中等】1379. 找出克隆二叉树中的相同节点
OOD论文:Revisit Overconfidence for OOD Detection
多线程之自定义线程池
Unity vertex animation
UXDB现在支持函数索引吗?
2022杭电多校联赛第七场 题解
小程序开发的报价为什么有差别?需要多少钱?
中级xss绕过【xss Game】
Problems and solutions related to Chinese character set in file operations in ABAP
c# 解决CS8602告警 解引用可能出现空引用
T5:Text-toText Transfer Transformer
Shell编程--awk
OpenSSF的开源软件风险评估工具:Scorecards
Solve the problem of sed replacement text containing special characters such as "/" and "#"
Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily