File tree 6 files changed +132
-0
lines changed
0300-0399/0332.Reconstruct Itinerary
0400-0499/0468.Validate IP Address
6 files changed +132
-0
lines changed Original file line number Diff line number Diff line change 57
57
<!-- 这里可写当前语言的特殊实现逻辑 -->
58
58
59
59
``` 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 ]
60
77
61
78
```
62
79
Original file line number Diff line number Diff line change 49
49
### ** Python3**
50
50
51
51
``` 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 ]
52
69
53
70
```
54
71
Original file line number Diff line number Diff line change
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 ]
Original file line number Diff line number Diff line change 65
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
66
67
67
``` 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"
68
95
69
96
```
70
97
Original file line number Diff line number Diff line change 57
57
### ** Python3**
58
58
59
59
``` 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"
60
87
61
88
```
62
89
Original file line number Diff line number Diff line change
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"
You can’t perform that action at this time.
0 commit comments