题解 | 左旋转字符串

左旋转字符串

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();
    }
}

全部评论

相关推荐

递归到脑子变傻:杭州还有上位机用VB的,实在没绷住
点赞 评论 收藏
分享
2025-12-18 11:59
广州南方学院 C++
牛客78682892...:直接点还好,总比要了简历也不回的强
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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