字典序、长度序

两种排序方法

http://www.nowcoder.com/questionTerminal/839f681bf36c486fbcc5fcb977ffe432

集合类容器比较是否相等时,比较的是内部的值,而不是这两个容器的句柄(引用):

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        List<String> v = new ArrayList<>();
        for(int i = 0; i < n; ++i) v.add(sc.nextLine());
        List<String> p = new ArrayList<>(v);
        Collections.sort(p);
        boolean a = p.equals(v);    //集合类容器比较是否相等
        boolean b = true;
        for(int i = 0; i < v.size() - 1; ++i){
            if(v.get(i).length() > v.get(i + 1).length()){
                b = false;
                break;
            }
        }
        if(a && b){
            System.out.println("both");
            return;
        }
        if(a){
            System.out.println("lexicographically");
            return;
        }
        if(b){
            System.out.println("lengths");
            return;
        }
        System.out.println("none");
    }
}
全部评论

相关推荐

7 收藏 评论
分享
牛客网
牛客企业服务