Skip to content

Commit 904dfb9

Browse files
committed
751_IP_to_CIDR
1 parent 31090c9 commit 904dfb9

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ Also, there are open source implementations for basic data structs and algorithm
178178
| 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/724_Find_Pivot_Index.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/724_Find_Pivot_Index.java) | Seach the array to find a place where left sum is equal to right sum, O(n) and O(1) |
179179
| 733 | [Flood Fill](https://leetcode.com/problems/flood-fill/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/733_Flood_Fill.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/733_Flood_Fill.java) | 1. DFS with stack or recursive, O(n) and O(n)<br>2. BFS with queue, O(n) and O(n) |
180180
| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/743_Network_Delay_Time.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/743_Network_Delay_Time.java) | Let V == N, then: 1. DFS, O(V^V+ElgE), O(V+E)<br>2. Dijkstra, O(V^2+E), O(V+E)|
181+
| 751 | [IP to CIDR](https://leetcode.com/problems/ip-to-cidr/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/751_IP_to_CIDR.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/751_IP_to_CIDR.java) | Bit manipulations, incrementail is 1 << (32 - mask) |
181182
| 766 | [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/766_Toeplitz_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/766_Toeplitz_Matrix.java) | Check from top left to bottom right, i,j == i + 1, j + 1. |
182183
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | Count given char in string. Hash or table. [Oneline](https://leetcode.com/problems/jewels-and-stones/discuss/113574/1-liners-PythonJavaRuby) |
183184
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/804_Unique_Morse_Code_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/804_Unique_Morse_Code_Words.java) | String, Hash and Set. Set is recommended. |

java/751_IP_to_CIDR.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public List<String> ipToCIDR(String ip, int n) {
3+
long start = ipToLong(ip);
4+
List<String> ans = new ArrayList();
5+
while (n > 0) {
6+
int mask = Math.max(33 - bitLength(Long.lowestOneBit(start)),
7+
33 - bitLength(n));
8+
ans.add(longToIP(start) + "/" + mask);
9+
start += 1 << (32 - mask);
10+
n -= 1 << (32 - mask);
11+
}
12+
return ans;
13+
}
14+
private long ipToLong(String ip) {
15+
long ans = 0;
16+
for (String x: ip.split("\\.")) {
17+
ans = 256 * ans + Integer.valueOf(x);
18+
}
19+
return ans;
20+
}
21+
private String longToIP(long x) {
22+
return String.format("%s.%s.%s.%s",
23+
x >> 24, (x >> 16) % 256, (x >> 8) % 256, x % 256);
24+
}
25+
private int bitLength(long x) {
26+
if (x == 0) return 1;
27+
int ans = 0;
28+
while (x > 0) {
29+
x >>= 1;
30+
ans++;
31+
}
32+
return ans;
33+
}
34+
}

python/751_IP_to_CIDR.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def ipToInt(self, ip):
3+
ans = 0
4+
for x in ip.split('.'):
5+
ans = 256 * ans + int(x)
6+
return ans
7+
8+
def intToIP(self, x):
9+
return ".".join(str((x >> i) % 256)
10+
for i in (24, 16, 8, 0))
11+
12+
def ipToCIDR(self, ip, n):
13+
# Start value of IP
14+
start = self.ipToInt(ip)
15+
ans = []
16+
while n:
17+
# Last 1 of start or can start from 0
18+
mask = max(33 - (start & -start).bit_length(),
19+
33 - n.bit_length())
20+
ans.append(self.intToIP(start) + '/' + str(mask))
21+
start += 1 << (32 - mask)
22+
n -= 1 << (32 - mask)
23+
return ans

0 commit comments

Comments
 (0)