剑指offer58-II 左旋转字符串

抄自LeetCode解析
方法一:切片

class Solution {
     
    public String reverseLeftWords(String s, int n) {
     
        return s.substring(n, s.length()) + s.substring(0, n);
    }
}

方法二:stringbuilder

class Solution {
     
    public String reverseLeftWords(String s, int n) {
     
        StringBuilder res = new StringBuilder();
        for(int i = n; i < s.length(); i++)
            res.append(s.charAt(i));
        for(int i = 0; i < n; i++)
            res.append(s.charAt(i));
        return res.toString();
    }
}

方法三:字符串遍历拼接

class Solution {
     
    public String reverseLeftWords(String s, int n) {
     
        String res = "";
        for(int i = n; i < s.length(); i++)
            res += s.charAt(i);
        for(int i = 0; i < n; i++)
            res += s.charAt(i);
        return res;
    }
}

作者:jyd
链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/solution/mian-shi-ti-58-ii-zuo-xuan-zhuan-zi-fu-chuan-qie-p/
来源:力扣(LeetCode)。

你可能感兴趣的:(剑指offer,字符串)