File tree 8 files changed +192
-8
lines changed
1812.Determine Color of a Chessboard Square
1813.Sentence Similarity III
8 files changed +192
-8
lines changed Original file line number Diff line number Diff line change 61
61
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
62
63
63
``` python
64
-
64
+ class Solution :
65
+ def squareIsWhite (self , coordinates : str ) -> bool :
66
+ x = ord (coordinates[0 ]) - ord (' a' ) + 1
67
+ y = int (coordinates[1 ])
68
+ return ((x + y) & 1 ) == 1
65
69
```
66
70
67
71
### ** Java**
68
72
69
73
<!-- 这里可写当前语言的特殊实现逻辑 -->
70
74
71
75
``` java
72
-
76
+ class Solution {
77
+ public boolean squareIsWhite (String coordinates ) {
78
+ int x = coordinates. charAt(0 ) - ' a' + 1 ;
79
+ int y = coordinates. charAt(1 ) - ' 0' ;
80
+ return ((x + y) & 1 ) == 1 ;
81
+ }
82
+ }
73
83
```
74
84
75
85
### ** ...**
Original file line number Diff line number Diff line change 53
53
### ** Python3**
54
54
55
55
``` python
56
-
56
+ class Solution :
57
+ def squareIsWhite (self , coordinates : str ) -> bool :
58
+ x = ord (coordinates[0 ]) - ord (' a' ) + 1
59
+ y = int (coordinates[1 ])
60
+ return ((x + y) & 1 ) == 1
57
61
```
58
62
59
63
### ** Java**
60
64
61
65
``` java
62
-
66
+ class Solution {
67
+ public boolean squareIsWhite (String coordinates ) {
68
+ int x = coordinates. charAt(0 ) - ' a' + 1 ;
69
+ int y = coordinates. charAt(1 ) - ' 0' ;
70
+ return ((x + y) & 1 ) == 1 ;
71
+ }
72
+ }
63
73
```
64
74
65
75
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean squareIsWhite (String coordinates ) {
3
+ int x = coordinates .charAt (0 ) - 'a' + 1 ;
4
+ int y = coordinates .charAt (1 ) - '0' ;
5
+ return ((x + y ) & 1 ) == 1 ;
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def squareIsWhite (self , coordinates : str ) -> bool :
3
+ x = ord (coordinates [0 ]) - ord ('a' ) + 1
4
+ y = int (coordinates [1 ])
5
+ return ((x + y ) & 1 ) == 1
Original file line number Diff line number Diff line change 63
63
<!-- 这里可写当前语言的特殊实现逻辑 -->
64
64
65
65
``` python
66
-
66
+ class Solution :
67
+ def areSentencesSimilar (self , sentence1 : str , sentence2 : str ) -> bool :
68
+ if sentence1 == sentence2:
69
+ return True
70
+ n1, n2 = len (sentence1), len (sentence2)
71
+ if n1 == n2:
72
+ return False
73
+ if n1 < n2:
74
+ sentence1, sentence2 = sentence2, sentence1
75
+ words1, words2 = sentence1.split(), sentence2.split()
76
+ i = j = 0
77
+ while i < len (words2) and words1[i] == words2[i]:
78
+ i += 1
79
+ if i == len (words2):
80
+ return True
81
+ while j < len (words2) and words1[len (words1) - 1 - j] == words2[len (words2) - 1 - j]:
82
+ j += 1
83
+ if j == len (words2):
84
+ return True
85
+ return i + j == len (words2)
67
86
```
68
87
69
88
### ** Java**
70
89
71
90
<!-- 这里可写当前语言的特殊实现逻辑 -->
72
91
73
92
``` java
74
-
93
+ class Solution {
94
+ public boolean areSentencesSimilar (String sentence1 , String sentence2 ) {
95
+ if (Objects . equals(sentence1, sentence2)) {
96
+ return true ;
97
+ }
98
+ int n1 = sentence1. length(), n2 = sentence2. length();
99
+ if (n1 == n2) {
100
+ return false ;
101
+ }
102
+ if (n1 < n2) {
103
+ String t = sentence1;
104
+ sentence1 = sentence2;
105
+ sentence2 = t;
106
+ }
107
+ String [] words1 = sentence1. split(" " );
108
+ String [] words2 = sentence2. split(" " );
109
+ int i = 0 , j = 0 ;
110
+ while (i < words2. length && Objects . equals(words1[i], words2[i])) {
111
+ ++ i;
112
+ }
113
+ if (i == words2. length) {
114
+ return true ;
115
+ }
116
+ while (j < words2. length && Objects . equals(words1[words1. length - 1 - j], words2[words2. length - 1 - j])) {
117
+ ++ j;
118
+ }
119
+ if (j == words2. length) {
120
+ return true ;
121
+ }
122
+ return i + j == words2. length;
123
+ }
124
+ }
75
125
```
76
126
77
127
### ** ...**
Original file line number Diff line number Diff line change 59
59
### ** Python3**
60
60
61
61
``` python
62
-
62
+ class Solution :
63
+ def areSentencesSimilar (self , sentence1 : str , sentence2 : str ) -> bool :
64
+ if sentence1 == sentence2:
65
+ return True
66
+ n1, n2 = len (sentence1), len (sentence2)
67
+ if n1 == n2:
68
+ return False
69
+ if n1 < n2:
70
+ sentence1, sentence2 = sentence2, sentence1
71
+ words1, words2 = sentence1.split(), sentence2.split()
72
+ i = j = 0
73
+ while i < len (words2) and words1[i] == words2[i]:
74
+ i += 1
75
+ if i == len (words2):
76
+ return True
77
+ while j < len (words2) and words1[len (words1) - 1 - j] == words2[len (words2) - 1 - j]:
78
+ j += 1
79
+ if j == len (words2):
80
+ return True
81
+ return i + j == len (words2)
63
82
```
64
83
65
84
### ** Java**
66
85
67
86
``` java
68
-
87
+ class Solution {
88
+ public boolean areSentencesSimilar (String sentence1 , String sentence2 ) {
89
+ if (Objects . equals(sentence1, sentence2)) {
90
+ return true ;
91
+ }
92
+ int n1 = sentence1. length(), n2 = sentence2. length();
93
+ if (n1 == n2) {
94
+ return false ;
95
+ }
96
+ if (n1 < n2) {
97
+ String t = sentence1;
98
+ sentence1 = sentence2;
99
+ sentence2 = t;
100
+ }
101
+ String [] words1 = sentence1. split(" " );
102
+ String [] words2 = sentence2. split(" " );
103
+ int i = 0 , j = 0 ;
104
+ while (i < words2. length && Objects . equals(words1[i], words2[i])) {
105
+ ++ i;
106
+ }
107
+ if (i == words2. length) {
108
+ return true ;
109
+ }
110
+ while (j < words2. length && Objects . equals(words1[words1. length - 1 - j], words2[words2. length - 1 - j])) {
111
+ ++ j;
112
+ }
113
+ if (j == words2. length) {
114
+ return true ;
115
+ }
116
+ return i + j == words2. length;
117
+ }
118
+ }
69
119
```
70
120
71
121
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean areSentencesSimilar (String sentence1 , String sentence2 ) {
3
+ if (Objects .equals (sentence1 , sentence2 )) {
4
+ return true ;
5
+ }
6
+ int n1 = sentence1 .length (), n2 = sentence2 .length ();
7
+ if (n1 == n2 ) {
8
+ return false ;
9
+ }
10
+ if (n1 < n2 ) {
11
+ String t = sentence1 ;
12
+ sentence1 = sentence2 ;
13
+ sentence2 = t ;
14
+ }
15
+ String [] words1 = sentence1 .split (" " );
16
+ String [] words2 = sentence2 .split (" " );
17
+ int i = 0 , j = 0 ;
18
+ while (i < words2 .length && Objects .equals (words1 [i ], words2 [i ])) {
19
+ ++i ;
20
+ }
21
+ if (i == words2 .length ) {
22
+ return true ;
23
+ }
24
+ while (j < words2 .length && Objects .equals (words1 [words1 .length - 1 - j ], words2 [words2 .length - 1 - j ])) {
25
+ ++j ;
26
+ }
27
+ if (j == words2 .length ) {
28
+ return true ;
29
+ }
30
+ return i + j == words2 .length ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def areSentencesSimilar (self , sentence1 : str , sentence2 : str ) -> bool :
3
+ if sentence1 == sentence2 :
4
+ return True
5
+ n1 , n2 = len (sentence1 ), len (sentence2 )
6
+ if n1 == n2 :
7
+ return False
8
+ if n1 < n2 :
9
+ sentence1 , sentence2 = sentence2 , sentence1
10
+ words1 , words2 = sentence1 .split (), sentence2 .split ()
11
+ i = j = 0
12
+ while i < len (words2 ) and words1 [i ] == words2 [i ]:
13
+ i += 1
14
+ if i == len (words2 ):
15
+ return True
16
+ while j < len (words2 ) and words1 [len (words1 ) - 1 - j ] == words2 [len (words2 ) - 1 - j ]:
17
+ j += 1
18
+ if j == len (words2 ):
19
+ return True
20
+ return i + j == len (words2 )
You can’t perform that action at this time.
0 commit comments