题解 | #字符串的分类#

字符串的分类

http://www.nowcoder.com/questionTerminal/32b53b1d8e4e46f8b71e8452788145b0

请问各位大佬,我这个为啥会数组越界

import java.util.*;

public class Main {

public static List<int[]> list = new ArrayList<>();

public static int ans = 0;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int n = scanner.nextInt();

    List<String> stringList = new ArrayList<>();

    for (int i = 0; i < n; i++) {
        String s = new String();
        s = scanner.next();
        if(s!=null){
            stringList.add(s);
        }
    }

    for (int i = 0; i < stringList.size(); i++) {
        toSure(stringList.get(i));
    }

    System.out.println(ans);

}

public static void toSure(String s){

    if(list.size()==0){
        int[] res = toArr(s);
        list.add(res);
        ans++;
    }
    else{
        int[] res = toArr(s);
        for (int i = 0; i < list.size(); i++) {
            int[] ints = list.get(i);
            if(ints.length!=res.length){
                ans++;
                break;
            }
            else{
                for (int j = 0; j < ints.length; j++) {
                    if(ints[j]!=res[j]){
                        ans++;
                        break;
                    }
                }
            }
        }
        list.add(res);
    }
}

public static int[] toArr(String s){
    char[] chars = s.toCharArray();

    int length = chars.length;

    int[] res = new int[length];

    for (int i = 0; i < chars.length; i++) {
        res[i] = chars[i];
    }

    Arrays.sort(res);

    return res;
}

}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务