LeetCode 848 题解

https://leetcode.com/problems/shifting-letters/description/

题目大意:一个字符串,给你一个向右移动的步数,问移动之后的字符串长什么样子。

解题思路:模拟

class Solution {
    public String shiftingLetters(String S, int[] shifts) {
        StringBuilder s = new StringBuilder(S);
        int n= shifts.length;
        int pre = 0;
        for(int i=n-1;i>=0;i--)
        {
            pre = (pre+shifts[i]%26) %26;
            int tmp = (s.charAt(i)-'a' +pre) %26;
            s.setCharAt(i,(char)(tmp+'a'));
        }
        return s.toString();
    }
}

猜你喜欢

转载自blog.csdn.net/u011439455/article/details/80642138