C# Solution


  • 0
    J

    Iterate over the array. There are 4 cases:

    1. Current num is 0 and the cur_sum > 0
    2. Current num is 0 and cur_sum = 0
    3. Current num is 1 and cur_sum > 0
    4. Current num is 1 and cur_sum is Zero
     public static bool IsOneBitCharacter(int[] bits)
            {
    			int i = 0;
                int cur_sum = 0;
                int prev_sum = 0;
    
                while (i < bits.Length)
                {
                    if (bits[i] == 0)
                    {
                        if (cur_sum > 0)
                        {
                            prev_sum = cur_sum;
                            cur_sum = 0;
                        }
                        else
                            prev_sum = 0;
                    }
                    else
                    {
                        if (cur_sum > 0)
                            cur_sum = 0;
                        else
                            cur_sum += 1;
                    }
    
                    i++;
                }
    
                return prev_sum == 0;
    		}
    			
    

Log in to reply
 

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