题解 | #句子逆序#

句子逆序

https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3

import java.util.Scanner;

/**
 * @author hll[yellowdradra@foxmail.com]
 * @since 2023-03-14 14:26
 **/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            recursive(in.nextLine());
            System.out.println();
        }
    }

    public static void recursive(String str) {
        int lastSpaceIndex = str.lastIndexOf(' ');
        if (lastSpaceIndex < 0) {
            System.out.print(str);
        } else {
            System.out.print(str.substring(lastSpaceIndex + 1, str.length()) + " ");
            recursive(str.substring(0, lastSpaceIndex));
        }
    }
}

全部评论

相关推荐

八极星:有什么不能问的,(/_\),这又不是多珍贵的机会,你有什么可失去的
点赞 评论 收藏
分享
昨天 22:36
门头沟学院 Java
投递运满满等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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