IP to CIDR — LeetCode 751 Python Solution
- Problem
- #751
- Pattern
- Bit Manipulation
- Reading time
- 2 min
- Source
- leetcode.com
The problem
An IP address is a formatted 32-bit unsigned integer where each group of 8 bits is printed as a decimal number and the dot character '.' splits the groups. For example, the binary number 00001111 10001000 11111111 01101011 (spaces added for clarity) formatted as an IP address would be "15.136.255.107".
Example
- Input
- ip = "255.0.0.7", n = 10
- Output
- ["255.0.0.7/32","255.0.0.8/29","255.0.0.16/32"]
- Explanation
- The IP addresses that need to be covered are:
Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) auxiliary |
Pattern: Bit Manipulation
Use XOR, masks and the low-bit trick to replace whole data structures with an integer. LeetCode 751. IP to CIDR is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Bit Manipulation.
The bit manipulation guide has the Python template for the pattern and the 194 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 751. IP to CIDR?
- LeetCode 751. IP to CIDR is rated Medium on LeetCode.
- What is the time complexity of LeetCode 751. IP to CIDR?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 751. IP to CIDR?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 751. IP to CIDR cover?
- LeetCode 751. IP to CIDR is tagged Bit Manipulation and String on LeetCode.
- Is LeetCode 751. IP to CIDR a premium problem?
- Yes. LeetCode 751. IP to CIDR is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.