Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.0125,0498
Browse files Browse the repository at this point in the history
- No.0125.Valid Palindrome
- No.0498.Diagonal Traverse
  • Loading branch information
YangFong committed Apr 26, 2022
1 parent b9800af commit e56ff75
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 1 deletion.
105 changes: 104 additions & 1 deletion solution/0100-0199/0125.Valid Palindrome/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,81 @@ private:
};
```
### **tTypeScript**
### **C#**
```cs
using System.Linq;
public class Solution {
public bool IsPalindrome(string s) {
var chars = s.Where(ch => char.IsLetterOrDigit(ch)).Select(char.ToLower).ToList();
var i = 0;
var j = chars.Count - 1;
for (; i < j; ++i, --j)
{
if (chars[i] != chars[j]) return false;
}
return true;
}
}
```

### **JavaScript**

```js
const isPalindrome1 = function (s) {
let arr1 = [],
arr2 = [];
for (let i = 0; i < s.length; i++) {
if (s[i] >= 'A' && s[i] <= 'Z') {
arr1.push(s[i].toLowerCase());
}
if ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z')) {
arr1.push(s[i]);
}
}
arr2 = [...arr1];
arr2.reverse();
return arr1.join('') === arr2.join('');
};

const isPalindrome = function (s) {
function isNumOrAl(a) {
if (
(a >= 'A' && a <= 'Z') ||
(a >= '0' && a <= '9') ||
(a >= 'a' && a <= 'z')
) {
return true;
} else {
return false;
}
}

if (s.length === 0) {
return true;
}
let i = 0,
j = s.length - 1;
while (i < j) {
while (i < j && !isNumOrAl(s[i])) {
i++;
}
while (i < j && !isNumOrAl(s[j])) {
j--;
}
if (s[i].toLowerCase() !== s[j].toLowerCase()) {
return false;
} else {
i++;
j--;
}
}
return true;
};
```

### **TypeScript**

```ts
function isPalindrome(s: string): boolean {
Expand All @@ -142,6 +216,35 @@ function isPalindrome(s: string): boolean {
}
```

### **Rust**

```rust
impl Solution {
pub fn is_palindrome(s: String) -> bool {
let s = s.to_lowercase();
let s = s.as_bytes();
let n = s.len();
let (mut l, mut r) = (0, n - 1);
while l < r {
while l < r && !s[l].is_ascii_alphanumeric() {
l += 1;
}
while l < r && !s[r].is_ascii_alphanumeric() {
r -= 1;
}
if s[l] != s[r] {
return false;
}
l += 1;
if r != 0 {
r -= 1;
}
}
true
}
}
```

### **...**

```
Expand Down
103 changes: 103 additions & 0 deletions solution/0100-0199/0125.Valid Palindrome/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,80 @@ private:
};
```
### **C#**
```cs
using System.Linq;
public class Solution {
public bool IsPalindrome(string s) {
var chars = s.Where(ch => char.IsLetterOrDigit(ch)).Select(char.ToLower).ToList();
var i = 0;
var j = chars.Count - 1;
for (; i < j; ++i, --j)
{
if (chars[i] != chars[j]) return false;
}
return true;
}
}
```

### **JavaScript**

```js
const isPalindrome1 = function (s) {
let arr1 = [],
arr2 = [];
for (let i = 0; i < s.length; i++) {
if (s[i] >= 'A' && s[i] <= 'Z') {
arr1.push(s[i].toLowerCase());
}
if ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z')) {
arr1.push(s[i]);
}
}
arr2 = [...arr1];
arr2.reverse();
return arr1.join('') === arr2.join('');
};

const isPalindrome = function (s) {
function isNumOrAl(a) {
if (
(a >= 'A' && a <= 'Z') ||
(a >= '0' && a <= '9') ||
(a >= 'a' && a <= 'z')
) {
return true;
} else {
return false;
}
}

if (s.length === 0) {
return true;
}
let i = 0,
j = s.length - 1;
while (i < j) {
while (i < j && !isNumOrAl(s[i])) {
i++;
}
while (i < j && !isNumOrAl(s[j])) {
j--;
}
if (s[i].toLowerCase() !== s[j].toLowerCase()) {
return false;
} else {
i++;
j--;
}
}
return true;
};
```

### **TypeScript**

```ts
Expand All @@ -141,6 +215,35 @@ function isPalindrome(s: string): boolean {
}
```

### **Rust**

```rust
impl Solution {
pub fn is_palindrome(s: String) -> bool {
let s = s.to_lowercase();
let s = s.as_bytes();
let n = s.len();
let (mut l, mut r) = (0, n - 1);
while l < r {
while l < r && !s[l].is_ascii_alphanumeric() {
l += 1;
}
while l < r && !s[r].is_ascii_alphanumeric() {
r -= 1;
}
if s[l] != s[r] {
return false;
}
l += 1;
if r != 0 {
r -= 1;
}
}
true
}
}
```

### **...**

```
Expand Down
24 changes: 24 additions & 0 deletions solution/0100-0199/0125.Valid Palindrome/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
impl Solution {
pub fn is_palindrome(s: String) -> bool {
let s = s.to_lowercase();
let s = s.as_bytes();
let n = s.len();
let (mut l, mut r) = (0, n - 1);
while l < r {
while l < r && !s[l].is_ascii_alphanumeric() {
l += 1;
}
while l < r && !s[r].is_ascii_alphanumeric() {
r -= 1;
}
if s[l] != s[r] {
return false;
}
l += 1;
if r != 0 {
r -= 1;
}
}
true
}
}
76 changes: 76 additions & 0 deletions solution/0400-0499/0498.Diagonal Traverse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,82 @@ func findDiagonalOrder(mat [][]int) []int {
}
```

### **TypeScript**

```ts
function findDiagonalOrder(mat: number[][]): number[] {
const res = [];
const m = mat.length;
const n = mat[0].length;
let i = 0;
let j = 0;
let mark = true;
while (res.length !== n * m) {
if (mark) {
while (i >= 0 && j < n) {
res.push(mat[i][j]);
i--;
j++;
}
if (j === n) {
j--;
i++;
}
i++;
} else {
while (i < m && j >= 0) {
res.push(mat[i][j]);
i++;
j--;
}
if (i === m) {
i--;
j++;
}
j++;
}
mark = !mark;
}
return res;
}
```

### **Rust**

```rust
impl Solution {
pub fn find_diagonal_order(mat: Vec<Vec<i32>>) -> Vec<i32> {
let (m, n) = (mat.len(), mat[0].len());
let (mut i, mut j) = (0, 0);
(0..m * n)
.map(|_| {
let res = mat[i][j];
if (i + j) % 2 == 0 {
if j == n - 1 {
i += 1;
} else if i == 0 {
j += 1;
} else {
i -= 1;
j += 1;
}
} else {
if i == m - 1 {
j += 1;
} else if j == 0 {
i += 1;
} else {
i += 1;
j -= 1;
}
}
res
})
.collect()
}
}
```

### **...**

```
Expand Down
Loading

0 comments on commit e56ff75

Please sign in to comment.