题解 | #两种排序方法#

两种排序方法

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

全部评论

相关推荐

明天不下雨了:我靠2022了都去字节了还什么读研我教你****:你好,本人985电子科大在读研一,本科西南大学(211)我在字节跳动实习过。对您的岗位很感兴趣,希望获得一次投递机会。
点赞 评论 收藏
分享
bLanK的小号:建议自己写一个比较新颖的项目,比如思维导图,在线文档,仿造postman,仿造一个组件库
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

更多
牛客网
牛客企业服务