题解 | 单组_二维字符数组

单组_二维字符数组

https://www.nowcoder.com/practice/2316c1fedff14a8d91694a26c9da3310

import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = sc.nextInt();
        int m = sc.nextInt();
        sc.nextLine();
        List<String> strs = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String s = sc.nextLine();
            String reversed = new StringBuilder(s).reverse().toString();
            strs.add(reversed);
        }

        Collections.reverse(strs);
        for (String str : strs) {
            System.out.println(str);
        }

        // 或者 for (int i = n - 1; i >= 0; i--) {
        //          System.out.println(strs.get(i));
        //     }
    }
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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