Skip to content

Commit eda4953

Browse files
committed
Add solution 0791
1 parent 41b0383 commit eda4953

File tree

7 files changed

+180
-5
lines changed

7 files changed

+180
-5
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
func customSortString(order string, str string) string {
6+
magic := map[byte]int{}
7+
for i := range order {
8+
magic[order[i]] = i - 30
9+
}
10+
byteSlice := []byte(str)
11+
sort.Slice(byteSlice, func(i, j int) bool {
12+
return magic[byteSlice[i]] < magic[byteSlice[j]]
13+
})
14+
return string(byteSlice)
15+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question791 struct {
9+
para791
10+
ans791
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para791 struct {
16+
order string
17+
str string
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans791 struct {
23+
one string
24+
}
25+
26+
func Test_Problem791(t *testing.T) {
27+
28+
qs := []question791{
29+
30+
{
31+
para791{"cba", "abcd"},
32+
ans791{"cbad"},
33+
},
34+
}
35+
36+
fmt.Printf("------------------------Leetcode Problem 791------------------------\n")
37+
38+
for _, q := range qs {
39+
_, p := q.ans791, q.para791
40+
fmt.Printf("【input】:%v 【output】:%v\n", p, customSortString(p.order, p.str))
41+
}
42+
fmt.Printf("\n\n\n")
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [791. Custom Sort String](https://leetcode.com/problems/custom-sort-string/)
2+
3+
4+
## 题目
5+
6+
`order` and `str` are strings composed of lowercase letters. In `order`, no letter occurs more than once.
7+
8+
`order` was sorted in some custom order previously. We want to permute the characters of `str` so that they match the order that `order` was sorted. More specifically, if `x` occurs before `y` in `order`, then `x` should occur before `y` in the returned string.
9+
10+
Return any permutation of `str` (as a string) that satisfies this property.
11+
12+
```
13+
Example:Input:
14+
order = "cba"
15+
str = "abcd"
16+
Output: "cbad"
17+
Explanation:
18+
"a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".
19+
Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.
20+
21+
```
22+
23+
**Note:**
24+
25+
- `order` has length at most `26`, and no character is repeated in `order`.
26+
- `str` has length at most `200`.
27+
- `order` and `str` consist of lowercase letters only.
28+
29+
## 题目大意
30+
31+
字符串 S 和 T 只包含小写字符。在 S 中,所有字符只会出现一次。S 已经根据某种规则进行了排序。我们要根据 S 中的字符顺序对 T 进行排序。更具体地说,如果 S 中 x 在 y 之前出现,那么返回的字符串中 x 也应出现在 y 之前。返回任意一种符合条件的字符串 T。
32+
33+
## 解题思路
34+
35+
- 题目只要求 T 中包含 S 的字符串有序,所以可以先将 T 中包含 S 的字符串排好序,然后再拼接上其他字符。S 字符串最长为 26 位,先将 S 中字符的下标向左偏移 30,并将偏移后的下标值存入字典中。再把 T 字符串按照字典中下标值进行排序。S 中出现的字符对应的下标经过处理以后变成了负数,S 中未出现的字符的下标还是正数。所以经过排序以后,S 中出现的字符按照原有顺序排列在前面,S 中未出现的字符依次排在后面。
36+
37+
## 代码
38+
39+
```go
40+
package leetcode
41+
42+
import "sort"
43+
44+
func customSortString(order string, str string) string {
45+
magic := map[byte]int{}
46+
for i := range order {
47+
magic[order[i]] = i - 30
48+
}
49+
byteSlice := []byte(str)
50+
sort.Slice(byteSlice, func(i, j int) bool {
51+
return magic[byteSlice[i]] < magic[byteSlice[j]]
52+
})
53+
return string(byteSlice)
54+
}
55+
```

leetcode/1846.Maximum-Element-After-Decreasing-and-Rearranging/1846. Maximum Element After Decreasing and Rearranging.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package leetcode
22

33
func maximumElementAfterDecrementingAndRearranging(arr []int) int {
44
n := len(arr)
5-
cnt := make([]int, n+1)
5+
count := make([]int, n+1)
66
for _, v := range arr {
7-
cnt[min(v, n)]++
7+
count[min(v, n)]++
88
}
99
miss := 0
10-
for _, c := range cnt[1:] {
10+
for _, c := range count[1:] {
1111
if c == 0 {
1212
miss++
1313
} else {

website/content/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,5 @@ func (a SortByFraction) Less(i, j int) bool {
122122
----------------------------------------------
123123
<div style="display: flex;justify-content: space-between;align-items: center;">
124124
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0785.Is-Graph-Bipartite/">⬅️上一页</a></p>
125-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0792.Number-of-Matching-Subsequences/">下一页➡️</a></p>
125+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0791.Custom-Sort-String/">下一页➡️</a></p>
126126
</div>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# [791. Custom Sort String](https://leetcode.com/problems/custom-sort-string/)
2+
3+
4+
## 题目
5+
6+
`order` and `str` are strings composed of lowercase letters. In `order`, no letter occurs more than once.
7+
8+
`order` was sorted in some custom order previously. We want to permute the characters of `str` so that they match the order that `order` was sorted. More specifically, if `x` occurs before `y` in `order`, then `x` should occur before `y` in the returned string.
9+
10+
Return any permutation of `str` (as a string) that satisfies this property.
11+
12+
```
13+
Example:Input:
14+
order = "cba"
15+
str = "abcd"
16+
Output: "cbad"
17+
Explanation:
18+
"a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".
19+
Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.
20+
21+
```
22+
23+
**Note:**
24+
25+
- `order` has length at most `26`, and no character is repeated in `order`.
26+
- `str` has length at most `200`.
27+
- `order` and `str` consist of lowercase letters only.
28+
29+
## 题目大意
30+
31+
字符串 S 和 T 只包含小写字符。在 S 中,所有字符只会出现一次。S 已经根据某种规则进行了排序。我们要根据 S 中的字符顺序对 T 进行排序。更具体地说,如果 S 中 x 在 y 之前出现,那么返回的字符串中 x 也应出现在 y 之前。返回任意一种符合条件的字符串 T。
32+
33+
## 解题思路
34+
35+
- 题目只要求 T 中包含 S 的字符串有序,所以可以先将 T 中包含 S 的字符串排好序,然后再拼接上其他字符。S 字符串最长为 26 位,先将 S 中字符的下标向左偏移 30,并将偏移后的下标值存入字典中。再把 T 字符串按照字典中下标值进行排序。S 中出现的字符对应的下标经过处理以后变成了负数,S 中未出现的字符的下标还是正数。所以经过排序以后,S 中出现的字符按照原有顺序排列在前面,S 中未出现的字符依次排在后面。
36+
37+
## 代码
38+
39+
```go
40+
package leetcode
41+
42+
import "sort"
43+
44+
func customSortString(order string, str string) string {
45+
magic := map[byte]int{}
46+
for i := range order {
47+
magic[order[i]] = i - 30
48+
}
49+
byteSlice := []byte(str)
50+
sort.Slice(byteSlice, func(i, j int) bool {
51+
return magic[byteSlice[i]] < magic[byteSlice[j]]
52+
})
53+
return string(byteSlice)
54+
}
55+
```
56+
57+
58+
----------------------------------------------
59+
<div style="display: flex;justify-content: space-between;align-items: center;">
60+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction/">⬅️上一页</a></p>
61+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0792.Number-of-Matching-Subsequences/">下一页➡️</a></p>
62+
</div>

website/content/ChapterFour/0700~0799/0792.Number-of-Matching-Subsequences.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ func numMatchingSubseq(s string, words []string) int {
6868

6969
----------------------------------------------
7070
<div style="display: flex;justify-content: space-between;align-items: center;">
71-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction/">⬅️上一页</a></p>
71+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0791.Custom-Sort-String/">⬅️上一页</a></p>
7272
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function/">下一页➡️</a></p>
7373
</div>

0 commit comments

Comments
 (0)