Skip to content

Commit 35b369d

Browse files
authored
Update Maximum 69 Number.java
1 parent 9ea1fb1 commit 35b369d

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

Easy/Maximum 69 Number.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
class Solution {
2-
public int maximum69Number (int num) {
3-
int lastSixIndex = Integer.MAX_VALUE;
4-
int count = 0;
5-
int copy = num;
6-
while (copy > 0) {
7-
int rem = copy % 10;
8-
if (rem == 6) {
9-
lastSixIndex = count;
10-
}
11-
copy /= 10;
12-
count++;
2+
public int maximum69Number (int num) {
3+
String numString = String.valueOf(num);
4+
for (int i = 0; i < numString.length(); i++) {
5+
if (numString.charAt(i) == '6') {
6+
return Integer.parseInt(numString.substring(0, i) + '9' + numString.substring(i + 1));
7+
}
8+
}
9+
return num;
1310
}
14-
if (lastSixIndex == Integer.MAX_VALUE) {
15-
return num;
16-
}
17-
return (
18-
((num / ((int) Math.pow(10, lastSixIndex + 1))) * ((int) Math.pow(10, lastSixIndex + 1))) +
19-
(9 * ((int) Math.pow(10, lastSixIndex))) +
20-
(num % ((int) Math.pow(10, lastSixIndex)))
21-
);
22-
}
2311
}

0 commit comments

Comments
 (0)