java 393. UTF-8 Validation.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 393. UTF-8 Validation.java相关的知识,希望对你有一定的参考价值。

class Solution(object):
    def validUtf8(self, data):
        """
        :type data: List[int]
        :rtype: bool
        """
        if not data :
            return False
        count = 0
        for d in data :
            if count == 0:
                if (d & 0xF8) == 0xF0 :
                    count += 3
                elif (d & 0xF0) == 0xE0 :
                    count += 2
                elif (d & 0xE0) == 0xC0 :
                    count += 1
                elif (d & 0x80 != 0) :
                    return False
            else :
                if (d & 0xC0) != 0x80 :
                    return False
                count -= 1
        return count == 0
public class Solution {
    public boolean validUtf8(int[] data) {

        int count = 0;
        for (int d : data) {
            if(count == 0) {
                if((d & 0xF8) == 0xF0) count += 3;
                else if((d & 0xF0) == 0xE0) count += 2;
                else if((d & 0xE0) == 0xC0) count += 1;
                else if((d & 0x80) != 0) return false;
            } else {
                if((d & 0xC0) != 0x80) return false;
                count--;
            }
        }
        return count == 0;
    }
}

以上是关于java 393. UTF-8 Validation.java的主要内容,如果未能解决你的问题,请参考以下文章

java 393. UTF-8 Validation.java

java 393. UTF-8 Validation.java

java 393. UTF-8 Validation.java

java 393. UTF-8 Validation.java

393. UTF-8 Validation

393. UTF-8 编码验证