Download IP to CIDR.py
class Solution(object): def ipToInt(self, ip): ans = 0 for x in ip.split('.'): ans = 256 * ans + int(x) return ans def intToIP(self, x): return ".".join(str((x >> i) % 256) for i in (24, 16, 8, 0)) def ipToCIDR(self, ip, n): # Start value of IP start = self.ipToInt(ip) ans =  while n: # Last 1 of start or can start from 0 mask = max(33 - (start & -start).bit_length(), 33 - n.bit_length()) ans.append(self.intToIP(start) + '/' + str(mask)) start += 1 << (32 - mask) n -= 1 << (32 - mask) return ans
Leetcode 751 IP to CIDR problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.
Feedback is the most important part of any website.
If you have any query, suggestion or feedback, Please feel free to contact us.