Test case [248,128,128,128] should fail my submitted codes but it did not


  • 0
    B

    When I ran customized test case [248,128,128,128], my submitted codes return true while expected answer is false. I agree with it. The confusing part is if I submit my codes, it is accepted by OJ, which I think incorrect.


  • 0
    S

    I think there doesn't exist test case which leading 1 more than 4.
    my code

    public class Solution {
        public boolean validUtf8(int[] data) {
            if (data == null || data.length == 0){
                return false;
            }
            int pos = 0;
            while (pos < data.length){
                int cur = data[pos];
                int shift = 7;
                int count = 0;
                int ref = 128;
                while (((cur & ref) >>> shift) == 1){
                    shift--;
                    count++;
                    ref = ref >>> 1;
                }
                if (count == 1 || shift < 3 || data.length - pos < count - 1){
                    return false;
                }
                pos++;
                for (int i = 0; i < count - 1; i++){
                    if ((data[pos] & 128) != 128){
                        return false;
                    }
                    pos++;
                }
            }
            return true;
        }
    }
    

    i have tried remove this condition (shift < 3) but still accept.
    it'd better add more test cases. :)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.