848. Shifting Letters

class Solution { public: string shiftingLetters(string S, vector<int>& shifts) { vector<int> total; for (auto it = shifts.begin(); it != shifts.end(); it++){ int shift = 0; for (auto it2 = it; it2 != shifts.end(); it2++){ shift += *it2; } total.push_back(shift); } string res = ""; for (size_t i = 0; i < total.size(); i++){ char r = 'a' + (S[i] - 'a' + total[i]) % 26; res.push_back(r); } return res; } };
Version 1: There might be overflow. Note the range of integers given.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.