题解 | #左旋转字符串#

左旋转字符串

https://www.nowcoder.com/practice/12d959b108cb42b1ab72cef4d36af5ec

import java.util.Deque;
import java.util.LinkedList;
public class Solution {
    public String LeftRotateString(String str,int n) {
        if(str==null||str.equals(""))
            return "";
        char[] chars=str.toCharArray();
        Deque<Character> deque=new LinkedList();
        for(char c:chars)
            deque.offer(c);
        for(int i=0;i<n;i++){
            char c=deque.poll();
            deque.offer(c);
        }
        StringBuilder builder=new StringBuilder();
        while(!deque.isEmpty())
            builder.append(deque.poll());
        return builder.toString();
        
        
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务