Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit541ddc7

Browse files
refactor 165
1 parentb1c02d8 commit541ddc7

File tree

1 file changed

+29
-43
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+29
-43
lines changed
Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,36 @@
11
packagecom.fishercoder.solutions;
2-
/**
3-
* 165. Compare Version Numbers
4-
5-
Compare two version numbers version1 and version2.
6-
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
7-
8-
You may assume that the version strings are non-empty and contain only digits and the . character.
9-
The . character does not represent a decimal point and is used to separate number sequences.
10-
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
11-
12-
Here is an example of version numbers ordering:
13-
14-
0.1 < 1.1 < 1.2 < 13.37
15-
*/
162

173
publicclass_165 {
18-
publicstaticclassSolution1 {
19-
publicintcompareVersion(Stringversion1,Stringversion2) {
20-
String[]v1s =version1.split(
21-
"\\.");//escaping it is very important! Otherwise, it's not going to work as expected!
22-
String[]v2s =version2.split("\\.");
23-
intlen = (v1s.length <v2s.length) ?v2s.length :v1s.length;
24-
for (inti =0;i <len;i++) {
25-
if (v1s.length ==i) {
26-
while (i <len) {
27-
if (Integer.parseInt(v2s[i]) >0) {
28-
return -1;
29-
}
30-
i++;
31-
}
32-
}elseif (v2s.length ==i) {
33-
while (i <len) {
34-
if (Integer.parseInt(v1s[i]) >0) {
35-
return1;
4+
publicstaticclassSolution1 {
5+
publicintcompareVersion(Stringversion1,Stringversion2) {
6+
String[]v1s =version1.split(
7+
"\\.");//escaping it is very important! Otherwise, it's not going to work as expected!
8+
String[]v2s =version2.split("\\.");
9+
intlen = (v1s.length <v2s.length) ?v2s.length :v1s.length;
10+
for (inti =0;i <len;i++) {
11+
if (v1s.length ==i) {
12+
while (i <len) {
13+
if (Integer.parseInt(v2s[i]) >0) {
14+
return -1;
15+
}
16+
i++;
17+
}
18+
}elseif (v2s.length ==i) {
19+
while (i <len) {
20+
if (Integer.parseInt(v1s[i]) >0) {
21+
return1;
22+
}
23+
i++;
24+
}
25+
}else {
26+
if (Integer.parseInt(v1s[i]) >Integer.parseInt(v2s[i])) {
27+
return1;
28+
}elseif (Integer.parseInt(v2s[i]) >Integer.parseInt(v1s[i])) {
29+
return -1;
30+
}
31+
}
3632
}
37-
i++;
38-
}
39-
}else {
40-
if (Integer.parseInt(v1s[i]) >Integer.parseInt(v2s[i])) {
41-
return1;
42-
}elseif (Integer.parseInt(v2s[i]) >Integer.parseInt(v1s[i])) {
43-
return -1;
44-
}
33+
return0;
4534
}
46-
}
47-
return0;
4835
}
49-
}
5036
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp