UTF8这个其实不改也没有问题,可以将给出的数组看成是多个字符的编码,然后判断是否合法,一下我的代码,改之前和之后都可以AC

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        int N = Integer.parseInt(line);
        int code[] = new int[N];
        String[] split = in.nextLine().split(" ");
        for(int i = 0; i < N; i++) {
            code[i] = (Integer.parseInt(split[i]) & 255);
        }

        int index = 0;
        int ret = 1;
        while(index != N && ret == 1) {
            if(code[index] <= 127) {
                index++;
                continue;
            }
            if(code[index] >= 192 && code[index] <= 223) {
                if(!(code[index + 1] >= 128 && code[index + 1] <= 191))
                    ret = 0;
                index += 2;
                continue;
            }
            if(code[index] >= 224 && code[index] <= 239) {
                if(!(code[index + 1] >= 128 && code[index + 1] <= 191 && code[index + 2] >= 128 && code[index + 2] <= 191))
                    ret = 0;
                index += 3;
                continue;
            }
            if(code[index] >= 240 && code[index] <= 247) {
                if(!(code[index + 1] >= 128 && code[index + 1] <= 191 && code[index + 2] >= 128 && code[index + 2] <= 191 && code[index + 3] >= 128 && code[index + 3] <= 191))
                    ret = 0;
                index += 4;
                continue;
            }
            ret = 0;
        }
        System.out.println(ret);
    }
}