普通网友 2016-09-19 15:48 采纳率: 0%
浏览 1020

leetcode 18. 4Sum,奇怪的性能问题,求java大神指点

 public static List<List<Integer>> fourSum(int[] nums, int target) {

    long firstTime = System.currentTimeMillis();
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    if(nums.length == 0)
        return result;
    Arrays.sort(nums);
    List<Integer> nowNode = null;

    int fixed1=0, fixed2;
    int first, last;
    int l = nums.length;
    int sum = 0;
    boolean isExist = false;

    while(fixed1 <= (l - 4)){
        fixed2 = fixed1 + 1;
        while(fixed2 <= (l - 3)){
            first = fixed2 + 1;
            last = l - 1;
            while(first < last){
                isExist = false;
                sum = nums[fixed1] + nums[fixed2] + nums[first] + nums[last];
                if(sum < target){
                    first++;
                }else if(sum > target){
                    last--;
                }else{

                    for(List<Integer> templ : result){
                        //代码1
                        if(templ.get(0) == nums[fixed1] && templ.get(1) == nums[fixed2] && templ.get(2) == nums[first]  
                        && templ.get(3) == nums[last]){
                            isExist = true;
                            break;
                        }
                    }
                    if(!isExist){
                        nowNode = new ArrayList<Integer>();
                        nowNode.add(nums[fixed1]);
                        nowNode.add(nums[fixed2]);
                        nowNode.add(nums[first]);
                        nowNode.add(nums[last]);
                        result.add(nowNode);
                    }


                    first++;
                    last--;
                }
            }

            fixed2++;

        }
        fixed1++;
    }
    System.out.println("先集合后数组"+(System.currentTimeMillis() - firstTime));
    return result;

    }




public static List<List<Integer>> fourSum1(int[] nums, int target) {
    long firstTime = System.currentTimeMillis(); 

    List<List<Integer>> result = new ArrayList<List<Integer>>();
    if(nums.length == 0)
        return result;
    Arrays.sort(nums);
    List<Integer> nowNode = null;

    int fixed1=0, fixed2;
    int first, last;
    int l = nums.length;
    int sum = 0;
    boolean isExist = false;

    while(fixed1 <= (l - 4)){
        fixed2 = fixed1 + 1;
        while(fixed2 <= (l - 3)){
            first = fixed2 + 1;
            last = l - 1;
            while(first < last){
                isExist = false;
                sum = nums[fixed1] + nums[fixed2] + nums[first] + nums[last];
                if(sum < target){
                    first++;
                }else if(sum > target){
                    last--;
                }else{

                    for(List<Integer> templ : result){
                        //代码2
                        if(nums[fixed1] == templ.get(0) && nums[fixed2] == templ.get(1) && nums[first] == templ.get(2) 
                        && nums[last] == templ.get(3)){
                            isExist = true;
                            break;
                        }
                    }
                    if(!isExist){
                        nowNode = new ArrayList<Integer>();
                        nowNode.add(nums[fixed1]);
                        nowNode.add(nums[fixed2]);
                        nowNode.add(nums[first]);
                        nowNode.add(nums[last]);
                        result.add(nowNode);
                    }


                    first++;
                    last--;
                }
            }

            fixed2++;

        }
        fixed1++;
    }
    System.out.println("先数组后集合"+(System.currentTimeMillis() - firstTime));
    return result;

    }

两个方法唯一不同只有注释地方,方法一中的”代码1“和方法二中的”代码2“,但是性能完全不同,方法一输出1,代码二输出0,再leetcode中方法一超时,方法二ac,这是什么原因。。。。。

  • 写回答

1条回答 默认 最新

  • devmiao 2016-09-19 15:51
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?