Leetcode - / Binary
67. Add Binary[E]
https://leetcode.com/problems/add-binary/
Description
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example 1:
1 | Input: a = "11", b = "1" |
Example 2:
1 | Input: a = "1010", b = "1011" |
Constraints:
- Each string consists only of
'0'or'1'characters. 1 <= a.length, b.length <= 10^4- Each string is either
"0"or doesn’t contain any leading zero.
Solution
1 | class Solution: |
401. Binary Watch[E]
https://leetcode.com/problems/binary-watch/
Description
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
Each LED represents a zero or one, with the least significant bit on the right.

For example, the above binary watch reads “3:25”.
Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
Example:
1 | Input: n = 1 |
Note:
- The order of output does not matter.
- The hour must not contain a leading zero, for example “01:00” is not valid, it should be “1:00”.
- The minute must be consist of two digits and may contain a leading zero, for example “10:2” is not valid, it should be “10:02”.
Solution
1 |
89. Gray Code[M]
https://leetcode.com/problems/gray-code/
Description
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
Example 1:
1 | Input: 2 |
Example 2:
1 | Input: 0 |
Solution
1 | class Solution: |
338. Counting Bits[]
https://leetcode.com/problems/counting-bits/
Description
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.
Example 1:
1 | Input: 2 |
Example 2:
1 | Input: 5 |
Follow up:
- It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
- Space complexity should be O(n).
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
Solution
1 | class Solution: |






