diff --git a/solution/0100-0199/0125.Valid Palindrome/README.md b/solution/0100-0199/0125.Valid Palindrome/README.md index df5c35260486a..d9c1db7f8393a 100644 --- a/solution/0100-0199/0125.Valid Palindrome/README.md +++ b/solution/0100-0199/0125.Valid Palindrome/README.md @@ -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 { @@ -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 + } +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0125.Valid Palindrome/README_EN.md b/solution/0100-0199/0125.Valid Palindrome/README_EN.md index 630db18c094cc..6bb2e8cb201da 100644 --- a/solution/0100-0199/0125.Valid Palindrome/README_EN.md +++ b/solution/0100-0199/0125.Valid Palindrome/README_EN.md @@ -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 @@ -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 + } +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0125.Valid Palindrome/Solution.rs b/solution/0100-0199/0125.Valid Palindrome/Solution.rs new file mode 100644 index 0000000000000..43bad4945eb9a --- /dev/null +++ b/solution/0100-0199/0125.Valid Palindrome/Solution.rs @@ -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 + } +} diff --git a/solution/0400-0499/0498.Diagonal Traverse/README.md b/solution/0400-0499/0498.Diagonal Traverse/README.md index c0846c6f5276c..6fd05dde03536 100644 --- a/solution/0400-0499/0498.Diagonal Traverse/README.md +++ b/solution/0400-0499/0498.Diagonal Traverse/README.md @@ -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 { + 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() + } +} +``` + ### **...** ``` diff --git a/solution/0400-0499/0498.Diagonal Traverse/README_EN.md b/solution/0400-0499/0498.Diagonal Traverse/README_EN.md index ddad99d17574e..47f33ec1fe9cc 100644 --- a/solution/0400-0499/0498.Diagonal Traverse/README_EN.md +++ b/solution/0400-0499/0498.Diagonal Traverse/README_EN.md @@ -148,6 +148,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 { + 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() + } +} +``` + ### **...** ``` diff --git a/solution/0400-0499/0498.Diagonal Traverse/Solution.rs b/solution/0400-0499/0498.Diagonal Traverse/Solution.rs new file mode 100644 index 0000000000000..1d323b51183ae --- /dev/null +++ b/solution/0400-0499/0498.Diagonal Traverse/Solution.rs @@ -0,0 +1,31 @@ +impl Solution { + pub fn find_diagonal_order(mat: Vec>) -> Vec { + 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() + } +} diff --git a/solution/0400-0499/0498.Diagonal Traverse/Solution.ts b/solution/0400-0499/0498.Diagonal Traverse/Solution.ts new file mode 100644 index 0000000000000..b8152f46c5cd6 --- /dev/null +++ b/solution/0400-0499/0498.Diagonal Traverse/Solution.ts @@ -0,0 +1,35 @@ +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; +}