题解 | #两种排序方法#

两种排序方法

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

import java.util.*;

public class Main {
    public static boolean isDicSort(String[] str) {
        for (int i = 0; i < str.length - 1; i++) {
            if(str[i].compareTo(str[i + 1]) > 0){
                return false;
            }
        }
        return true;
    }
    public static boolean isLenSort(String[] str) {
        for (int i = 0; i < str.length - 1; i++) {
            if (str[i].length() > str[i + 1].length()) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNext()) {
            int n = in.nextInt();
            String[] str = new String[n];
            for (int i = 0; i < n; i++) {
                str[i] = in.next();
            }
            boolean len = isLenSort(str);
            boolean dic = isDicSort(str);
            
            if(len && dic){
                System.out.println("both");
            } else if (len) {
                System.out.println("lengths");
            } else if(dic) {
                System.out.println("lexicographically");
            } else {
                System.out.println("none");
            }
        
        }
    }
}

全部评论

相关推荐

投递美团等公司10个岗位
点赞 评论 收藏
转发
2 1 评论
分享
牛客网
牛客企业服务