EasyFoundational
Convert a Non-Negative Integer to Binary
- Time limit
- 1 s
- Memory limit
- 128 MB
- Estimated
- 15 min
- Bit Manipulation
Description
Given a non-negative integer n, return its binary representation as a string.
The binary representation uses only the characters 0 and 1, with no leading zeroes unless the number itself is zero. Therefore, the binary representation of 0 is exactly "0".
For example, decimal 10 is "1010" in binary, and decimal 7 is "111".
Do not include a prefix such as 0b in the returned string.
Implement toBinary(n).
Constraints
0 <= n <= 1000000000
Sample cases
3Sample 1
Input
n = 10
Expected output
"1010"
Sample 2
Input
n = 7
Expected output
"111"
Sample 3
Skills and languages
Concepts
- Binary Representation and Bitwise Operators
- Number Bases and Digit Manipulation
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer