Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 47f85d2

Browse files
aQuaaQua
aQua
authored and
aQua
committed
848 finish
1 parent b001b83 commit 47f85d2

File tree

4 files changed

+113
-9
lines changed

4 files changed

+113
-9
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [848. Shifting Letters](https://leetcode.com/problems/shifting-letters/)
2+
3+
## 题目
4+
5+
We have a string S of lowercase letters, and an integer array shifts.
6+
7+
Call the shift of a letter, the next letter in the alphabet, (wrapping around so that 'z' becomes 'a').
8+
9+
For example, shift('a') = 'b', shift('t') = 'u', and shift('z') = 'a'.
10+
11+
Now for each shifts[i] = x, we want to shift the first i+1letters of S, x times.
12+
13+
Return the final stringafter all such shifts to S are applied.
14+
15+
Example 1:
16+
17+
```text
18+
Input: S = "abc", shifts = [3,5,9]
19+
Output: "rpl"
20+
Explanation:
21+
We start with "abc".
22+
After shifting the first 1 letters of S by 3, we have "dbc".
23+
After shifting the first 2 letters of S by 5, we have "igc".
24+
After shifting the first 3 letters of S by 9, we have "rpl", the answer.
25+
```
26+
27+
Note:
28+
29+
1. 1 <= S.length = shifts.length <= 20000
30+
1. 0 <= shifts[i] <= 10 ^ 9
31+
32+
## 解题思路
33+
34+
见程序注释
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package problem0848
2+
3+
func shiftingLetters(S string, shifts []int) string {
4+
size := len(S)
5+
a := s2is(S)
6+
shift := 0
7+
for i := size - 1; 0 <= i; i-- {
8+
shift += shifts[i]
9+
shift %= 26
10+
a[i] = (a[i] + shift) % 26
11+
}
12+
return is2s(a)
13+
}
14+
15+
func s2is(s string) []int {
16+
res := make([]int, len(s))
17+
for i := range s {
18+
res[i] = int(s[i] - 'a')
19+
}
20+
return res
21+
}
22+
23+
func is2s(a []int) string {
24+
bs := make([]byte, len(a))
25+
for i, n := range a {
26+
bs[i] = byte(n + 'a')
27+
}
28+
return string(bs)
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package problem0848
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
S string
13+
shifts []int
14+
ans string
15+
}{
16+
17+
{
18+
"abc",
19+
[]int{3, 5, 9},
20+
"rpl",
21+
},
22+
23+
// 可以有多个 testcase
24+
}
25+
26+
func Test_shiftingLetters(t *testing.T) {
27+
ast := assert.New(t)
28+
29+
for _, tc := range tcs {
30+
fmt.Printf("~~%v~~\n", tc)
31+
ast.Equal(tc.ans, shiftingLetters(tc.S, tc.shifts), "输入:%v", tc)
32+
}
33+
}
34+
35+
func Benchmark_shiftingLetters(b *testing.B) {
36+
for i := 0; i < b.N; i++ {
37+
for _, tc := range tcs {
38+
shiftingLetters(tc.S, tc.shifts)
39+
}
40+
}
41+
}

leetcode.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 1019,
4-
"Updated": "2018-07-03T14:09:44.198054366+08:00",
3+
"Ranking": 997,
4+
"Updated": "2018-07-04T05:44:33.515334967+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 175,
@@ -1453,7 +1453,7 @@
14531453
"ID": 119,
14541454
"Title": "Pascal's Triangle II",
14551455
"TitleSlug": "pascals-triangle-ii",
1456-
"PassRate": "38%",
1456+
"PassRate": "39%",
14571457
"Difficulty": "Easy",
14581458
"IsAccepted": true,
14591459
"IsPaid": false,
@@ -4825,7 +4825,7 @@
48254825
"ID": 400,
48264826
"Title": "Nth Digit",
48274827
"TitleSlug": "nth-digit",
4828-
"PassRate": "30%",
4828+
"PassRate": "29%",
48294829
"Difficulty": "Easy",
48304830
"IsAccepted": true,
48314831
"IsPaid": false,
@@ -9865,7 +9865,7 @@
98659865
"ID": 820,
98669866
"Title": "Short Encoding of Words",
98679867
"TitleSlug": "short-encoding-of-words",
9868-
"PassRate": "42%",
9868+
"PassRate": "41%",
98699869
"Difficulty": "Medium",
98709870
"IsAccepted": true,
98719871
"IsPaid": false,
@@ -10033,7 +10033,7 @@
1003310033
"ID": 834,
1003410034
"Title": "Sum of Distances in Tree",
1003510035
"TitleSlug": "sum-of-distances-in-tree",
10036-
"PassRate": "32%",
10036+
"PassRate": "31%",
1003710037
"Difficulty": "Hard",
1003810038
"IsAccepted": true,
1003910039
"IsPaid": false,
@@ -10249,7 +10249,7 @@
1024910249
"ID": 852,
1025010250
"Title": "Peak Index in a Mountain Array",
1025110251
"TitleSlug": "peak-index-in-a-mountain-array",
10252-
"PassRate": "68%",
10252+
"PassRate": "69%",
1025310253
"Difficulty": "Easy",
1025410254
"IsAccepted": false,
1025510255
"IsPaid": false,
@@ -10369,7 +10369,7 @@
1036910369
"ID": 862,
1037010370
"Title": "Shortest Subarray with Sum at Least K",
1037110371
"TitleSlug": "shortest-subarray-with-sum-at-least-k",
10372-
"PassRate": "14%",
10372+
"PassRate": "15%",
1037310373
"Difficulty": "Hard",
1037410374
"IsAccepted": false,
1037510375
"IsPaid": false,
@@ -10393,7 +10393,7 @@
1039310393
"ID": 864,
1039410394
"Title": "Random Pick with Blacklist",
1039510395
"TitleSlug": "random-pick-with-blacklist",
10396-
"PassRate": "0%",
10396+
"PassRate": "26%",
1039710397
"Difficulty": "Hard",
1039810398
"IsAccepted": false,
1039910399
"IsPaid": false,

0 commit comments

Comments
 (0)