mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-30 07:33:45 +00:00
Update 57.1 和为 S 的两个数字.md
题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
## 题目链接
|
||||
|
||||
[牛客网](https://www.nowcoder.com/practice/390da4f7a00f44bea7c2f3d19491311b?tpId=13&tqId=11195&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
|
||||
[Leetcode](https://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof/)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@@ -17,17 +17,17 @@
|
||||
- 如果 sum \< target,移动较小的元素,使 sum 变大一些。
|
||||
|
||||
```java
|
||||
public ArrayList<Integer> FindNumbersWithSum(int[] nums, int target) {
|
||||
int i = 0, j = nums.length - 1;
|
||||
public int[] twoSum(int[] price, int target) {
|
||||
int i = 0, j = price.length - 1;
|
||||
while (i < j) {
|
||||
int cur = nums[i] + nums[j];
|
||||
int cur = price[i] + price[j];
|
||||
if (cur == target)
|
||||
return new ArrayList<>(Arrays.asList(nums[i], nums[j]));
|
||||
return new int[] {price[i], price[j]};
|
||||
if (cur < target)
|
||||
i++;
|
||||
else
|
||||
j--;
|
||||
}
|
||||
return new ArrayList<>();
|
||||
return new int[0];
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user