File tree Expand file tree Collapse file tree 5 files changed +180
-2
lines changed
solution/2100-2199/2108.Find First Palindromic String in the Array Expand file tree Collapse file tree 5 files changed +180
-2
lines changed Original file line number Diff line number Diff line change @@ -141,10 +141,71 @@ func firstPalindrome(words []string) string {
141
141
142
142
### ** TypeScript**
143
143
144
- <!-- 这里可写当前语言的特殊实现逻辑 -->
145
-
146
144
``` 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
+ ```
147
163
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
+ }
148
209
```
149
210
150
211
### **...**
Original file line number Diff line number Diff line change @@ -135,7 +135,70 @@ func firstPalindrome(words []string) string {
135
135
### ** TypeScript**
136
136
137
137
``` 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
+ ```
138
156
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
+ }
139
202
```
140
203
141
204
### **...**
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments