Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed Feb 20, 2023
1 parent 84b8d67 commit 141b9e4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
21 changes: 7 additions & 14 deletions leetcode/biweekly/98/a/README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
把 $\textit{num}$ 转成字符串,从左到右找第一个不是 $9$ 的字符,替换成 $9$ 得到最大数;同理找第一个不是 $0$ 的字符,替换成 $0$ 得到最小数。
把 $\textit{num}$ 转成字符串 $s$,从左到右找第一个不是 $9$ 的字符,把这个字符都替换成 $9$,得到最大数。

同理找第一个不是 $0$ 的字符,替换成 $0$ 得到最小数,由于 $s[0]$ 一定不是 $0$,所以替换它就行。

附:[视频讲解](https://www.bilibili.com/video/BV15D4y1G7ms/)

```py [sol1-Python3]
class Solution:
def minMaxDifference(self, num: int) -> int:
mx = mn = num
mx = num
s = str(num)
for c in s:
if c != '9':
mx = int(s.replace(c, '9'))
break
for c in s:
if c != '0':
mn = int(s.replace(c, '0'))
break
return mx - mn
return mx - int(s.replace(s[0], '0'))
```

```go [sol1-Go]
func minMaxDifference(num int) int {
mx, mn := num, num
mx := num
s := strconv.Itoa(num)
for _, c := range s {
if c != '9' {
mx, _ = strconv.Atoi(strings.ReplaceAll(s, string(c), "9"))
break
}
}
for _, c := range s {
if c != '0' {
mn, _ = strconv.Atoi(strings.ReplaceAll(s, string(c), "0"))
break
}
}
mn, _ := strconv.Atoi(strings.ReplaceAll(s, s[:1], "0"))
return mx - mn
}
```
Expand Down
9 changes: 2 additions & 7 deletions leetcode/biweekly/98/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ import (

// https://space.bilibili.com/206214
func minMaxDifference(num int) int {
mx, mn := num, num
mx := num
s := strconv.Itoa(num)
for _, c := range s {
if c != '9' {
mx, _ = strconv.Atoi(strings.ReplaceAll(s, string(c), "9"))
break
}
}
for _, c := range s {
if c != '0' {
mn, _ = strconv.Atoi(strings.ReplaceAll(s, string(c), "0"))
break
}
}
mn, _ := strconv.Atoi(strings.ReplaceAll(s, s[:1], "0"))
return mx - mn
}

0 comments on commit 141b9e4

Please sign in to comment.