Skip to content

Commit bb5e2e6

Browse files
authored
feat: add solutions to lc problem: No.1323 (doocs#4648)
No.1323.Maximum 69 Number
1 parent 6c3f9f5 commit bb5e2e6

File tree

4 files changed

+42
-59
lines changed

4 files changed

+42
-59
lines changed

solution/1300-1399/1323.Maximum 69 Number/README.md

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ tags:
7070

7171
我们将数组转换为字符串,然后从左到右遍历字符串,找到第一个出现的 $6$,将其替换为 $9$,然后返回转换后的字符串对应的整数即可。
7272

73-
时间复杂度 $O(\log num)$,空间复杂度 $O(\log num)$。其中 $num$ 为给定的整数。
73+
时间复杂度 $O(\log \textit{num})$,空间复杂度 $O(\log \textit{num})$。其中 $\textit{num}$ 为给定的整数。
7474

7575
<!-- tabs:start -->
7676

@@ -114,15 +114,8 @@ public:
114114
115115
```go
116116
func maximum69Number(num int) int {
117-
s := strconv.Itoa(num)
118-
nums := []byte(s)
119-
for i, ch := range nums {
120-
if ch == '6' {
121-
nums[i] = '9'
122-
break
123-
}
124-
}
125-
ans, _ := strconv.Atoi(string(nums))
117+
s := strings.Replace(strconv.Itoa(num), "6", "9", 1)
118+
ans, _ := strconv.Atoi(s)
126119
return ans
127120
}
128121
```
@@ -166,17 +159,17 @@ class Solution {
166159

167160
```c
168161
int maximum69Number(int num) {
169-
int n = 0;
170-
int i = 0;
171-
int t = num;
172-
while (t) {
173-
n++;
174-
if (t % 10 == 6) {
175-
i = n;
162+
char buf[12];
163+
sprintf(buf, "%d", num);
164+
for (int i = 0; buf[i] != '\0'; i++) {
165+
if (buf[i] == '6') {
166+
buf[i] = '9';
167+
break;
176168
}
177-
t /= 10;
178169
}
179-
return num + 3 * pow(10, i - 1);
170+
int ans;
171+
sscanf(buf, "%d", &ans);
172+
return ans;
180173
}
181174
```
182175

solution/1300-1399/1323.Maximum 69 Number/README_EN.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tags:
2929
<pre>
3030
<strong>Input:</strong> num = 9669
3131
<strong>Output:</strong> 9969
32-
<strong>Explanation:</strong>
32+
<strong>Explanation:</strong>
3333
Changing the first digit results in 6669.
3434
Changing the second digit results in 9969.
3535
Changing the third digit results in 9699.
@@ -67,7 +67,11 @@ The maximum number is 9969.
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Greedy
71+
72+
We convert the number to a string, then traverse the string from left to right to find the first occurrence of $6$, replace it with $9$, and then return the integer corresponding to the converted string.
73+
74+
Time complexity $O(\log \textit{num})$, space complexity $O(\log \textit{num})$. Where $\textit{num}$ is the given integer.
7175

7276
<!-- tabs:start -->
7377

@@ -111,15 +115,8 @@ public:
111115
112116
```go
113117
func maximum69Number(num int) int {
114-
s := strconv.Itoa(num)
115-
nums := []byte(s)
116-
for i, ch := range nums {
117-
if ch == '6' {
118-
nums[i] = '9'
119-
break
120-
}
121-
}
122-
ans, _ := strconv.Atoi(string(nums))
118+
s := strings.Replace(strconv.Itoa(num), "6", "9", 1)
119+
ans, _ := strconv.Atoi(s)
123120
return ans
124121
}
125122
```
@@ -163,17 +160,17 @@ class Solution {
163160

164161
```c
165162
int maximum69Number(int num) {
166-
int n = 0;
167-
int i = 0;
168-
int t = num;
169-
while (t) {
170-
n++;
171-
if (t % 10 == 6) {
172-
i = n;
163+
char buf[12];
164+
sprintf(buf, "%d", num);
165+
for (int i = 0; buf[i] != '\0'; i++) {
166+
if (buf[i] == '6') {
167+
buf[i] = '9';
168+
break;
173169
}
174-
t /= 10;
175170
}
176-
return num + 3 * pow(10, i - 1);
171+
int ans;
172+
sscanf(buf, "%d", &ans);
173+
return ans;
177174
}
178175
```
179176
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
int maximum69Number(int num) {
2-
int n = 0;
3-
int i = 0;
4-
int t = num;
5-
while (t) {
6-
n++;
7-
if (t % 10 == 6) {
8-
i = n;
2+
char buf[12];
3+
sprintf(buf, "%d", num);
4+
for (int i = 0; buf[i] != '\0'; i++) {
5+
if (buf[i] == '6') {
6+
buf[i] = '9';
7+
break;
98
}
10-
t /= 10;
119
}
12-
return num + 3 * pow(10, i - 1);
13-
}
10+
int ans;
11+
sscanf(buf, "%d", &ans);
12+
return ans;
13+
}
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
func maximum69Number(num int) int {
2-
s := strconv.Itoa(num)
3-
nums := []byte(s)
4-
for i, ch := range nums {
5-
if ch == '6' {
6-
nums[i] = '9'
7-
break
8-
}
9-
}
10-
ans, _ := strconv.Atoi(string(nums))
2+
s := strings.Replace(strconv.Itoa(num), "6", "9", 1)
3+
ans, _ := strconv.Atoi(s)
114
return ans
12-
}
5+
}

0 commit comments

Comments
 (0)