File tree 1 file changed +8
-20
lines changed
1 file changed +8
-20
lines changed Original file line number Diff line number Diff line change 1
1
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 ;
13
10
}
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
- }
23
11
}
You can’t perform that action at this time.
0 commit comments