java从给定数组中选出和为固定值的两个数

package com.51dev;


/**
 *  https://www.51dev.com
 *  从给定数组中选出和为固定值的两个数
 * 
 */
public class Test {
    public static void main(String[] args) {
        int[] nums = {1, 5, 9, 11, 3, 33, 11, 45, 67};
        Test test = new Test();
        test.sum(nums, 14);
    }

    public int[] sum(int[] nums, int target) {
        int data[] = new int[2];
        for (int i = 0; i < nums.length; i++) {

            for (int j = i + 1; j < nums.length-1; j++) {
                    if (nums[i] + nums[j]  == target) {
                        data[0] = i;
                        data[1] = j;
                    System.out.println(i + "," + j );
                    }
                }
            }
        return data;
    }
}

 

你可能感兴趣的