FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-algorithms/src/CompareVersionNumbers.java at master · anishLearnsToCode/leetcode-algorithms · GitHub
anishLearnsToCode
/
leetcode-algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
17
Star
98
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-algorithms
/
src
/
CompareVersionNumbers.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
37 lines (32 loc) · 1.24 KB
Breadcrumbs
leetcode-algorithms
/
src
/
CompareVersionNumbers.java
Copy path
File metadata and controls
37 lines (32 loc) · 1.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// https://leetcode.com/problems/compare-version-numbers
// T: O(|v1| + |v2|)
// S: O(v1 + v2)
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
CompareVersionNumbers
{
public
static
int
compareVersion
(
String
version1
,
String
version2
) {
final
List
<
Integer
>
revisions1
=
getRevisions
(
version1
);
final
List
<
Integer
>
revisions2
=
getRevisions
(
version2
);
return
compareVersion
(
revisions1
,
revisions2
);
}
public
static
int
compareVersion
(
List
<
Integer
>
version1
,
List
<
Integer
>
version2
) {
int
length
=
Math
.
max
(
version1
.
size
(),
version2
.
size
());
for
(
int
i
=
0
;
i
<
length
;
i
++){
if
(
get
(
version1
,
i
) <
get
(
version2
,
i
))
return
-
1
;
else
if
(
get
(
version1
,
i
) >
get
(
version2
,
i
))
return
1
;
}
return
0
;
}
private
static
int
get
(
List
<
Integer
>
list
,
int
index
) {
if
(
index
>=
list
.
size
())
return
0
;
return
list
.
get
(
index
);
}
private
static
List
<
Integer
>
getRevisions
(
String
version
) {
final
String
[]
revisions
=
version
.
split
(
"
\\
."
);
final
List
<
Integer
>
result
=
new
ArrayList
<>();
for
(
String
revision
:
revisions
) {
result
.
add
(
Integer
.
parseInt
(
revision
));
}
return
result
;
}
}
Back
|
FazBrowse Home
|
New Git URL