Finding consecutive bit string of 1 or 0

前端 未结 10 697
刺人心
刺人心 2020-11-29 05:27

How to find the length of the longest consecutive bit string(either 1 or 0)?

00000000 11110000 00000000 00000000 -> If it is 0 then length will be 20

11111

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 06:18

    Posting from iPhone withbig fingers.

    If ones, then invert.

    Loop over the input using a leadz function. For each iteration, shift the input to the left. Continue until you reach the end of the input. Note that you need to compare the original input length with the cumulative leadz counts.

    Also, as an optimization, you can early abort when the remaining input length is less than the largest leadz you have seen.

    There are many fast leadz algorithms online.

提交回复
热议问题