Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Solution for 332. #1880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions solution/0300-0399/0332.Reconstruct Itinerary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
class Solution:
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
graph = defaultdict(list)

for src, dst in sorted(tickets, reverse=True):
graph[src].append(dst)

itinerary = []

def dfs(airport):
while graph[airport]:
dfs(graph[airport].pop())
itinerary.append(airport)

dfs("JFK")

return itinerary[::-1]

```

Expand Down
17 changes: 17 additions & 0 deletions solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@
### **Python3**

```python
class Solution:
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
graph = defaultdict(list)

for src, dst in sorted(tickets, reverse=True):
graph[src].append(dst)

itinerary = []

def dfs(airport):
while graph[airport]:
dfs(graph[airport].pop())
itinerary.append(airport)

dfs("JFK")

return itinerary[::-1]

```

Expand Down
17 changes: 17 additions & 0 deletions solution/0300-0399/0332.Reconstruct Itinerary/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
graph = defaultdict(list)

for src, dst in sorted(tickets, reverse=True):
graph[src].append(dst)

itinerary = []

def dfs(airport):
while graph[airport]:
dfs(graph[airport].pop())
itinerary.append(airport)

dfs("JFK")

return itinerary[::-1]
27 changes: 27 additions & 0 deletions solution/0400-0499/0468.Validate IP Address/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
class Solution:
def validIPAddress(self, IP: str) -> str:
if "." in IP:
segments = IP.split(".")
if len(segments) != 4:
return "Neither"
for segment in segments:
if (
not segment.isdigit()
or not 0 <= int(segment) <= 255
or (segment[0] == "0" and len(segment) > 1)
):
return "Neither"
return "IPv4"
elif ":" in IP:
segments = IP.split(":")
if len(segments) != 8:
return "Neither"
for segment in segments:
if (
not segment
or len(segment) > 4
or not all(c in string.hexdigits for c in segment)
):
return "Neither"
return "IPv6"
return "Neither"

```

Expand Down
27 changes: 27 additions & 0 deletions solution/0400-0499/0468.Validate IP Address/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@
### **Python3**

```python
class Solution:
def validIPAddress(self, IP: str) -> str:
if "." in IP:
segments = IP.split(".")
if len(segments) != 4:
return "Neither"
for segment in segments:
if (
not segment.isdigit()
or not 0 <= int(segment) <= 255
or (segment[0] == "0" and len(segment) > 1)
):
return "Neither"
return "IPv4"
elif ":" in IP:
segments = IP.split(":")
if len(segments) != 8:
return "Neither"
for segment in segments:
if (
not segment
or len(segment) > 4
or not all(c in string.hexdigits for c in segment)
):
return "Neither"
return "IPv6"
return "Neither"

```

Expand Down
27 changes: 27 additions & 0 deletions solution/0400-0499/0468.Validate IP Address/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def validIPAddress(self, IP: str) -> str:
if "." in IP:
segments = IP.split(".")
if len(segments) != 4:
return "Neither"
for segment in segments:
if (
not segment.isdigit()
or not 0 <= int(segment) <= 255
or (segment[0] == "0" and len(segment) > 1)
):
return "Neither"
return "IPv4"
elif ":" in IP:
segments = IP.split(":")
if len(segments) != 8:
return "Neither"
for segment in segments:
if (
not segment
or len(segment) > 4
or not all(c in string.hexdigits for c in segment)
):
return "Neither"
return "IPv6"
return "Neither"