[LeetCode 165] Compare Version Numbers (Medium)

Compare two version numbers version1 and version2.
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.

You may assume the default revision number for each level of a version number to be 0. For example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number. Its third and fourth level revision number are both 0.

Example 1:

Input: version1 = "0.1", version2 = "1.1"
Output: -1

Example 2:

Input: version1 = "1.0.1", version2 = "1"
Output: 1

Example 3:

Input: version1 = "7.5.2.4", version2 = "7.5.3"
Output: -1

Example 4:

Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both “01” and “001" represent the same number “1”

Example 5:

Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0"

Note:

  • Version strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes.
  • Version strings do not start or end with dots, and they will not be two consecutive dots.

Solution: Split the String using "\.", 再對每個subversion進(jìn)行比較

class Solution {
    public int compareVersion(String version1, String version2) {
        if (version1 == null || version1.length () == 0 || version2 == null || version2.length () == 0)
            return 0;
        
        String[] version1List = version1.split ("\\.");
        String[] version2List = version2.split ("\\.");
        
        // System.out.println (Arrays.toString (version1List));
        int len1 = version1List.length;
        int len2 = version2List.length;
        
        int len = Math.max (len1, len2);
        int i = 0;
        
        for (; i < len; i++) {
            int ver1 = i < len1 ? Integer.valueOf (version1List[i]) : 0;
            int ver2 = i < len2 ? Integer.valueOf (version2List[i]) : 0;
            
            // System.out.println (ver1 + " " + ver2);
            
            if (ver1 < ver2) {
                return -1;
            } else if (ver1 > ver2) {
                return 1;
            }
        }
        
        return 0;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子谈况,更是在濱河造成了極大的恐慌,老刑警劉巖冬殃,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異叁怪,居然都是意外死亡审葬,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門骂束,熙熙樓的掌柜王于貴愁眉苦臉地迎上來耳璧,“玉大人,你說我怎么就攤上這事展箱≈伎荩” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵混驰,是天一觀的道長攀隔。 經(jīng)常有香客問我,道長栖榨,這世上最難降的妖魔是什么昆汹? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮婴栽,結(jié)果婚禮上满粗,老公的妹妹穿的比我還像新娘。我一直安慰自己愚争,他們只是感情好映皆,可當(dāng)我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著轰枝,像睡著了一般捅彻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鞍陨,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天步淹,我揣著相機與錄音,去河邊找鬼。 笑死缭裆,一個胖子當(dāng)著我的面吹牛键闺,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播幼驶,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼艾杏,長吁一口氣:“原來是場噩夢啊……” “哼韧衣!你這毒婦竟也來了盅藻?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤畅铭,失蹤者是張志新(化名)和其女友劉穎氏淑,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體硕噩,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡假残,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了炉擅。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辉懒。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖谍失,靈堂內(nèi)的尸體忽然破棺而出眶俩,到底是詐尸還是另有隱情,我是刑警寧澤快鱼,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布颠印,位于F島的核電站,受9級特大地震影響抹竹,放射性物質(zhì)發(fā)生泄漏线罕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一窃判、第九天 我趴在偏房一處隱蔽的房頂上張望钞楼。 院中可真熱鬧,春花似錦袄琳、人聲如沸询件。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽雳殊。三九已至,卻和暖如春窗轩,著一層夾襖步出監(jiān)牢的瞬間夯秃,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留仓洼,地道東北人介陶。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像色建,于是被迫代替她去往敵國和親哺呜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評論 0 10
  • Compare two version numbers version1 and version2.If vers...
    煙雨醉塵緣閱讀 100評論 0 0
  • 跑步箕戳,每天5.20公里某残! 當(dāng)運動成為習(xí)慣時,心理忽然就少了很多恐懼陵吸!在跑出的那一刻玻墅,無比的放松!真的很感激...
    楽春水初生閱讀 353評論 0 0
  • 膚白如雪顏如玉 雙峰傲挺惹人迷 花庭通幽門緊閉 只等郎君來開啟 秀恩愛 溫馨甜蜜 懷抱起 如膠似漆 眼神里 滿是愛...
    天使狼閱讀 203評論 0 0
  • 兒子壮虫。有時間啦澳厢,客戶要兩個小時后才到,就地坐下和你聊會兒囚似。 先說《華山銘》: 一聲笛音開篇(或者蕭)剩拢,接下來對華山...
    姬乾瑋閱讀 229評論 0 1