要注意力扣和机试题的不同之处
import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args) {
/* 辅助输入验证代码
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int target = scanner.nextInt();
int [] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
scanner.close();
Solution solution = new Solution();
int [] reslut = solution.twoSum(nums, target);
System.out.println("[" + reslut[0] + "," + reslut[1] + "]");
*/
}
}
//解决函数
class Solution {
public int[] twoSum(int [] nums, int target) {
for (int i = 0; i < nums.length; i++) {
int a = nums[i];
int result = target - a;
for (int j = 0; j < nums.length; j++) {
if (j != i && nums[j]== result) {
int[] e = {i, j};
return e;
}
}
}
int [] r = {-1, -1};
return r;
}
}