package com.zerroi.leetcode.Three12; import java.util.Arrays; /* * 给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列 ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1] 和 numbers[index2] ,则 1 <= index1 < index2 <= numbers.length 。 以长度为 2 的整数数组 [index1, index2] 的形式返回这两个整数的下标 index1 和 index2。 你可以假设每个输入 只对应唯一的答案 ,而且你 不可以 重复使用相同的元素。 你所设计的解决方案必须只使用常量级的额外空间。 * */ public class TwoSum { public static void main(String[] args) { SolutionSecond solutionSecond = new SolutionSecond(); int[] res = solutionSecond.twoSum(new int[]{2, 7, 11, 15}, 9); for (int i : res) { System.out.println("i = " + i); } } } class SolutionSecond { public int[] twoSum(int[] numbers, int target) { for (int i = 0; i < numbers.length; i++) { for (int j = i + 1; j < numbers.length; j++) { if (numbers[i] + numbers[j] == target) { return new int[]{i+1, j+1}; } } } return null; } public int[] twoSum(int[] numbers, int target, int abc) { int left = 0; int right = numbers.length - 1; while (left < right) { int sum = numbers[left] + numbers[right]; if (sum == target) { return new int[]{left + 1, right + 1}; } else if (sum > target) { right--; } else { left++; } } return null; } }