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
Parse and flatten string
Street light
191fa07131
Sample ArrayList
PE #9
// Java Coding Challenge - 09: Find out duplicate numbers using HashSet
This code is not what it seems...
191fa07050
Write a program to sort the odd elements descending order and even elements in ascending order
Basic LinkedList in Java