题解 | 左旋转字符串

左旋转字符串

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

import java.util.*;


public class Solution {
    // 循环左移
    // 思路:把所有的字符存入队列中,利用队列先进先出的性质来完成循环左移
    public String LeftRotateString (String str, int n) {
        // write code here
        System.out.println(str.length());
        if(str.length() == 0) {
            return "";
        }

        Queue<Character> queue = new LinkedList<>();
        for(int i = 0; i < str.length(); i++){
            queue.add(str.charAt(i));
        }
        for(int i = 0; i < n; i++){
            char c = queue.poll();
            queue.add(c);
        }
        StringBuilder res = new StringBuilder();
        while(!queue.isEmpty()){
            res.append(queue.poll());
        }
        return res.toString();
    }
}

全部评论

相关推荐

兄弟们,实习都是在接各种api,该怎么包装简历
仁者伍敌:感觉我自己做小项目也是各种api啊,我要怎么包装简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务