https://leetcode.com/problems/utf-8-validation/

public class Solution {
    public boolean validUtf8(int[] data) {
        int last = 0;
        for (int i=0; i<data.length; i++) {
            String str = Integer.toBinaryString(data[i]);
            if (last > 0) {
                if (str.length() != 8 || !str.startsWith("10")) {
                    return false;
                }
                last--;
            }
            else {
                if (str.length() > 8) {
                    return false;
                }
                if (str.length() == 8) {
                    int j=0;
                    for (; j<8; j++) {
                        if (str.charAt(j) != '1') {
                            break;
                        }
                    }
                    if (j > 1 && j <= 4) {
                        last = j - 1;
                    }
                    else {
                        return false;
                    }
                }
            }
        }
        if (last == 0) {
            return true;
        }
        else {
            return false;
        }
    }
}

 

相关文章:

  • 2021-12-01
  • 2021-04-07
  • 2022-12-23
  • 2021-07-11
  • 2021-06-24
猜你喜欢
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2021-09-28
  • 2021-07-15
相关资源
相似解决方案