两数之和
两数之和
http://www.nowcoder.com/questionTerminal/20ef0972485e41019e39543e8e895b7f
import java.util.*; public class Solution { /** * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] numbers, int target) { // write code here‘ int[] twoSum = new int[2]; List<Integer> list = new ArrayList<Integer>(); for(int i=0 ; i < numbers.length ; i++){ list.add(numbers[i]); } for(int i=0 ; i < numbers.length ; i++){ int index = target - numbers[i]; if(list.contains(index)){ index = list.indexOf(index); if(index != i){ if(index < i){ twoSum[0] = index + 1; twoSum[1] = i + 1; }else{ twoSum[1] = index + 1; twoSum[0] = i + 1; } break; } } } return twoSum; } }