-
[LeetCode] 848. Shifting Letters 문제풀이Algorithm/문제풀이 2021. 9. 8. 16:41
https://leetcode.com/problems/shifting-letters/
Shifting Letters - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제 내용
문자를 shift 한 결과를 출력하는 문제
문제 풀이
어차피 다 shift 한 결과를 구하는 것이기 때문에
문자를 다 'a' 로 초기화 되있다고 생각하고 shift 값만 구하면 된다.
class Solution { public: string shiftingLetters(string s, vector<int>& shifts) { for (int i = shifts.size()-1; i > 0; --i) shifts[i-1] += shifts[i] % 26; for (int i = 0; i < s.length(); ++i) shifts[i] = (shifts[i] + s[i] - 'a') % 26; for (int i = 0; i < s.length(); ++i) s[i] = shifts[i] + 'a'; return s; } };
'Algorithm > 문제풀이' 카테고리의 다른 글
[Leetcode] 784. Letter Case Permutation 문제 풀이 (0) 2021.09.09 [LeetCode] 21. Merge Two Sorted Lists 문제풀이 (0) 2021.09.08 [LeetCode] 206. Reverse Linked List 문제 풀이 (0) 2021.09.08 [LeetCode] 994. Rotting Oranges 문제 풀이 (0) 2021.09.08 [LeetCode] 542. 01 Matrix 문제 풀이 (0) 2021.09.08