Skip to content

Commit e56ff75

Browse files
committed
feat: add solutions to lc problems: No.0125,0498
- No.0125.Valid Palindrome - No.0498.Diagonal Traverse
1 parent b9800af commit e56ff75

File tree

7 files changed

+449
-1
lines changed

7 files changed

+449
-1
lines changed

solution/0100-0199/0125.Valid Palindrome/README.md

+104-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,81 @@ private:
118118
};
119119
```
120120
121-
### **tTypeScript**
121+
### **C#**
122+
123+
```cs
124+
using System.Linq;
125+
126+
public class Solution {
127+
public bool IsPalindrome(string s) {
128+
var chars = s.Where(ch => char.IsLetterOrDigit(ch)).Select(char.ToLower).ToList();
129+
var i = 0;
130+
var j = chars.Count - 1;
131+
for (; i < j; ++i, --j)
132+
{
133+
if (chars[i] != chars[j]) return false;
134+
}
135+
return true;
136+
}
137+
}
138+
```
139+
140+
### **JavaScript**
141+
142+
```js
143+
const isPalindrome1 = function (s) {
144+
let arr1 = [],
145+
arr2 = [];
146+
for (let i = 0; i < s.length; i++) {
147+
if (s[i] >= 'A' && s[i] <= 'Z') {
148+
arr1.push(s[i].toLowerCase());
149+
}
150+
if ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z')) {
151+
arr1.push(s[i]);
152+
}
153+
}
154+
arr2 = [...arr1];
155+
arr2.reverse();
156+
return arr1.join('') === arr2.join('');
157+
};
158+
159+
const isPalindrome = function (s) {
160+
function isNumOrAl(a) {
161+
if (
162+
(a >= 'A' && a <= 'Z') ||
163+
(a >= '0' && a <= '9') ||
164+
(a >= 'a' && a <= 'z')
165+
) {
166+
return true;
167+
} else {
168+
return false;
169+
}
170+
}
171+
172+
if (s.length === 0) {
173+
return true;
174+
}
175+
let i = 0,
176+
j = s.length - 1;
177+
while (i < j) {
178+
while (i < j && !isNumOrAl(s[i])) {
179+
i++;
180+
}
181+
while (i < j && !isNumOrAl(s[j])) {
182+
j--;
183+
}
184+
if (s[i].toLowerCase() !== s[j].toLowerCase()) {
185+
return false;
186+
} else {
187+
i++;
188+
j--;
189+
}
190+
}
191+
return true;
192+
};
193+
```
194+
195+
### **TypeScript**
122196

123197
```ts
124198
function isPalindrome(s: string): boolean {
@@ -142,6 +216,35 @@ function isPalindrome(s: string): boolean {
142216
}
143217
```
144218

219+
### **Rust**
220+
221+
```rust
222+
impl Solution {
223+
pub fn is_palindrome(s: String) -> bool {
224+
let s = s.to_lowercase();
225+
let s = s.as_bytes();
226+
let n = s.len();
227+
let (mut l, mut r) = (0, n - 1);
228+
while l < r {
229+
while l < r && !s[l].is_ascii_alphanumeric() {
230+
l += 1;
231+
}
232+
while l < r && !s[r].is_ascii_alphanumeric() {
233+
r -= 1;
234+
}
235+
if s[l] != s[r] {
236+
return false;
237+
}
238+
l += 1;
239+
if r != 0 {
240+
r -= 1;
241+
}
242+
}
243+
true
244+
}
245+
}
246+
```
247+
145248
### **...**
146249

147250
```

solution/0100-0199/0125.Valid Palindrome/README_EN.md

+103
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,80 @@ private:
117117
};
118118
```
119119
120+
### **C#**
121+
122+
```cs
123+
using System.Linq;
124+
125+
public class Solution {
126+
public bool IsPalindrome(string s) {
127+
var chars = s.Where(ch => char.IsLetterOrDigit(ch)).Select(char.ToLower).ToList();
128+
var i = 0;
129+
var j = chars.Count - 1;
130+
for (; i < j; ++i, --j)
131+
{
132+
if (chars[i] != chars[j]) return false;
133+
}
134+
return true;
135+
}
136+
}
137+
```
138+
139+
### **JavaScript**
140+
141+
```js
142+
const isPalindrome1 = function (s) {
143+
let arr1 = [],
144+
arr2 = [];
145+
for (let i = 0; i < s.length; i++) {
146+
if (s[i] >= 'A' && s[i] <= 'Z') {
147+
arr1.push(s[i].toLowerCase());
148+
}
149+
if ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z')) {
150+
arr1.push(s[i]);
151+
}
152+
}
153+
arr2 = [...arr1];
154+
arr2.reverse();
155+
return arr1.join('') === arr2.join('');
156+
};
157+
158+
const isPalindrome = function (s) {
159+
function isNumOrAl(a) {
160+
if (
161+
(a >= 'A' && a <= 'Z') ||
162+
(a >= '0' && a <= '9') ||
163+
(a >= 'a' && a <= 'z')
164+
) {
165+
return true;
166+
} else {
167+
return false;
168+
}
169+
}
170+
171+
if (s.length === 0) {
172+
return true;
173+
}
174+
let i = 0,
175+
j = s.length - 1;
176+
while (i < j) {
177+
while (i < j && !isNumOrAl(s[i])) {
178+
i++;
179+
}
180+
while (i < j && !isNumOrAl(s[j])) {
181+
j--;
182+
}
183+
if (s[i].toLowerCase() !== s[j].toLowerCase()) {
184+
return false;
185+
} else {
186+
i++;
187+
j--;
188+
}
189+
}
190+
return true;
191+
};
192+
```
193+
120194
### **TypeScript**
121195

122196
```ts
@@ -141,6 +215,35 @@ function isPalindrome(s: string): boolean {
141215
}
142216
```
143217

218+
### **Rust**
219+
220+
```rust
221+
impl Solution {
222+
pub fn is_palindrome(s: String) -> bool {
223+
let s = s.to_lowercase();
224+
let s = s.as_bytes();
225+
let n = s.len();
226+
let (mut l, mut r) = (0, n - 1);
227+
while l < r {
228+
while l < r && !s[l].is_ascii_alphanumeric() {
229+
l += 1;
230+
}
231+
while l < r && !s[r].is_ascii_alphanumeric() {
232+
r -= 1;
233+
}
234+
if s[l] != s[r] {
235+
return false;
236+
}
237+
l += 1;
238+
if r != 0 {
239+
r -= 1;
240+
}
241+
}
242+
true
243+
}
244+
}
245+
```
246+
144247
### **...**
145248

146249
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn is_palindrome(s: String) -> bool {
3+
let s = s.to_lowercase();
4+
let s = s.as_bytes();
5+
let n = s.len();
6+
let (mut l, mut r) = (0, n - 1);
7+
while l < r {
8+
while l < r && !s[l].is_ascii_alphanumeric() {
9+
l += 1;
10+
}
11+
while l < r && !s[r].is_ascii_alphanumeric() {
12+
r -= 1;
13+
}
14+
if s[l] != s[r] {
15+
return false;
16+
}
17+
l += 1;
18+
if r != 0 {
19+
r -= 1;
20+
}
21+
}
22+
true
23+
}
24+
}

solution/0400-0499/0498.Diagonal Traverse/README.md

+76
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,82 @@ func findDiagonalOrder(mat [][]int) []int {
158158
}
159159
```
160160

161+
### **TypeScript**
162+
163+
```ts
164+
function findDiagonalOrder(mat: number[][]): number[] {
165+
const res = [];
166+
const m = mat.length;
167+
const n = mat[0].length;
168+
let i = 0;
169+
let j = 0;
170+
let mark = true;
171+
while (res.length !== n * m) {
172+
if (mark) {
173+
while (i >= 0 && j < n) {
174+
res.push(mat[i][j]);
175+
i--;
176+
j++;
177+
}
178+
if (j === n) {
179+
j--;
180+
i++;
181+
}
182+
i++;
183+
} else {
184+
while (i < m && j >= 0) {
185+
res.push(mat[i][j]);
186+
i++;
187+
j--;
188+
}
189+
if (i === m) {
190+
i--;
191+
j++;
192+
}
193+
j++;
194+
}
195+
mark = !mark;
196+
}
197+
return res;
198+
}
199+
```
200+
201+
### **Rust**
202+
203+
```rust
204+
impl Solution {
205+
pub fn find_diagonal_order(mat: Vec<Vec<i32>>) -> Vec<i32> {
206+
let (m, n) = (mat.len(), mat[0].len());
207+
let (mut i, mut j) = (0, 0);
208+
(0..m * n)
209+
.map(|_| {
210+
let res = mat[i][j];
211+
if (i + j) % 2 == 0 {
212+
if j == n - 1 {
213+
i += 1;
214+
} else if i == 0 {
215+
j += 1;
216+
} else {
217+
i -= 1;
218+
j += 1;
219+
}
220+
} else {
221+
if i == m - 1 {
222+
j += 1;
223+
} else if j == 0 {
224+
i += 1;
225+
} else {
226+
i += 1;
227+
j -= 1;
228+
}
229+
}
230+
res
231+
})
232+
.collect()
233+
}
234+
}
235+
```
236+
161237
### **...**
162238

163239
```

0 commit comments

Comments
 (0)