Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3bcec72

Browse files
Add files via upload
1 parent 50092f2 commit 3bcec72

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 从后向前,反向解决问题
2+
3+
// Runtime: 52 ms, faster than 92.07% of C++ online submissions for Shifting Letters.
4+
// Memory Usage: 11.7 MB, less than 75.00% of C++ online submissions for Shifting Letters.
5+
6+
class Solution
7+
{
8+
public:
9+
string shiftingLetters(string S, vector<int>& shifts)
10+
{
11+
string res = S;
12+
13+
int shift = 0;
14+
15+
for (int index = res.length() - 1; index >= 0; --index)
16+
{
17+
shift = (shift + shifts[index]) % 26;
18+
19+
int temp = res[index] - 'a';
20+
temp = (temp + shift) % 26;
21+
22+
res[index] = (temp + 'a');
23+
}
24+
25+
return res;
26+
}
27+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /