leectcode. 两数之和

示例:

给定 nums = [2, 7, 11, 15], target = 9
 
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
 
class Solution {
    public int[] twoSum(int[] nums, int target) {
        
        int [] a = new int[2];
        for ( int i = 0; i < nums.length; i ++){
            for (int j = i + 1; j < nums.length ; j ++){
                if (nums[i] + nums[j] == target){
                    a[0] = i;
                    a[1] = j;
                    break;
                }
            }
        }
    return a;
    }
    
}

猜你喜欢

转载自blog.csdn.net/weixin_38261597/article/details/89377106
今日推荐