Protocol Buffers encode non-negative integers as varints so that small values use fewer bytes. Each byte holds seven data bits and one continuation bit.
Design a ProtobufVarintEncoder class:
ProtobufVarintEncoder() creates a stateless encoder.int[] encode(int value) returns the protobuf varint bytes in transmission order.
Take seven-bit groups from least significant to most significant. Set bit 0x80 on every byte followed by another group. Leave that bit clear on the final byte.
Example 1:
Input:
Output:
Explanation: The low seven bits of 300 are 44. The first byte is 44 | 128 = 172. Shifting right by seven leaves 2, which is emitted without a continuation bit.
Example 2:
Input:
Output:
Explanation: Zero is represented by one terminating byte.
Constraints
0 <= value <= 2^31 - 1- Return byte values as integers in the range
0 through 255. - Every non-final byte must have its
0x80 bit set. - The final byte must have its
0x80 bit clear. - At most
100 calls are made to encode.