Compare Numbers

比较两个数大小。。
本来是打算比版本号的。。结果读错题 了。

if (!version1.matches("^\\d+\\.\\d+$")
                || !version1.matches("^\\d+\\.\\d+$")){
            return 0;
        }

然后第二个就是转化。其实我们没必要将两个数都转化出来然后再去比, 可以实时的去比。根据小数点的位置可以判断从第几位开始比。
初步方案:

public class Calu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println(compareVersion("1.1", "1.2"));
        System.out.println(compareVersion("0", "1.2"));
        System.out.println(compareVersion("1.1", "1.1.1"));
        System.out.println(compareVersion("1.1", "12.2"));
        System.out.println(compareVersion("123.1", "1.2"));

    }

    public static int compareVersion(String version1, String version2) {
        if (!version1.matches("^\\d+\\.\\d+$")
                & !version2.matches("^\\d+\\.\\d+$")) {
            return 0;
        }
        int V1_point = findpoint(version1);
        int V2_point = findpoint(version2);
        if (V1_point > V2_point)
            return 1;
        else if (V1_point < V2_point)
            return -1;
        else
            for (int i = 0; i < version1.length() & i < version2.length(); i++) {
                if (version1.charAt(i) < version2.charAt(i)) {
                    return -1;
                } else if (version1.charAt(i) > version2.charAt(i)) {
                    return 1;
                }
            }
                if(version1.length()>version2.length())
                return 1;
            else if(version1.length()<version2.length())
                return -1;
        return 0;

    }

    public static int findpoint(String ver) {
        for (int i = 0; i < ver.length(); i++) {
            if (ver.charAt(i) == '.')
                return i;
        }
        return 0;
    }
}

第一次测试结果:好的。整数也行–
Input: “1”, “0”
Output: 0
Expected: 1
正则表达式扩展为:^\d+\.\d+|\d+$

第二次
Input: “01”, “1”
Output: -1
Expected: 0

public class Calu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        long startTime = System.nanoTime(); // 获取开始时间
        System.out.println(compareVersion("1.2", "1.10"));
        System.out.println(compareVersion("1.1", "1.0"));
        System.out.println(compareVersion("01", "1"));
        System.out.println(compareVersion("1", "0"));
        System.out.println(compareVersion("0", "1"));
        System.out.println(compareVersion("1.1", "1.2"));
        System.out.println(compareVersion("0", "1.2"));
        System.out.println(compareVersion("1.1", "1.1.1"));
        System.out.println(compareVersion("1.1", "12.2"));
        System.out.println(compareVersion("123.1", "1.2"));
        System.out.println(compareVersion("123", "1"));
        System.out.println(compareVersion("123", "12.4"));
        long endTime = System.nanoTime(); // 获取结束时间
        System.out.println("程序运行时间: " + (endTime - startTime) + "ns");

        long startTime2 = System.nanoTime(); // 获取开始时间
        System.out.println(compareVersion2("1.2", "1.10"));
        System.out.println(compareVersion2("1.1", "1.0"));
        System.out.println(compareVersion2("01", "1"));
        System.out.println(compareVersion2("1", "0"));
        System.out.println(compareVersion2("0", "1"));
        System.out.println(compareVersion2("1.1", "1.2"));
        System.out.println(compareVersion2("0", "1.2"));
        System.out.println(compareVersion2("1.1", "1.1.1"));
        System.out.println(compareVersion2("1.1", "12.2"));
        System.out.println(compareVersion2("123.1", "1.2"));
        System.out.println(compareVersion2("123", "1"));
        System.out.println(compareVersion2("123", "12.4"));
        long endTime2 = System.nanoTime(); // 获取结束时间
        System.out.println("程序运行时间: " + (endTime2 - startTime2) + "ns");
    }

    public static int compareVersion2(String version1, String version2) {
        double v1=0;
        double v2=0;
        try {
             v1 = Double.parseDouble(version1);
             v2 = Double.parseDouble(version2);
        } catch (Exception e) {
            return 0;
        }
        if (v1 > v2)
            return 1;
        else if (v2 > v1)
            return -1;
        return 0;

    }

    public static int compareVersion(String version1, String version2) {
        String regex = "^\\d+\\.\\d+|\\d+$";
        if (!version1.matches(regex) || !version2.matches(regex)) {
            return 0;
        }
        int V1_point = findpoint(version1);
        int V2_point = findpoint(version2);
        if (V1_point > V2_point) {
            return 1;
        } else if (V1_point < V2_point) {
            return -1;
        } else {
            int v1_len = Length(version1);
            int v2_len = Length(version2);
            boolean tempv1 = true;
            boolean tempv2 = true;
            for (int i = 0, j = 0; i < v1_len && i < v2_len; i++, j++) {
                if (version1.charAt(i) == '0' && tempv1) {
                    i++;
                    continue;
                }
                tempv1 = false;
                if (version2.charAt(j) == '0' && tempv2) {
                    j++;
                    continue;
                }
                tempv2 = false;
                if (version1.charAt(i) < version2.charAt(j)) {
                    return -1;
                } else if (version1.charAt(i) > version2.charAt(j)) {
                    return 1;
                }
            }
            if (v1_len > v2_len)
                return 1;
            else if (v1_len < v2_len) {
                return -1;
            }

        }
        return 0;

    }

    public static int findpoint(String ver) {
        int t = 0;
        for (int i = 0; i < ver.length(); i++) {
            if (ver.charAt(i) == '0')
                t++;
            else if (ver.charAt(i) == '.')
                return i - t;
        }
        return ver.length() - t;
    }

    public static int Length(String ver) {
        int result = 0;
        for (int i = 0; i < ver.length() && ver.charAt(i) == '0'; i++) {
            result++;
        }
        return ver.length() - result;
    }
}

这个运行效率不高啊。。
1
1
0
1
-1
-1
-1
0
-1
1
1
1
程序运行时间: 2467802ns
1
1
0
1
-1
-1
-1
0
-1
1
1
1
程序运行时间: 873143ns

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值