题解 | #比较版本号#
比较版本号
https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */ public int compare (string version1, string version2) { String[] s1 = version1.Split("."); String[] s2 = version2.Split("."); int l1 = s1.Length, l2 = s2.Length; int i = 0, j = 0; while(i < l1 || j < l2){ int num1 = 0, num2 = 0; if(i < l1) num1 = Int32.Parse(s1[i++]); if(j < l2) num2 = Int32.Parse(s2[j++]); if(num1 != num2) return num1 > num2 ? 1 : -1; } return 0; } }