class Solution {
public int[] twoSum(int[] nums, int target) {
//定义一个数组用来接收两个数的下标
int[] a = new int[2];
//定义一个整数用来接收传过来的数target
int b = target;
//遍历数组
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
//找到两个数之和等于target的数的下标,并将两个下标传入a数组
if (nums[i] + nums[j] == b) {
a[0] = i;
a[1] = j;
}
}
}
//返回数组a
return a;
}
}
public class Test {
public static void main(String[] args) {
int[] a = {1,2,4,8,12};
int b = 9;
Solution solution = new Solution();
int[] c = solution.twoSum(a,b);
System.out.println("["+c[0]+","+c[1]+"]");
}
}