Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Compare Version Numbers
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//'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; } }
[
+
]
Show input
Compilation time: 1.14 sec, absolute running time: 0.2 sec, cpu time: 0.15 sec, memory peak: 18 Mb, absolute service time: 1,35 sec
edit mode
|
history
|
discussion
-1