题解 | #比较版本号#
比较版本号
https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
using System; using System.Collections.Generic; using System.Numerics; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */ public int compare (string version1, string version2) { System.Console.WriteLine(version1); System.Console.WriteLine(version2); string[] token1 = version1.Split("."); string[] token2 = version2.Split("."); BigInteger result1 = 0; BigInteger result2 = 0; BigInteger num = 1000; for(int i = token1.Length; i>0; i--){ result1 += BigInteger.Parse(token1[i - 1]) * num; num = num*1000; } //num = 1000000000; for(int i = 0 ; i<token2.Length; i++){ num = num/1000; result2 += BigInteger.Parse(token2[i]) * num; } if(result1 < result2){ return -1; } else if(result1 > result2){ return 1; } else{ return 0; } } }