计蒜客---两数之和

这道题,给的提示是哈希表,排序,双指针,不明白为什么用到哈希表和排序,自己用简单数组搞的
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num,target;
        num = input.nextInt();
        int nums[] = new int[num];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = input.nextInt();
        }
        target = input.nextInt();
        judge(nums, target);
    }
    public static void judge(int[] nums,int target) {
        int min = 0,max = nums.length;
        boolean is = true;
        while(is){
            max--;
            for (min = 0; min < nums.length; min++) {
                if ((nums[min]+nums[max] == target)&&(min!=max)) {
                    is = false;
                    break;
                }else {
                    continue;
                }
            }
        }
        System.out.print((min+1)+" "+(max+1));
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-07-29 13:44  牛李  阅读(274)  评论(0编辑  收藏  举报

导航