Skip to content

Commit d6d2f6c

Browse files
committed
feat: add solutions to lc problem: No.2108
No.2108.Find First Palindromic String in the Array
1 parent 07d7ace commit d6d2f6c

File tree

5 files changed

+180
-2
lines changed

5 files changed

+180
-2
lines changed

solution/2100-2199/2108.Find First Palindromic String in the Array/README.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,71 @@ func firstPalindrome(words []string) string {
141141

142142
### **TypeScript**
143143

144-
<!-- 这里可写当前语言的特殊实现逻辑 -->
145-
146144
```ts
145+
function firstPalindrome(words: string[]): string {
146+
for (const word of words) {
147+
let left = 0;
148+
let right = word.length - 1;
149+
while (left < right) {
150+
if (word[left] !== word[right]) {
151+
break;
152+
}
153+
left++;
154+
right--;
155+
}
156+
if (left >= right) {
157+
return word;
158+
}
159+
}
160+
return '';
161+
}
162+
```
147163

164+
### **Rust**
165+
166+
```rust
167+
impl Solution {
168+
pub fn first_palindrome(words: Vec<String>) -> String {
169+
for word in words.iter() {
170+
let s = word.as_bytes();
171+
let mut left = 0;
172+
let mut right = s.len() - 1;
173+
while (left < right) {
174+
if (s[left] != s[right]) {
175+
break;
176+
}
177+
left += 1;
178+
right -= 1;
179+
}
180+
if left >= right {
181+
return word.clone();
182+
}
183+
}
184+
String::new()
185+
}
186+
}
187+
```
188+
189+
### **C**
190+
191+
```c
192+
char *firstPalindrome(char **words, int wordsSize) {
193+
for (int i = 0; i < wordsSize; i++) {
194+
int left = 0;
195+
int right = strlen(words[i]) - 1;
196+
while (left < right) {
197+
if (words[i][left] != words[i][right]) {
198+
break;
199+
}
200+
left++;
201+
right--;
202+
}
203+
if (left >= right) {
204+
return words[i];
205+
}
206+
}
207+
return "";
208+
}
148209
```
149210
150211
### **...**

solution/2100-2199/2108.Find First Palindromic String in the Array/README_EN.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,70 @@ func firstPalindrome(words []string) string {
135135
### **TypeScript**
136136

137137
```ts
138+
function firstPalindrome(words: string[]): string {
139+
for (const word of words) {
140+
let left = 0;
141+
let right = word.length - 1;
142+
while (left < right) {
143+
if (word[left] !== word[right]) {
144+
break;
145+
}
146+
left++;
147+
right--;
148+
}
149+
if (left >= right) {
150+
return word;
151+
}
152+
}
153+
return '';
154+
}
155+
```
138156

157+
### **Rust**
158+
159+
```rust
160+
impl Solution {
161+
pub fn first_palindrome(words: Vec<String>) -> String {
162+
for word in words.iter() {
163+
let s = word.as_bytes();
164+
let mut left = 0;
165+
let mut right = s.len() - 1;
166+
while (left < right) {
167+
if (s[left] != s[right]) {
168+
break;
169+
}
170+
left += 1;
171+
right -= 1;
172+
}
173+
if left >= right {
174+
return word.clone();
175+
}
176+
}
177+
String::new()
178+
}
179+
}
180+
```
181+
182+
### **C**
183+
184+
```c
185+
char *firstPalindrome(char **words, int wordsSize) {
186+
for (int i = 0; i < wordsSize; i++) {
187+
int left = 0;
188+
int right = strlen(words[i]) - 1;
189+
while (left < right) {
190+
if (words[i][left] != words[i][right]) {
191+
break;
192+
}
193+
left++;
194+
right--;
195+
}
196+
if (left >= right) {
197+
return words[i];
198+
}
199+
}
200+
return "";
201+
}
139202
```
140203
141204
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
char *firstPalindrome(char **words, int wordsSize) {
2+
for (int i = 0; i < wordsSize; i++) {
3+
int left = 0;
4+
int right = strlen(words[i]) - 1;
5+
while (left < right) {
6+
if (words[i][left] != words[i][right]) {
7+
break;
8+
}
9+
left++;
10+
right--;
11+
}
12+
if (left >= right) {
13+
return words[i];
14+
}
15+
}
16+
return "";
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn first_palindrome(words: Vec<String>) -> String {
3+
for word in words.iter() {
4+
let s = word.as_bytes();
5+
let mut left = 0;
6+
let mut right = s.len() - 1;
7+
while (left < right) {
8+
if (s[left] != s[right]) {
9+
break;
10+
}
11+
left += 1;
12+
right -= 1;
13+
}
14+
if left >= right {
15+
return word.clone();
16+
}
17+
}
18+
String::new()
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function firstPalindrome(words: string[]): string {
2+
for (const word of words) {
3+
let left = 0;
4+
let right = word.length - 1;
5+
while (left < right) {
6+
if (word[left] !== word[right]) {
7+
break;
8+
}
9+
left++;
10+
right--;
11+
}
12+
if (left >= right) {
13+
return word;
14+
}
15+
}
16+
return '';
17+
}

0 commit comments

Comments
 (0)