题解 | #两种排序方法#

两种排序方法

https://www.nowcoder.com/practice/839f681bf36c486fbcc5fcb977ffe432

import java.util.*;
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            int n = Integer.parseInt(str);
            String[] s = new String[n];
            for(int i = 0 ; i < n; i++) {
                s[i] = in.nextLine();
            }
            if(isOrderIex(s) && isOrderLen(s)) {
                System.out.println("both");
            } else if(isOrderIex(s)) {
                System.out.println("lexicographically");
            } else if(isOrderLen(s)) {
                System.out.println("lengths");
            } else {
                System.out.println("none");
            }
        }
    }
    public static boolean isOrderIex(String[] s) {
        for(int i = 0 ; i < s.length-1; i++) {
            if(s[i].compareTo(s[i+1]) > 0) {
                return false;
            }
        }
        return true;
    }
    public static boolean isOrderLen(String[] s) {
        for(int i = 0 ; i < s.length-1; i++) {
            if(s[i].length() > s[i+1].length()) {
                return false;
            }
        }
        return true;
    }
}

全部评论

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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