JAVA bit manipulation, minimum hard coding


  • 0
    C
    public class Solution {
        public boolean validUtf8(int[] data)
        {
            int maxPow = (int) Math.pow(2, 7), i = 0;
            while(i < data.length)
            {
                if((data[i] & maxPow) == 0)
                {
                    i++; continue;
                }
                int no = maxPow, j = 0;
                while(((data[i] & no) == no) && j <= 4)
                {
                    j++;
                    no = no >> 1;
                }
                if(j == 1) return false;
                j--; i++;
                while(i < data.length && j > 0 && ((data[i] & maxPow) == maxPow))
                {
                    j--; i++;
                }
                if(j > 0) return false;
            }
            return true;
        }
    }

Log in to reply
 

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