Skip to content

feat: add solutions to lc problem: No.1323 #4648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions solution/1300-1399/1323.Maximum 69 Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ tags:

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -114,15 +114,8 @@ public:

```go
func maximum69Number(num int) int {
s := strconv.Itoa(num)
nums := []byte(s)
for i, ch := range nums {
if ch == '6' {
nums[i] = '9'
break
}
}
ans, _ := strconv.Atoi(string(nums))
s := strings.Replace(strconv.Itoa(num), "6", "9", 1)
ans, _ := strconv.Atoi(s)
return ans
}
```
Expand Down Expand Up @@ -166,17 +159,17 @@ class Solution {

```c
int maximum69Number(int num) {
int n = 0;
int i = 0;
int t = num;
while (t) {
n++;
if (t % 10 == 6) {
i = n;
char buf[12];
sprintf(buf, "%d", num);
for (int i = 0; buf[i] != '\0'; i++) {
if (buf[i] == '6') {
buf[i] = '9';
break;
}
t /= 10;
}
return num + 3 * pow(10, i - 1);
int ans;
sscanf(buf, "%d", &ans);
return ans;
}
```

Expand Down
37 changes: 17 additions & 20 deletions solution/1300-1399/1323.Maximum 69 Number/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tags:
<pre>
<strong>Input:</strong> num = 9669
<strong>Output:</strong> 9969
<strong>Explanation:</strong>
<strong>Explanation:</strong>
Changing the first digit results in 6669.
Changing the second digit results in 9969.
Changing the third digit results in 9699.
Expand Down Expand Up @@ -67,7 +67,11 @@ The maximum number is 9969.

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy

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.

Time complexity $O(\log \textit{num})$, space complexity $O(\log \textit{num})$. Where $\textit{num}$ is the given integer.

<!-- tabs:start -->

Expand Down Expand Up @@ -111,15 +115,8 @@ public:

```go
func maximum69Number(num int) int {
s := strconv.Itoa(num)
nums := []byte(s)
for i, ch := range nums {
if ch == '6' {
nums[i] = '9'
break
}
}
ans, _ := strconv.Atoi(string(nums))
s := strings.Replace(strconv.Itoa(num), "6", "9", 1)
ans, _ := strconv.Atoi(s)
return ans
}
```
Expand Down Expand Up @@ -163,17 +160,17 @@ class Solution {

```c
int maximum69Number(int num) {
int n = 0;
int i = 0;
int t = num;
while (t) {
n++;
if (t % 10 == 6) {
i = n;
char buf[12];
sprintf(buf, "%d", num);
for (int i = 0; buf[i] != '\0'; i++) {
if (buf[i] == '6') {
buf[i] = '9';
break;
}
t /= 10;
}
return num + 3 * pow(10, i - 1);
int ans;
sscanf(buf, "%d", &ans);
return ans;
}
```

Expand Down
20 changes: 10 additions & 10 deletions solution/1300-1399/1323.Maximum 69 Number/Solution.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
int maximum69Number(int num) {
int n = 0;
int i = 0;
int t = num;
while (t) {
n++;
if (t % 10 == 6) {
i = n;
char buf[12];
sprintf(buf, "%d", num);
for (int i = 0; buf[i] != '\0'; i++) {
if (buf[i] == '6') {
buf[i] = '9';
break;
}
t /= 10;
}
return num + 3 * pow(10, i - 1);
}
int ans;
sscanf(buf, "%d", &ans);
return ans;
}
13 changes: 3 additions & 10 deletions solution/1300-1399/1323.Maximum 69 Number/Solution.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
func maximum69Number(num int) int {
s := strconv.Itoa(num)
nums := []byte(s)
for i, ch := range nums {
if ch == '6' {
nums[i] = '9'
break
}
}
ans, _ := strconv.Atoi(string(nums))
s := strings.Replace(strconv.Itoa(num), "6", "9", 1)
ans, _ := strconv.Atoi(s)
return ans
}
}