• leetcode Compare Version Numbers


    Compare two version numbers version1 and version1.
    If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.

    You may assume that the version strings are non-empty and contain only digits and the . character.
    The . character does not represent a decimal point and is used to separate number sequences.
    For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.

    Here is an example of version numbers ordering:

    0.1 < 1.1 < 1.2 < 13.37

    Credits:
    Special thanks to @ts for adding this problem and creating all test cases.

    题目还是简单的,但是要考虑很多情况。

    我写的代码边写边调试,写的比较烂。

     1 public class Solution {
     2     public int compareVersion(String version1, String version2) {
     3         String[] versionsOne=version1.split("\.");
     4         String[] versionsTwo=version2.split("\.");
     5         int oneLength=versionsOne.length;
     6         int twoLength=versionsTwo.length;
     7         int length=oneLength<twoLength?oneLength:twoLength;
     8         
     9         for (int i = 0; i < length; i++) {
    10             while (versionsOne[i].startsWith("0")&&versionsOne[i].length()>1) {
    11                 versionsOne[i]=versionsOne[i].substring(1);
    12             }
    13             while (versionsTwo[i].startsWith("0")&&versionsTwo[i].length()>1) {
    14                 versionsTwo[i]=versionsTwo[i].substring(1);
    15             }
    16             if (versionsOne[i].length()>versionsTwo[i].length()) {
    17                 return 1;
    18             }else if (versionsOne[i].length()<versionsTwo[i].length()) {
    19                 return -1;
    20             }
    21             int l = versionsOne[i].length()<versionsTwo[i].length()?versionsOne[i].length():versionsTwo[i].length();
    22             for (int j = 0; j < l; j++) {
    23                 int a=versionsOne[i].charAt(j);
    24                 int b=versionsTwo[i].charAt(j);
    25                 if (a>b) {
    26                     return 1;
    27                 }else if (a<b) {
    28                     return -1;
    29                 }
    30             }
    31             
    32         }
    33         if (oneLength>twoLength) {
    34             for (int i = twoLength; i < oneLength; i++) {
    35                 for (int j = 0; j < versionsOne[i].length(); j++) {
    36                     if (versionsOne[i].charAt(j)>'0') {
    37                         return 1;
    38                     }
    39                 }
    40             }
    41         }else if (oneLength<twoLength) {
    42             for (int i = oneLength; i < twoLength; i++) {
    43                 for (int j = 0; j < versionsTwo[i].length(); j++) {
    44                     if (versionsTwo[i].charAt(j)>'0') {
    45                         return -1;
    46                     }
    47                 }
    48             }
    49         }
    50         return 0;
    51     }
    52 }
  • 相关阅读:
    WinRAR5.01注册码附注册机
    PS不能存储,因为程序错误
    mysql中 date datetime time timestamp 的区别
    sublime text 3 3126 注册码+中文包
    IIS7.5 用 IIS AppPool应用程序池名 做账号 将各站点权限分开
    linux vi 报错 E37: No write since last change (add ! to override)
    Linux 安装 apache2.4.23
    三级分类及名称及列表
    二级栏目名称及列表
    每隔N行输出不同样式
  • 原文地址:https://www.cnblogs.com/birdhack/p/4168180.html
Copyright © 2020-2023  润新知