File tree 5 files changed +155
-4
lines changed
solution/1400-1499/1497.Check If Array Pairs Are Divisible by k
5 files changed +155
-4
lines changed Original file line number Diff line number Diff line change 71
71
<!-- 这里可写当前语言的特殊实现逻辑 -->
72
72
73
73
``` python
74
-
74
+ class Solution :
75
+ def isPathCrossing (self , path : str ) -> bool :
76
+ x = y = 0
77
+ visited = set ()
78
+ visited.add(' 0.0' )
79
+ for c in path:
80
+ if c == ' N' :
81
+ y += 1
82
+ elif c == ' S' :
83
+ y -= 1
84
+ elif c == ' E' :
85
+ x += 1
86
+ else :
87
+ x -= 1
88
+ pos = f ' { x} . { y} '
89
+ if pos in visited:
90
+ return True
91
+ visited.add(pos)
92
+ return False
75
93
```
76
94
77
95
### ** Java**
78
96
79
97
<!-- 这里可写当前语言的特殊实现逻辑 -->
80
98
81
99
``` java
82
-
100
+ class Solution {
101
+ public boolean isPathCrossing (String path ) {
102
+ Set<String > visited = new HashSet<> ();
103
+ visited. add(" 0.0" );
104
+ int x = 0 , y = 0 ;
105
+ for (int i = 0 ; i < path. length(); ++ i) {
106
+ char c = path. charAt(i);
107
+ if (c == ' N' ) {
108
+ ++ y;
109
+ } else if (c == ' S' ) {
110
+ -- y;
111
+ } else if (c == ' E' ) {
112
+ ++ x;
113
+ } else {
114
+ -- x;
115
+ }
116
+ String pos = x + " ." + y;
117
+ if (visited. contains(pos)) {
118
+ return true ;
119
+ }
120
+ visited. add(pos);
121
+ }
122
+ return false ;
123
+ }
124
+ }
83
125
```
84
126
85
127
### ** ...**
Original file line number Diff line number Diff line change 68
68
### ** Python3**
69
69
70
70
``` python
71
-
71
+ class Solution :
72
+ def isPathCrossing (self , path : str ) -> bool :
73
+ x = y = 0
74
+ visited = set ()
75
+ visited.add(' 0.0' )
76
+ for c in path:
77
+ if c == ' N' :
78
+ y += 1
79
+ elif c == ' S' :
80
+ y -= 1
81
+ elif c == ' E' :
82
+ x += 1
83
+ else :
84
+ x -= 1
85
+ pos = f ' { x} . { y} '
86
+ if pos in visited:
87
+ return True
88
+ visited.add(pos)
89
+ return False
72
90
```
73
91
74
92
### ** Java**
75
93
76
94
``` java
77
-
95
+ class Solution {
96
+ public boolean isPathCrossing (String path ) {
97
+ Set<String > visited = new HashSet<> ();
98
+ visited. add(" 0.0" );
99
+ int x = 0 , y = 0 ;
100
+ for (int i = 0 ; i < path. length(); ++ i) {
101
+ char c = path. charAt(i);
102
+ if (c == ' N' ) {
103
+ ++ y;
104
+ } else if (c == ' S' ) {
105
+ -- y;
106
+ } else if (c == ' E' ) {
107
+ ++ x;
108
+ } else {
109
+ -- x;
110
+ }
111
+ String pos = x + " ." + y;
112
+ if (visited. contains(pos)) {
113
+ return true ;
114
+ }
115
+ visited. add(pos);
116
+ }
117
+ return false ;
118
+ }
119
+ }
78
120
```
79
121
80
122
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isPathCrossing (String path ) {
3
+ Set <String > visited = new HashSet <>();
4
+ visited .add ("0.0" );
5
+ int x = 0 , y = 0 ;
6
+ for (int i = 0 ; i < path .length (); ++i ) {
7
+ char c = path .charAt (i );
8
+ if (c == 'N' ) {
9
+ ++y ;
10
+ } else if (c == 'S' ) {
11
+ --y ;
12
+ } else if (c == 'E' ) {
13
+ ++x ;
14
+ } else {
15
+ --x ;
16
+ }
17
+ String pos = x + "." + y ;
18
+ if (visited .contains (pos )) {
19
+ return true ;
20
+ }
21
+ visited .add (pos );
22
+ }
23
+ return false ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPathCrossing (self , path : str ) -> bool :
3
+ x = y = 0
4
+ visited = set ()
5
+ visited .add ('0.0' )
6
+ for c in path :
7
+ if c == 'N' :
8
+ y += 1
9
+ elif c == 'S' :
10
+ y -= 1
11
+ elif c == 'E' :
12
+ x += 1
13
+ else :
14
+ x -= 1
15
+ pos = f'{ x } .{ y } '
16
+ if pos in visited :
17
+ return True
18
+ visited .add (pos )
19
+ return False
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPathCrossing (self , path : str ) -> bool :
3
+ x = y = 0
4
+ visited = set ()
5
+ visited .add (f'{ x } .{ y } ' )
6
+ for c in path :
7
+ if c == 'N' :
8
+ y += 1
9
+ elif c == 'S' :
10
+ y -= 1
11
+ elif c == 'E' :
12
+ x += 1
13
+ else :
14
+ x -= 1
15
+ pos = f'{ x } .{ y } '
16
+ if pos in visited :
17
+ return True
18
+ visited .add (pos )
19
+ return False
20
+
21
+
22
+ if __name__ == '__main__' :
23
+ print (Solution ().isPathCrossing ("NESWW" ))
You can’t perform that action at this time.
0 commit comments