题解 | 比较版本号
比较版本号
https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */ public int compare (String version1, String version2) { // write code here String[] vs1 = version1.split("\\."), vs2 = version2.split("\\."); int n1 = vs1.length, n2 = vs2.length, n = Math.max(n1, n2); for (int i = 0; i < n; i++) { int v1 = i < n1 ? Integer.parseInt(vs1[i]) : 0; int v2 = i < n2 ? Integer.parseInt(vs2[i]) : 0; if (v1 == v2) continue; return v1 < v2 ? -1 : 1; } return 0; } }
线性表基础 文章被收录于专栏
链表、递归、栈