Skip to content

Commit 7f6cc7d

Browse files
authored
feat: add python solutions to lc problems: No.0332,0468 (#1880)
1 parent ac9e8eb commit 7f6cc7d

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

solution/0300-0399/0332.Reconstruct Itinerary/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@
5757
<!-- 这里可写当前语言的特殊实现逻辑 -->
5858

5959
```python
60+
class Solution:
61+
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
62+
graph = defaultdict(list)
63+
64+
for src, dst in sorted(tickets, reverse=True):
65+
graph[src].append(dst)
66+
67+
itinerary = []
68+
69+
def dfs(airport):
70+
while graph[airport]:
71+
dfs(graph[airport].pop())
72+
itinerary.append(airport)
73+
74+
dfs("JFK")
75+
76+
return itinerary[::-1]
6077

6178
```
6279

solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md

+17
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@
4949
### **Python3**
5050

5151
```python
52+
class Solution:
53+
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
54+
graph = defaultdict(list)
55+
56+
for src, dst in sorted(tickets, reverse=True):
57+
graph[src].append(dst)
58+
59+
itinerary = []
60+
61+
def dfs(airport):
62+
while graph[airport]:
63+
dfs(graph[airport].pop())
64+
itinerary.append(airport)
65+
66+
dfs("JFK")
67+
68+
return itinerary[::-1]
5269

5370
```
5471

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
3+
graph = defaultdict(list)
4+
5+
for src, dst in sorted(tickets, reverse=True):
6+
graph[src].append(dst)
7+
8+
itinerary = []
9+
10+
def dfs(airport):
11+
while graph[airport]:
12+
dfs(graph[airport].pop())
13+
itinerary.append(airport)
14+
15+
dfs("JFK")
16+
17+
return itinerary[::-1]

solution/0400-0499/0468.Validate IP Address/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```python
68+
class Solution:
69+
def validIPAddress(self, IP: str) -> str:
70+
if "." in IP:
71+
segments = IP.split(".")
72+
if len(segments) != 4:
73+
return "Neither"
74+
for segment in segments:
75+
if (
76+
not segment.isdigit()
77+
or not 0 <= int(segment) <= 255
78+
or (segment[0] == "0" and len(segment) > 1)
79+
):
80+
return "Neither"
81+
return "IPv4"
82+
elif ":" in IP:
83+
segments = IP.split(":")
84+
if len(segments) != 8:
85+
return "Neither"
86+
for segment in segments:
87+
if (
88+
not segment
89+
or len(segment) > 4
90+
or not all(c in string.hexdigits for c in segment)
91+
):
92+
return "Neither"
93+
return "IPv6"
94+
return "Neither"
6895

6996
```
7097

solution/0400-0499/0468.Validate IP Address/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@
5757
### **Python3**
5858

5959
```python
60+
class Solution:
61+
def validIPAddress(self, IP: str) -> str:
62+
if "." in IP:
63+
segments = IP.split(".")
64+
if len(segments) != 4:
65+
return "Neither"
66+
for segment in segments:
67+
if (
68+
not segment.isdigit()
69+
or not 0 <= int(segment) <= 255
70+
or (segment[0] == "0" and len(segment) > 1)
71+
):
72+
return "Neither"
73+
return "IPv4"
74+
elif ":" in IP:
75+
segments = IP.split(":")
76+
if len(segments) != 8:
77+
return "Neither"
78+
for segment in segments:
79+
if (
80+
not segment
81+
or len(segment) > 4
82+
or not all(c in string.hexdigits for c in segment)
83+
):
84+
return "Neither"
85+
return "IPv6"
86+
return "Neither"
6087

6188
```
6289

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def validIPAddress(self, IP: str) -> str:
3+
if "." in IP:
4+
segments = IP.split(".")
5+
if len(segments) != 4:
6+
return "Neither"
7+
for segment in segments:
8+
if (
9+
not segment.isdigit()
10+
or not 0 <= int(segment) <= 255
11+
or (segment[0] == "0" and len(segment) > 1)
12+
):
13+
return "Neither"
14+
return "IPv4"
15+
elif ":" in IP:
16+
segments = IP.split(":")
17+
if len(segments) != 8:
18+
return "Neither"
19+
for segment in segments:
20+
if (
21+
not segment
22+
or len(segment) > 4
23+
or not all(c in string.hexdigits for c in segment)
24+
):
25+
return "Neither"
26+
return "IPv6"
27+
return "Neither"

0 commit comments

Comments
 (0)