Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Compare Version Numbers
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; /* Input: version1 = "7.5.2.4", version2 = "7.5.3" Output: -1 Input: version1 = "1.0.1", version2 = "1" Output: 1 */ class Rextester { public static void main(String args[]) { System.out.println(compareVersion("1.2.1","1.3.5")); } public static int compareVersion(String version1, String version2) { String[] one = version1.split("\\."); String[] two = version2.split("\\."); int index=0; while(index<one.length && index<two.length){ if(Integer.parseInt(one[index])>Integer.parseInt(two[index])) return 1; else if(Integer.parseInt(one[index])<Integer.parseInt(two[index])) return -1; else index++; } while(index<one.length){ if(Integer.parseInt(one[index])>0) return 1; index++; } while(index<two.length){ if(Integer.parseInt(two[index])>0) return -1; index++; } return 0; } }
run
|
edit
|
history
|
help
0
MyBirdClass
LRU cache - Simple solution (costly)
Rotate matrix clockwise
data
1 to 100 except 30 to40
l
switching light bulbs
Java consecutive repeated cha
piglatin
bookstore