Brute Force Java


  • 0
    M
                public boolean validUtf8(int[] data) {
                    if (data == null || data.length == 0) return false;
    		int n = data.length;
    		for (int i = 0; i < n; ++i) {
    			if ((data[i] & (1 << 7)) == 0) continue;
    			int d = data[i];
    			int k = 7;
    			while (k >= 0) {
    				if ((d & (1 << k)) == 0) break;
    				--k;
    			}
    			k = 7 - k;
    			if (k < 2 || k > 4) return false;
    			int j;
    			for (j = i + 1; j < i + k; ++j) {
    				if (j >= n || (data[j] & ((1 << 7) + (1 << 6))) != (1 << 7)) return false;
    			}
    			i = j - 1;
    		}
    		return true;
                }
    

Log in to reply
 

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