1.two sum

题目大意:给出一个int数组,和一个目标数字。求出该数组中哪两个的值相加等于目标数字,返回两个值得数组下标。

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++){
            for (int j = i + 1; j < nums.length; j++){
                if(nums[j] == target - nums[i]){
                    return new int[] {i,j};
                }
            }
        } 
        throw new IllegalArgumentException("No two sum solution");
    }
}

你可能感兴趣的:(1.two sum)