题解 | #两数之和#
两数之和
http://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
暴力解法,用和減去一個數得到一個值必然在數組中,遍歷數組找到該值的下標即可
public static int[] twoSum(int[] numbers, int target) { // write code here int reduceNumber = 0; for (int i = 0; i < numbers.length; i++) { reduceNumber = target - numbers[i]; if (reduceNumber >= 0) { for (int j = i + 1; j < numbers.length; j++) { if (reduceNumber == numbers[j]) { return new int[]{i+1, j+1}; } } } } return 0; }