848.Shifting Letters——weekly contest 87
848. Shifting Letters
题目链接:https://leetcode.com/problems/shifting-letters/description/
思路:O(N^2)复杂度过不了;先处理shifts, 从尾到头执行shifts[i] = sum(shifts[i+1]+...+shifts[n-1])。
注意点:会爆int,及时取余或用longlong。
1 string shiftingLetters(string S, vector<int>& shifts) { 2 int n = S.size(); 3 for(int i = n - 2; i >= 0 ; i--){ 4 shifts[i] += shifts[i+1]; 5 shifts[i] = shifts[i]%26; 6 } 7 for(int i = 0; i < n; i++){ 8 S[i] = (S[i]-'a'+shifts[i])%26 + 'a'; 9 } 10 return S; 11 }