Skip to content

Commit 0c76d8a

Browse files
committed
feat: add solutions to lc problems: No.1812,1813
1 parent 35bf28f commit 0c76d8a

File tree

8 files changed

+192
-8
lines changed

8 files changed

+192
-8
lines changed

solution/1800-1899/1812.Determine Color of a Chessboard Square/README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,25 @@
6161
<!-- 这里可写当前语言的特殊实现逻辑 -->
6262

6363
```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
6569
```
6670

6771
### **Java**
6872

6973
<!-- 这里可写当前语言的特殊实现逻辑 -->
7074

7175
```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+
}
7383
```
7484

7585
### **...**

solution/1800-1899/1812.Determine Color of a Chessboard Square/README_EN.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,23 @@
5353
### **Python3**
5454

5555
```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
5761
```
5862

5963
### **Java**
6064

6165
```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+
}
6373
```
6474

6575
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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

solution/1800-1899/1813.Sentence Similarity III/README.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,65 @@
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```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)
6786
```
6887

6988
### **Java**
7089

7190
<!-- 这里可写当前语言的特殊实现逻辑 -->
7291

7392
```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+
}
75125
```
76126

77127
### **...**

solution/1800-1899/1813.Sentence Similarity III/README_EN.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,63 @@
5959
### **Python3**
6060

6161
```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)
6382
```
6483

6584
### **Java**
6685

6786
```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+
}
69119
```
70120

71121
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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)

0 commit comments

Comments
 (0)