美文网首页
1.4.15 ThreeSum

1.4.15 ThreeSum

作者: 风亡小窝 | 来源:发表于2016-07-28 17:24 被阅读9次
package analyse;
import java.util.Arrays;

import edu.princeton.cs.algs4.*;
import search.BinarySearch;
import tool.StopWatch;

public class ThreeSum {
    public static int count(int[] a){
        int count = 0;
        int len = a.length;
        
        for(int i = 0; i < len; i++){
            for(int j  = i + 1; j < len; j++){
                for(int z = j + 1; z < len; z++){
                    if((a[i] + a[j] + a[z]) == 0){
                        count++;
                    }
                }
            }
        }
        
        return count;
    }
    
    public static int countFaster(int[] a){
        int count = 0;
        int len = a.length;
        Arrays.sort(a); 
        
        for(int i = 0; i < len; i++){
            for(int j = i + 1; j < len; j++){
                if(j < BinarySearch.rank(-a[i]-a[j], a))
                    count++;
            }
        }   
        
        return count;
    }
    
    public static int countFastest(int[] a){
        int count = 0;
        Arrays.sort(a);
        int len = a.length;
        int posmin;
        int posmax;
    
        for(int i = 0; i < len; i++){
            posmin = i + 1;
            posmax = len - 1;
            while(posmin < posmax && a[posmin] + a[i] <= 0){
                if(a[posmin] + a[i] + a[posmax] < 0) posmin++;
                else if(a[posmin] + a[i] + a[posmax] > 0) posmax--;
                else {posmin++; posmax--; count++;}
            }
        }

        return count;
    }
    
    public static void main(String[] args) {
        //Attention: the array must has no duplicate elements;

        int a[] = new In("data/4Kints.txt").readAllInts();
        Stopwatch timer = new Stopwatch();
        System.out.print(count(a) + " ");
        System.out.println(timer.elapsedTime());
        
        Stopwatch timer2 = new Stopwatch();
        System.out.print(countFaster(a) + " ");
        System.out.println(timer2.elapsedTime());
        
        Stopwatch timer3 = new Stopwatch();
        System.out.print(countFastest(a) + " ");
        System.out.println(timer3.elapsedTime());
        
    }
}

来见证不同算法的性能差距吧吧:

Paste_Image.png

相关文章

  • 1.4.15 ThreeSum

    来见证不同算法的性能差距吧吧:

  • task

    task1.python实现三数之和,(正整数) class threesum: def threeSum(s...

  • ThreeSum问题

    ThreeSum问题:计算所有不同的整数的三元组的和,统计和为0的数量。上述代码是最简单直接的求解方式。 通过简单...

  • twoSum && threeSum

    from leetcode.com/problems twoSum Description: Possible A...

  • 3Sum

    class Solution {public: vector> threeSum(vector& nums)...

  • JavaScript三数之和

    var threeSum = function (nums) {if (nums.length < 3) {ret...

  • 3Sum

    /*dfs 算法 时间超时 class Solution { public List> threeSum(i...

  • ARTS打卡第一周6.16

    Algorithm 经典题型:threeSum 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存...

  • 亲亲我的宝贝 1.4.15

    宝贝,你现在也会逗人了啊 在我们背后挠痒痒,嘿嘿的笑 姥姥训练你在尿盆中大小便,成功一次,不过你还是不太习惯,只...

  • APACHE THRIFT, CPPMICROSERVICES,

    开发注意事项 第一版本(当前版本) libevent的版本比较老(1.4.15)。要测试网络性能时,需要考虑这一点...

网友评论

      本文标题:1.4.15 ThreeSum

      本文链接:https://www.haomeiwen.com/subject/shbajttx.html