剑指 Offer 57. 和为s的两个数字

题目链接:https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/

执行用时:1 ms, 在所有 Java 提交中击败了99.43% 的用户
内存消耗:60.2 MB, 在所有 Java 提交中击败了5.61% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int[] twoSum(int[] nums, int target) {
int left = 0, right = nums.length-1;
while (nums[right] + nums[left] != target) {
if (nums[right] + nums[left] < target) {
left++;
} else {
right--;
}
}
int[] ans = {nums[left], nums[right]};
return ans;
}
}

剑指 Offer 57. 和为s的两个数字
https://pisces34.github.io/2022/02/12/leetcode/offer57/
发布于
2022年2月12日
许可协议