diff --git a/lcci/01.01.Is Unique/README.md b/lcci/01.01.Is Unique/README.md
index e8f0aafbf7165..013967f04e16f 100644
--- a/lcci/01.01.Is Unique/README.md
+++ b/lcci/01.01.Is Unique/README.md
@@ -19,7 +19,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/01.01.Is%20Unique/REA
示例 1:
输入: s = "leetcode"
-输出: false
+输出: false
示例 2:
@@ -56,8 +56,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/01.01.Is%20Unique/REA
class Solution:
def isUnique(self, astr: str) -> bool:
mask = 0
- for c in astr:
- i = ord(c) - ord('a')
+ for i in map(lambda c: ord(c) - ord("a"), astr):
if (mask >> i) & 1:
return False
mask |= 1 << i
diff --git a/lcci/01.01.Is Unique/README_EN.md b/lcci/01.01.Is Unique/README_EN.md
index 86501a1e6e8e5..a63cf58656329 100644
--- a/lcci/01.01.Is Unique/README_EN.md
+++ b/lcci/01.01.Is Unique/README_EN.md
@@ -64,8 +64,7 @@ The time complexity is $O(n)$, where $n$ is the length of the string. The space
class Solution:
def isUnique(self, astr: str) -> bool:
mask = 0
- for c in astr:
- i = ord(c) - ord('a')
+ for i in map(lambda c: ord(c) - ord("a"), astr):
if (mask >> i) & 1:
return False
mask |= 1 << i
diff --git a/lcci/01.01.Is Unique/Solution.py b/lcci/01.01.Is Unique/Solution.py
index 1d63247d2b3a0..06482062b2939 100644
--- a/lcci/01.01.Is Unique/Solution.py
+++ b/lcci/01.01.Is Unique/Solution.py
@@ -1,8 +1,7 @@
class Solution:
def isUnique(self, astr: str) -> bool:
mask = 0
- for c in astr:
- i = ord(c) - ord('a')
+ for i in map(lambda c: ord(c) - ord("a"), astr):
if (mask >> i) & 1:
return False
mask |= 1 << i
diff --git a/lcci/01.02.Check Permutation/README.md b/lcci/01.02.Check Permutation/README.md
index db958faa9953e..786b5c7538b57 100644
--- a/lcci/01.02.Check Permutation/README.md
+++ b/lcci/01.02.Check Permutation/README.md
@@ -93,11 +93,18 @@ class Solution {
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- if (s1.size() != s2.size()) return false;
- int cnt[26] = {0};
- for (char& c : s1) ++cnt[c - 'a'];
- for (char& c : s2)
- if (--cnt[c - 'a'] < 0) return false;
+ if (s1.size() != s2.size()) {
+ return false;
+ }
+ int cnt[26]{};
+ for (char c : s1) {
+ ++cnt[c - 'a'];
+ }
+ for (char c : s2) {
+ if (--cnt[c - 'a'] < 0) {
+ return false;
+ }
+ }
return true;
}
};
@@ -115,8 +122,7 @@ func CheckPermutation(s1 string, s2 string) bool {
cnt[c-'a']++
}
for _, c := range s2 {
- cnt[c-'a']--
- if cnt[c-'a'] < 0 {
+ if cnt[c-'a']--; cnt[c-'a'] < 0 {
return false
}
}
@@ -128,20 +134,18 @@ func CheckPermutation(s1 string, s2 string) bool {
```ts
function CheckPermutation(s1: string, s2: string): boolean {
- const n = s1.length;
- const m = s2.length;
- if (n !== m) {
+ if (s1.length !== s2.length) {
return false;
}
- const map = new Map();
- for (let i = 0; i < n; i++) {
- map.set(s1[i], (map.get(s1[i]) ?? 0) + 1);
- map.set(s2[i], (map.get(s2[i]) ?? 0) - 1);
+ const cnt: Record = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (const v of map.values()) {
- if (v !== 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
}
@@ -150,22 +154,26 @@ function CheckPermutation(s1: string, s2: string): boolean {
#### Rust
```rust
-use std::collections::HashMap;
impl Solution {
pub fn check_permutation(s1: String, s2: String) -> bool {
- let n = s1.len();
- let m = s2.len();
- if n != m {
+ if s1.len() != s2.len() {
return false;
}
- let s1 = s1.as_bytes();
- let s2 = s2.as_bytes();
- let mut map = HashMap::new();
- for i in 0..n {
- *map.entry(s1[i]).or_insert(0) += 1;
- *map.entry(s2[i]).or_insert(0) -= 1;
+
+ let mut cnt = vec![0; 26];
+ for c in s1.chars() {
+ cnt[(c as usize - 'a' as usize)] += 1;
+ }
+
+ for c in s2.chars() {
+ let index = c as usize - 'a' as usize;
+ if cnt[index] == 0 {
+ return false;
+ }
+ cnt[index] -= 1;
}
- map.values().all(|i| *i == 0)
+
+ true
}
}
```
@@ -179,19 +187,18 @@ impl Solution {
* @return {boolean}
*/
var CheckPermutation = function (s1, s2) {
- if (s1.length != s2.length) {
+ if (s1.length !== s2.length) {
return false;
}
- const cnt = new Array(26).fill(0);
- for (let i = 0; i < s1.length; ++i) {
- const j = s1.codePointAt(i) - 'a'.codePointAt(0);
- ++cnt[j];
+ const cnt = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (let i = 0; i < s2.length; ++i) {
- const j = s2.codePointAt(i) - 'a'.codePointAt(0);
- if (--cnt[j] < 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
};
@@ -206,19 +213,18 @@ class Solution {
return false
}
- var cnt = Array(repeating: 0, count: 26)
+ var cnt = [Int](repeating: 0, count: 26)
for char in s1 {
- let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] += 1
+ cnt[Int(char.asciiValue! - Character("a").asciiValue!)] += 1
}
for char in s2 {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] -= 1
- if cnt[index] < 0 {
+ if cnt[index] == 0 {
return false
}
+ cnt[index] -= 1
}
return true
@@ -268,8 +274,8 @@ class Solution {
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- sort(s1.begin(), s1.end());
- sort(s2.begin(), s2.end());
+ ranges::sort(s1);
+ ranges::sort(s2);
return s1 == s2;
}
};
@@ -308,6 +314,31 @@ impl Solution {
}
```
+#### JavaScript
+
+```js
+/**
+ * @param {string} s1
+ * @param {string} s2
+ * @return {boolean}
+ */
+var CheckPermutation = function (s1, s2) {
+ return [...s1].sort().join('') === [...s2].sort().join('');
+};
+```
+
+#### Swift
+
+```swift
+class Solution {
+ func CheckPermutation(_ s1: String, _ s2: String) -> Bool {
+ let s1 = s1.sorted()
+ let s2 = s2.sorted()
+ return s1 == s2
+ }
+}
+```
+
diff --git a/lcci/01.02.Check Permutation/README_EN.md b/lcci/01.02.Check Permutation/README_EN.md
index c18cd58477e8d..c582f9a548145 100644
--- a/lcci/01.02.Check Permutation/README_EN.md
+++ b/lcci/01.02.Check Permutation/README_EN.md
@@ -100,11 +100,18 @@ class Solution {
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- if (s1.size() != s2.size()) return false;
- int cnt[26] = {0};
- for (char& c : s1) ++cnt[c - 'a'];
- for (char& c : s2)
- if (--cnt[c - 'a'] < 0) return false;
+ if (s1.size() != s2.size()) {
+ return false;
+ }
+ int cnt[26]{};
+ for (char c : s1) {
+ ++cnt[c - 'a'];
+ }
+ for (char c : s2) {
+ if (--cnt[c - 'a'] < 0) {
+ return false;
+ }
+ }
return true;
}
};
@@ -122,8 +129,7 @@ func CheckPermutation(s1 string, s2 string) bool {
cnt[c-'a']++
}
for _, c := range s2 {
- cnt[c-'a']--
- if cnt[c-'a'] < 0 {
+ if cnt[c-'a']--; cnt[c-'a'] < 0 {
return false
}
}
@@ -135,20 +141,18 @@ func CheckPermutation(s1 string, s2 string) bool {
```ts
function CheckPermutation(s1: string, s2: string): boolean {
- const n = s1.length;
- const m = s2.length;
- if (n !== m) {
+ if (s1.length !== s2.length) {
return false;
}
- const map = new Map();
- for (let i = 0; i < n; i++) {
- map.set(s1[i], (map.get(s1[i]) ?? 0) + 1);
- map.set(s2[i], (map.get(s2[i]) ?? 0) - 1);
+ const cnt: Record = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (const v of map.values()) {
- if (v !== 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
}
@@ -157,22 +161,26 @@ function CheckPermutation(s1: string, s2: string): boolean {
#### Rust
```rust
-use std::collections::HashMap;
impl Solution {
pub fn check_permutation(s1: String, s2: String) -> bool {
- let n = s1.len();
- let m = s2.len();
- if n != m {
+ if s1.len() != s2.len() {
return false;
}
- let s1 = s1.as_bytes();
- let s2 = s2.as_bytes();
- let mut map = HashMap::new();
- for i in 0..n {
- *map.entry(s1[i]).or_insert(0) += 1;
- *map.entry(s2[i]).or_insert(0) -= 1;
+
+ let mut cnt = vec![0; 26];
+ for c in s1.chars() {
+ cnt[(c as usize - 'a' as usize)] += 1;
+ }
+
+ for c in s2.chars() {
+ let index = c as usize - 'a' as usize;
+ if cnt[index] == 0 {
+ return false;
+ }
+ cnt[index] -= 1;
}
- map.values().all(|i| *i == 0)
+
+ true
}
}
```
@@ -186,19 +194,18 @@ impl Solution {
* @return {boolean}
*/
var CheckPermutation = function (s1, s2) {
- if (s1.length != s2.length) {
+ if (s1.length !== s2.length) {
return false;
}
- const cnt = new Array(26).fill(0);
- for (let i = 0; i < s1.length; ++i) {
- const j = s1.codePointAt(i) - 'a'.codePointAt(0);
- ++cnt[j];
+ const cnt = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (let i = 0; i < s2.length; ++i) {
- const j = s2.codePointAt(i) - 'a'.codePointAt(0);
- if (--cnt[j] < 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
};
@@ -213,19 +220,18 @@ class Solution {
return false
}
- var cnt = Array(repeating: 0, count: 26)
+ var cnt = [Int](repeating: 0, count: 26)
for char in s1 {
- let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] += 1
+ cnt[Int(char.asciiValue! - Character("a").asciiValue!)] += 1
}
for char in s2 {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] -= 1
- if cnt[index] < 0 {
+ if cnt[index] == 0 {
return false
}
+ cnt[index] -= 1
}
return true
@@ -275,8 +281,8 @@ class Solution {
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- sort(s1.begin(), s1.end());
- sort(s2.begin(), s2.end());
+ ranges::sort(s1);
+ ranges::sort(s2);
return s1 == s2;
}
};
@@ -315,6 +321,31 @@ impl Solution {
}
```
+#### JavaScript
+
+```js
+/**
+ * @param {string} s1
+ * @param {string} s2
+ * @return {boolean}
+ */
+var CheckPermutation = function (s1, s2) {
+ return [...s1].sort().join('') === [...s2].sort().join('');
+};
+```
+
+#### Swift
+
+```swift
+class Solution {
+ func CheckPermutation(_ s1: String, _ s2: String) -> Bool {
+ let s1 = s1.sorted()
+ let s2 = s2.sorted()
+ return s1 == s2
+ }
+}
+```
+
diff --git a/lcci/01.02.Check Permutation/Solution.cpp b/lcci/01.02.Check Permutation/Solution.cpp
index 72808b7ee740c..ff8e6d531810c 100644
--- a/lcci/01.02.Check Permutation/Solution.cpp
+++ b/lcci/01.02.Check Permutation/Solution.cpp
@@ -1,11 +1,18 @@
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- if (s1.size() != s2.size()) return false;
- int cnt[26] = {0};
- for (char& c : s1) ++cnt[c - 'a'];
- for (char& c : s2)
- if (--cnt[c - 'a'] < 0) return false;
+ if (s1.size() != s2.size()) {
+ return false;
+ }
+ int cnt[26]{};
+ for (char c : s1) {
+ ++cnt[c - 'a'];
+ }
+ for (char c : s2) {
+ if (--cnt[c - 'a'] < 0) {
+ return false;
+ }
+ }
return true;
}
-};
\ No newline at end of file
+};
diff --git a/lcci/01.02.Check Permutation/Solution.go b/lcci/01.02.Check Permutation/Solution.go
index 7acd2c2c4bc00..e8fd1ace061de 100644
--- a/lcci/01.02.Check Permutation/Solution.go
+++ b/lcci/01.02.Check Permutation/Solution.go
@@ -7,10 +7,9 @@ func CheckPermutation(s1 string, s2 string) bool {
cnt[c-'a']++
}
for _, c := range s2 {
- cnt[c-'a']--
- if cnt[c-'a'] < 0 {
+ if cnt[c-'a']--; cnt[c-'a'] < 0 {
return false
}
}
return true
-}
\ No newline at end of file
+}
diff --git a/lcci/01.02.Check Permutation/Solution.js b/lcci/01.02.Check Permutation/Solution.js
index 4c0adedaf125b..7aa0ea9b1d428 100644
--- a/lcci/01.02.Check Permutation/Solution.js
+++ b/lcci/01.02.Check Permutation/Solution.js
@@ -4,19 +4,18 @@
* @return {boolean}
*/
var CheckPermutation = function (s1, s2) {
- if (s1.length != s2.length) {
+ if (s1.length !== s2.length) {
return false;
}
- const cnt = new Array(26).fill(0);
- for (let i = 0; i < s1.length; ++i) {
- const j = s1.codePointAt(i) - 'a'.codePointAt(0);
- ++cnt[j];
+ const cnt = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (let i = 0; i < s2.length; ++i) {
- const j = s2.codePointAt(i) - 'a'.codePointAt(0);
- if (--cnt[j] < 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
};
diff --git a/lcci/01.02.Check Permutation/Solution.rs b/lcci/01.02.Check Permutation/Solution.rs
index efe963a0130f2..45cda06875452 100644
--- a/lcci/01.02.Check Permutation/Solution.rs
+++ b/lcci/01.02.Check Permutation/Solution.rs
@@ -1,18 +1,22 @@
-use std::collections::HashMap;
impl Solution {
pub fn check_permutation(s1: String, s2: String) -> bool {
- let n = s1.len();
- let m = s2.len();
- if n != m {
+ if s1.len() != s2.len() {
return false;
}
- let s1 = s1.as_bytes();
- let s2 = s2.as_bytes();
- let mut map = HashMap::new();
- for i in 0..n {
- *map.entry(s1[i]).or_insert(0) += 1;
- *map.entry(s2[i]).or_insert(0) -= 1;
+
+ let mut cnt = vec![0; 26];
+ for c in s1.chars() {
+ cnt[(c as usize - 'a' as usize)] += 1;
}
- map.values().all(|i| *i == 0)
+
+ for c in s2.chars() {
+ let index = c as usize - 'a' as usize;
+ if cnt[index] == 0 {
+ return false;
+ }
+ cnt[index] -= 1;
+ }
+
+ true
}
}
diff --git a/lcci/01.02.Check Permutation/Solution.swift b/lcci/01.02.Check Permutation/Solution.swift
index 7161edfad155c..6bfd2af7416a5 100644
--- a/lcci/01.02.Check Permutation/Solution.swift
+++ b/lcci/01.02.Check Permutation/Solution.swift
@@ -3,22 +3,21 @@ class Solution {
if s1.count != s2.count {
return false
}
-
- var cnt = Array(repeating: 0, count: 26)
-
+
+ var cnt = [Int](repeating: 0, count: 26)
+
for char in s1 {
- let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] += 1
+ cnt[Int(char.asciiValue! - Character("a").asciiValue!)] += 1
}
-
+
for char in s2 {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] -= 1
- if cnt[index] < 0 {
+ if cnt[index] == 0 {
return false
}
+ cnt[index] -= 1
}
-
+
return true
}
-}
\ No newline at end of file
+}
diff --git a/lcci/01.02.Check Permutation/Solution.ts b/lcci/01.02.Check Permutation/Solution.ts
index 65f0c434d61b9..b6bb1f8822cec 100644
--- a/lcci/01.02.Check Permutation/Solution.ts
+++ b/lcci/01.02.Check Permutation/Solution.ts
@@ -1,18 +1,16 @@
function CheckPermutation(s1: string, s2: string): boolean {
- const n = s1.length;
- const m = s2.length;
- if (n !== m) {
+ if (s1.length !== s2.length) {
return false;
}
- const map = new Map();
- for (let i = 0; i < n; i++) {
- map.set(s1[i], (map.get(s1[i]) ?? 0) + 1);
- map.set(s2[i], (map.get(s2[i]) ?? 0) - 1);
+ const cnt: Record = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (const v of map.values()) {
- if (v !== 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
}
diff --git a/lcci/01.02.Check Permutation/Solution2.cpp b/lcci/01.02.Check Permutation/Solution2.cpp
index 70c67c68db593..142e3154d5a17 100644
--- a/lcci/01.02.Check Permutation/Solution2.cpp
+++ b/lcci/01.02.Check Permutation/Solution2.cpp
@@ -1,8 +1,8 @@
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- sort(s1.begin(), s1.end());
- sort(s2.begin(), s2.end());
+ ranges::sort(s1);
+ ranges::sort(s2);
return s1 == s2;
}
-};
\ No newline at end of file
+};
diff --git a/lcci/01.02.Check Permutation/Solution2.js b/lcci/01.02.Check Permutation/Solution2.js
new file mode 100644
index 0000000000000..a5d33c913da3a
--- /dev/null
+++ b/lcci/01.02.Check Permutation/Solution2.js
@@ -0,0 +1,8 @@
+/**
+ * @param {string} s1
+ * @param {string} s2
+ * @return {boolean}
+ */
+var CheckPermutation = function (s1, s2) {
+ return [...s1].sort().join('') === [...s2].sort().join('');
+};
diff --git a/lcci/01.02.Check Permutation/Solution2.swift b/lcci/01.02.Check Permutation/Solution2.swift
new file mode 100644
index 0000000000000..0aef38caddbb4
--- /dev/null
+++ b/lcci/01.02.Check Permutation/Solution2.swift
@@ -0,0 +1,7 @@
+class Solution {
+ func CheckPermutation(_ s1: String, _ s2: String) -> Bool {
+ let s1 = s1.sorted()
+ let s2 = s2.sorted()
+ return s1 == s2
+ }
+}
diff --git a/lcci/01.04.Palindrome Permutation/README.md b/lcci/01.04.Palindrome Permutation/README.md
index 689ccb5841266..b174846e8cd30 100644
--- a/lcci/01.04.Palindrome Permutation/README.md
+++ b/lcci/01.04.Palindrome Permutation/README.md
@@ -94,18 +94,15 @@ public:
```go
func canPermutePalindrome(s string) bool {
- vis := map[rune]bool{}
- cnt := 0
+ cnt := map[rune]int{}
for _, c := range s {
- if vis[c] {
- vis[c] = false
- cnt--
- } else {
- vis[c] = true
- cnt++
- }
+ cnt[c]++
}
- return cnt < 2
+ sum := 0
+ for _, v := range cnt {
+ sum += v & 1
+ }
+ return sum < 2
}
```
@@ -113,34 +110,26 @@ func canPermutePalindrome(s string) bool {
```ts
function canPermutePalindrome(s: string): boolean {
- const set = new Set();
+ const cnt: Record = {};
for (const c of s) {
- if (set.has(c)) {
- set.delete(c);
- } else {
- set.add(c);
- }
+ cnt[c] = (cnt[c] || 0) + 1;
}
- return set.size <= 1;
+ return Object.values(cnt).filter(v => v % 2 === 1).length < 2;
}
```
#### Rust
```rust
-use std::collections::HashSet;
+use std::collections::HashMap;
impl Solution {
pub fn can_permute_palindrome(s: String) -> bool {
- let mut set = HashSet::new();
+ let mut cnt = HashMap::new();
for c in s.chars() {
- if set.contains(&c) {
- set.remove(&c);
- } else {
- set.insert(c);
- }
+ *cnt.entry(c).or_insert(0) += 1;
}
- set.len() <= 1
+ cnt.values().filter(|&&v| v % 2 == 1).count() < 2
}
}
```
@@ -173,7 +162,7 @@ class Solution {
### 方法二:哈希表的另一种实现
-我们用哈希表 $vis$ 存储每个字符是否出现过。若出现过,则从哈希表中删除该字符;否则,将该字符加入哈希表。
+我们用一个哈希表 $\textit{vis}$ 存储每个字符是否出现过。若出现过,则从哈希表中删除该字符;否则,将该字符加入哈希表。
最后判断哈希表中字符的个数是否小于 $2$,若是,则是回文排列。
@@ -231,6 +220,76 @@ public:
};
```
+#### Go
+
+```go
+func canPermutePalindrome(s string) bool {
+ vis := map[rune]bool{}
+ for _, c := range s {
+ if vis[c] {
+ delete(vis, c)
+ } else {
+ vis[c] = true
+ }
+ }
+ return len(vis) < 2
+}
+```
+
+#### TypeScript
+
+```ts
+function canPermutePalindrome(s: string): boolean {
+ const vis = new Set();
+ for (const c of s) {
+ if (vis.has(c)) {
+ vis.delete(c);
+ } else {
+ vis.add(c);
+ }
+ }
+ return vis.size < 2;
+}
+```
+
+#### Rust
+
+```rust
+use std::collections::HashSet;
+
+impl Solution {
+ pub fn can_permute_palindrome(s: String) -> bool {
+ let mut vis = HashSet::new();
+ for c in s.chars() {
+ if vis.contains(&c) {
+ vis.remove(&c);
+ } else {
+ vis.insert(c);
+ }
+ }
+ vis.len() < 2
+ }
+}
+```
+
+#### Swift
+
+```swift
+class Solution {
+ func canPermutePalindrome(_ s: String) -> Bool {
+ var vis = Set()
+ for c in s {
+ if vis.contains(c) {
+ vis.remove(c)
+ } else {
+ vis.insert(c)
+ }
+ }
+ return vis.count < 2
+ }
+}
+```
+
diff --git a/lcci/01.04.Palindrome Permutation/README_EN.md b/lcci/01.04.Palindrome Permutation/README_EN.md
index e942aadc8b522..28d7e6c7c4c02 100644
--- a/lcci/01.04.Palindrome Permutation/README_EN.md
+++ b/lcci/01.04.Palindrome Permutation/README_EN.md
@@ -92,18 +92,15 @@ public:
```go
func canPermutePalindrome(s string) bool {
- vis := map[rune]bool{}
- cnt := 0
+ cnt := map[rune]int{}
for _, c := range s {
- if vis[c] {
- vis[c] = false
- cnt--
- } else {
- vis[c] = true
- cnt++
- }
+ cnt[c]++
}
- return cnt < 2
+ sum := 0
+ for _, v := range cnt {
+ sum += v & 1
+ }
+ return sum < 2
}
```
@@ -111,34 +108,26 @@ func canPermutePalindrome(s string) bool {
```ts
function canPermutePalindrome(s: string): boolean {
- const set = new Set();
+ const cnt: Record = {};
for (const c of s) {
- if (set.has(c)) {
- set.delete(c);
- } else {
- set.add(c);
- }
+ cnt[c] = (cnt[c] || 0) + 1;
}
- return set.size <= 1;
+ return Object.values(cnt).filter(v => v % 2 === 1).length < 2;
}
```
#### Rust
```rust
-use std::collections::HashSet;
+use std::collections::HashMap;
impl Solution {
pub fn can_permute_palindrome(s: String) -> bool {
- let mut set = HashSet::new();
+ let mut cnt = HashMap::new();
for c in s.chars() {
- if set.contains(&c) {
- set.remove(&c);
- } else {
- set.insert(c);
- }
+ *cnt.entry(c).or_insert(0) += 1;
}
- set.len() <= 1
+ cnt.values().filter(|&&v| v % 2 == 1).count() < 2
}
}
```
@@ -229,6 +218,76 @@ public:
};
```
+#### Go
+
+```go
+func canPermutePalindrome(s string) bool {
+ vis := map[rune]bool{}
+ for _, c := range s {
+ if vis[c] {
+ delete(vis, c)
+ } else {
+ vis[c] = true
+ }
+ }
+ return len(vis) < 2
+}
+```
+
+#### TypeScript
+
+```ts
+function canPermutePalindrome(s: string): boolean {
+ const vis = new Set();
+ for (const c of s) {
+ if (vis.has(c)) {
+ vis.delete(c);
+ } else {
+ vis.add(c);
+ }
+ }
+ return vis.size < 2;
+}
+```
+
+#### Rust
+
+```rust
+use std::collections::HashSet;
+
+impl Solution {
+ pub fn can_permute_palindrome(s: String) -> bool {
+ let mut vis = HashSet::new();
+ for c in s.chars() {
+ if vis.contains(&c) {
+ vis.remove(&c);
+ } else {
+ vis.insert(c);
+ }
+ }
+ vis.len() < 2
+ }
+}
+```
+
+#### Swift
+
+```swift
+class Solution {
+ func canPermutePalindrome(_ s: String) -> Bool {
+ var vis = Set()
+ for c in s {
+ if vis.contains(c) {
+ vis.remove(c)
+ } else {
+ vis.insert(c)
+ }
+ }
+ return vis.count < 2
+ }
+}
+```
+
diff --git a/lcci/01.04.Palindrome Permutation/Solution.go b/lcci/01.04.Palindrome Permutation/Solution.go
index 5d156fd33340e..76d5c69a60a52 100644
--- a/lcci/01.04.Palindrome Permutation/Solution.go
+++ b/lcci/01.04.Palindrome Permutation/Solution.go
@@ -1,14 +1,11 @@
-func canPermutePalindrome(s string) bool {
- vis := map[rune]bool{}
- cnt := 0
- for _, c := range s {
- if vis[c] {
- vis[c] = false
- cnt--
- } else {
- vis[c] = true
- cnt++
- }
- }
- return cnt < 2
-}
\ No newline at end of file
+func canPermutePalindrome(s string) bool {
+ cnt := map[rune]int{}
+ for _, c := range s {
+ cnt[c]++
+ }
+ sum := 0
+ for _, v := range cnt {
+ sum += v & 1
+ }
+ return sum < 2
+}
diff --git a/lcci/01.04.Palindrome Permutation/Solution.rs b/lcci/01.04.Palindrome Permutation/Solution.rs
index 0d0e1b07a5e70..895e119d6b604 100644
--- a/lcci/01.04.Palindrome Permutation/Solution.rs
+++ b/lcci/01.04.Palindrome Permutation/Solution.rs
@@ -1,15 +1,11 @@
-use std::collections::HashSet;
+use std::collections::HashMap;
impl Solution {
pub fn can_permute_palindrome(s: String) -> bool {
- let mut set = HashSet::new();
+ let mut cnt = HashMap::new();
for c in s.chars() {
- if set.contains(&c) {
- set.remove(&c);
- } else {
- set.insert(c);
- }
+ *cnt.entry(c).or_insert(0) += 1;
}
- set.len() <= 1
+ cnt.values().filter(|&&v| v % 2 == 1).count() < 2
}
}
diff --git a/lcci/01.04.Palindrome Permutation/Solution.ts b/lcci/01.04.Palindrome Permutation/Solution.ts
index 7bd7505057358..44ba2bc906e83 100644
--- a/lcci/01.04.Palindrome Permutation/Solution.ts
+++ b/lcci/01.04.Palindrome Permutation/Solution.ts
@@ -1,11 +1,7 @@
function canPermutePalindrome(s: string): boolean {
- const set = new Set();
+ const cnt: Record = {};
for (const c of s) {
- if (set.has(c)) {
- set.delete(c);
- } else {
- set.add(c);
- }
+ cnt[c] = (cnt[c] || 0) + 1;
}
- return set.size <= 1;
+ return Object.values(cnt).filter(v => v % 2 === 1).length < 2;
}
diff --git a/lcci/01.04.Palindrome Permutation/Solution2.go b/lcci/01.04.Palindrome Permutation/Solution2.go
new file mode 100644
index 0000000000000..425935647c9f4
--- /dev/null
+++ b/lcci/01.04.Palindrome Permutation/Solution2.go
@@ -0,0 +1,11 @@
+func canPermutePalindrome(s string) bool {
+ vis := map[rune]bool{}
+ for _, c := range s {
+ if vis[c] {
+ delete(vis, c)
+ } else {
+ vis[c] = true
+ }
+ }
+ return len(vis) < 2
+}
diff --git a/lcci/01.04.Palindrome Permutation/Solution2.rs b/lcci/01.04.Palindrome Permutation/Solution2.rs
new file mode 100644
index 0000000000000..5ef83e50f0c4d
--- /dev/null
+++ b/lcci/01.04.Palindrome Permutation/Solution2.rs
@@ -0,0 +1,15 @@
+use std::collections::HashSet;
+
+impl Solution {
+ pub fn can_permute_palindrome(s: String) -> bool {
+ let mut vis = HashSet::new();
+ for c in s.chars() {
+ if vis.contains(&c) {
+ vis.remove(&c);
+ } else {
+ vis.insert(c);
+ }
+ }
+ vis.len() < 2
+ }
+}
diff --git a/lcci/01.04.Palindrome Permutation/Solution2.swift b/lcci/01.04.Palindrome Permutation/Solution2.swift
new file mode 100644
index 0000000000000..993dc395a5b33
--- /dev/null
+++ b/lcci/01.04.Palindrome Permutation/Solution2.swift
@@ -0,0 +1,13 @@
+class Solution {
+ func canPermutePalindrome(_ s: String) -> Bool {
+ var vis = Set()
+ for c in s {
+ if vis.contains(c) {
+ vis.remove(c)
+ } else {
+ vis.insert(c)
+ }
+ }
+ return vis.count < 2
+ }
+}
diff --git a/lcci/01.04.Palindrome Permutation/Solution2.ts b/lcci/01.04.Palindrome Permutation/Solution2.ts
new file mode 100644
index 0000000000000..c72db04c15438
--- /dev/null
+++ b/lcci/01.04.Palindrome Permutation/Solution2.ts
@@ -0,0 +1,11 @@
+function canPermutePalindrome(s: string): boolean {
+ const vis = new Set();
+ for (const c of s) {
+ if (vis.has(c)) {
+ vis.delete(c);
+ } else {
+ vis.add(c);
+ }
+ }
+ return vis.size < 2;
+}
diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md b/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md
index 77320242b4963..d84ddc3e91a90 100644
--- a/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md
+++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md
@@ -281,6 +281,34 @@ var search = function (nums, target) {
};
```
+#### C#
+
+```cs
+public class Solution {
+ public int Search(int[] nums, int target) {
+ int n = nums.Length;
+ int left = 0, right = n - 1;
+ while (left < right) {
+ int mid = (left + right) >> 1;
+ if (nums[0] <= nums[mid]) {
+ if (nums[0] <= target && target <= nums[mid]) {
+ right = mid;
+ } else {
+ left = mid + 1;
+ }
+ } else {
+ if (nums[mid] < target && target <= nums[n - 1]) {
+ left = mid + 1;
+ } else {
+ right = mid;
+ }
+ }
+ }
+ return nums[left] == target ? left : -1;
+ }
+}
+```
+
#### PHP
```php
diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md b/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md
index 0826fedd5b185..871221d1251a8 100644
--- a/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md
+++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md
@@ -269,6 +269,34 @@ var search = function (nums, target) {
};
```
+#### C#
+
+```cs
+public class Solution {
+ public int Search(int[] nums, int target) {
+ int n = nums.Length;
+ int left = 0, right = n - 1;
+ while (left < right) {
+ int mid = (left + right) >> 1;
+ if (nums[0] <= nums[mid]) {
+ if (nums[0] <= target && target <= nums[mid]) {
+ right = mid;
+ } else {
+ left = mid + 1;
+ }
+ } else {
+ if (nums[mid] < target && target <= nums[n - 1]) {
+ left = mid + 1;
+ } else {
+ right = mid;
+ }
+ }
+ }
+ return nums[left] == target ? left : -1;
+ }
+}
+```
+
#### PHP
```php
diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.cs b/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.cs
new file mode 100644
index 0000000000000..94ec080d4a5a9
--- /dev/null
+++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.cs
@@ -0,0 +1,23 @@
+public class Solution {
+ public int Search(int[] nums, int target) {
+ int n = nums.Length;
+ int left = 0, right = n - 1;
+ while (left < right) {
+ int mid = (left + right) >> 1;
+ if (nums[0] <= nums[mid]) {
+ if (nums[0] <= target && target <= nums[mid]) {
+ right = mid;
+ } else {
+ left = mid + 1;
+ }
+ } else {
+ if (nums[mid] < target && target <= nums[n - 1]) {
+ left = mid + 1;
+ } else {
+ right = mid;
+ }
+ }
+ }
+ return nums[left] == target ? left : -1;
+ }
+}
\ No newline at end of file
diff --git a/solution/1300-1399/1352.Product of the Last K Numbers/README.md b/solution/1300-1399/1352.Product of the Last K Numbers/README.md
index 4db8ec50691f2..a18cf267c0c0d 100644
--- a/solution/1300-1399/1352.Product of the Last K Numbers/README.md
+++ b/solution/1300-1399/1352.Product of the Last K Numbers/README.md
@@ -61,7 +61,7 @@ productOfNumbers.getProduct(2); // 返回 20 。最后 2 个数字的乘积是 5
productOfNumbers.getProduct(3); // 返回 40 。最后 3 个数字的乘积是 2 * 5 * 4 = 40
productOfNumbers.getProduct(4); // 返回 0 。最后 4 个数字的乘积是 0 * 2 * 5 * 4 = 0
productOfNumbers.add(8); // [3,0,2,5,4,8]
-productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4 * 8 = 32
+productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4 * 8 = 32
@@ -218,6 +218,52 @@ func (this *ProductOfNumbers) GetProduct(k int) int {
*/
```
+#### TypeScript
+
+```ts
+class ProductOfNumbers {
+ s = [1];
+
+ add(num: number): void {
+ if (num === 0) {
+ this.s = [1];
+ } else {
+ const i = this.s.length;
+ this.s[i] = this.s[i - 1] * num;
+ }
+ }
+
+ getProduct(k: number): number {
+ const i = this.s.length;
+ if (k > i - 1) return 0;
+ return this.s[i - 1] / this.s[i - k - 1];
+ }
+}
+```
+
+#### JavaScript
+
+```js
+class ProductOfNumbers {
+ s = [1];
+
+ add(num) {
+ if (num === 0) {
+ this.s = [1];
+ } else {
+ const i = this.s.length;
+ this.s[i] = this.s[i - 1] * num;
+ }
+ }
+
+ getProduct(k) {
+ const i = this.s.length;
+ if (k > i - 1) return 0;
+ return this.s[i - 1] / this.s[i - k - 1];
+ }
+}
+```
+
diff --git a/solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md b/solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md
index c22336b3e4557..5333ea38575f1 100644
--- a/solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md
+++ b/solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md
@@ -56,7 +56,7 @@ productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers
productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
productOfNumbers.add(8); // [3,0,2,5,4,8]
-productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
+productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
@@ -213,6 +213,52 @@ func (this *ProductOfNumbers) GetProduct(k int) int {
*/
```
+#### TypeScript
+
+```ts
+class ProductOfNumbers {
+ s = [1];
+
+ add(num: number): void {
+ if (num === 0) {
+ this.s = [1];
+ } else {
+ const i = this.s.length;
+ this.s[i] = this.s[i - 1] * num;
+ }
+ }
+
+ getProduct(k: number): number {
+ const i = this.s.length;
+ if (k > i - 1) return 0;
+ return this.s[i - 1] / this.s[i - k - 1];
+ }
+}
+```
+
+#### JavaScript
+
+```js
+class ProductOfNumbers {
+ s = [1];
+
+ add(num) {
+ if (num === 0) {
+ this.s = [1];
+ } else {
+ const i = this.s.length;
+ this.s[i] = this.s[i - 1] * num;
+ }
+ }
+
+ getProduct(k) {
+ const i = this.s.length;
+ if (k > i - 1) return 0;
+ return this.s[i - 1] / this.s[i - k - 1];
+ }
+}
+```
+
diff --git a/solution/1300-1399/1352.Product of the Last K Numbers/Solution.js b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.js
new file mode 100644
index 0000000000000..e1a7c08c5dcfe
--- /dev/null
+++ b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.js
@@ -0,0 +1,18 @@
+class ProductOfNumbers {
+ s = [1];
+
+ add(num) {
+ if (num === 0) {
+ this.s = [1];
+ } else {
+ const i = this.s.length;
+ this.s[i] = this.s[i - 1] * num;
+ }
+ }
+
+ getProduct(k) {
+ const i = this.s.length;
+ if (k > i - 1) return 0;
+ return this.s[i - 1] / this.s[i - k - 1];
+ }
+}
diff --git a/solution/1300-1399/1352.Product of the Last K Numbers/Solution.ts b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.ts
new file mode 100644
index 0000000000000..a05afac603acf
--- /dev/null
+++ b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.ts
@@ -0,0 +1,18 @@
+class ProductOfNumbers {
+ s = [1];
+
+ add(num: number): void {
+ if (num === 0) {
+ this.s = [1];
+ } else {
+ const i = this.s.length;
+ this.s[i] = this.s[i - 1] * num;
+ }
+ }
+
+ getProduct(k: number): number {
+ const i = this.s.length;
+ if (k > i - 1) return 0;
+ return this.s[i - 1] / this.s[i - k - 1];
+ }
+}
diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/README.md b/solution/1700-1799/1706.Where Will the Ball Fall/README.md
index 5b28a84722165..7e2f6bf2d9fae 100644
--- a/solution/1700-1799/1706.Where Will the Ball Fall/README.md
+++ b/solution/1700-1799/1706.Where Will the Ball Fall/README.md
@@ -84,7 +84,7 @@ b4 球开始放在第 4 列上,会卡在第 2、3 列和第 1 行之间的 "V"
### 方法一:分情况讨论 + DFS
-我们可以使用 DFS 来模拟球的运动过程,设计一个函数 $dfs(i, j)$,表示球从第 $i$ 行第 $j$ 列出发,最终会落在第几列。对于以下情况,球会卡住:
+我们可以使用 DFS 来模拟球的运动过程,设计一个函数 $\textit{dfs}(i, j)$,表示球从第 $i$ 行第 $j$ 列出发,最终会落在第几列。对于以下情况,球会卡住:
1. 球位于最左一列,并且球所在的单元格单元格挡板将球导向左侧
1. 球位于最右一列,并且此单元格挡板将球导向右侧
@@ -93,7 +93,7 @@ b4 球开始放在第 4 列上,会卡在第 2、3 列和第 1 行之间的 "V"
如果满足以上任意一种情况,我们就可以判断球会卡住,返回 $-1$。否则,我们就可以继续递归地寻找球的下一个位置。最后,如果球到了最后一行,我们就可以返回当前列的编号。
-时间复杂度 $O(m \times n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $grid$ 的行数和列数。
+时间复杂度 $O(m \times n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $\textit{grid}$ 的行数和列数。
@@ -249,11 +249,7 @@ function findBall(grid: number[][]): number[] {
return dfs(i + 1, j - 1);
}
};
- const ans: number[] = [];
- for (let j = 0; j < n; ++j) {
- ans.push(dfs(0, j));
- }
- return ans;
+ return Array.from({ length: n }, (_, j) => dfs(0, j));
}
```
@@ -281,15 +277,45 @@ impl Solution {
pub fn find_ball(grid: Vec>) -> Vec {
let m = grid.len();
let n = grid[0].len();
- let mut res = vec![0; n];
+ let mut ans = vec![0; n];
for i in 0..n {
- res[i] = Self::dfs(&grid, 0, i);
+ ans[i] = Self::dfs(&grid, 0, i);
}
- res
+ ans
}
}
```
+#### JavaScript
+
+```js
+/**
+ * @param {number[][]} grid
+ * @return {number[]}
+ */
+var findBall = function (grid) {
+ const m = grid.length;
+ const n = grid[0].length;
+ const dfs = (i, j) => {
+ if (i === m) {
+ return j;
+ }
+ if (grid[i][j] === 1) {
+ if (j === n - 1 || grid[i][j + 1] === -1) {
+ return -1;
+ }
+ return dfs(i + 1, j + 1);
+ } else {
+ if (j === 0 || grid[i][j - 1] === 1) {
+ return -1;
+ }
+ return dfs(i + 1, j - 1);
+ }
+ };
+ return Array.from({ length: n }, (_, j) => dfs(0, j));
+};
+```
+
diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md b/solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md
index e0ac840dd1195..89c141f0cd0cf 100644
--- a/solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md
+++ b/solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md
@@ -80,18 +80,18 @@ Ball b4 is dropped at column 4 and will get stuck on the box between column 2 an
-### Solution 1: Case Discussion + DFS
+### Solution 1: Case Analysis + DFS
-We can use DFS to simulate the movement of the ball. We design a function $dfs(i, j)$, which represents that the ball starts from the $i$th row and the $j$th column, and finally falls in which column. The ball will get stuck in the following situations:
+We can use DFS to simulate the movement of the ball. Design a function $\textit{dfs}(i, j)$, which represents the column where the ball will fall when it starts from row $i$ and column $j$. The ball will get stuck in the following cases:
-1. The ball is in the leftmost column, and the cell's vane directs the ball to the left.
-2. The ball is in the rightmost column, and the cell's vane directs the ball to the right.
-3. The cell's vane where the ball is located directs the ball to the right, and the vane of the cell adjacent to the right of the ball directs the ball to the left.
-4. The cell's vane where the ball is located directs the ball to the left, and the vane of the cell adjacent to the left of the ball directs the ball to the right.
+1. The ball is in the leftmost column, and the cell's diagonal directs the ball to the left.
+2. The ball is in the rightmost column, and the cell's diagonal directs the ball to the right.
+3. The cell's diagonal directs the ball to the right, and the adjacent cell to the right directs the ball to the left.
+4. The cell's diagonal directs the ball to the left, and the adjacent cell to the left directs the ball to the right.
-If any of the above situations are met, we can judge that the ball will get stuck and return $-1$. Otherwise, we can continue to recursively find the next position of the ball. Finally, if the ball reaches the last row, we can return the current column number.
+If any of the above conditions are met, we can determine that the ball will get stuck and return $-1$. Otherwise, we can continue to recursively find the next position of the ball. Finally, if the ball reaches the last row, we can return the current column index.
-The time complexity is $O(m \times n)$, and the space complexity is $O(m)$. Where $m$ and $n$ are the number of rows and columns of the array $grid$, respectively.
+The time complexity is $O(m \times n)$, and the space complexity is $O(m)$. Here, $m$ and $n$ are the number of rows and columns of the grid, respectively.
@@ -247,11 +247,7 @@ function findBall(grid: number[][]): number[] {
return dfs(i + 1, j - 1);
}
};
- const ans: number[] = [];
- for (let j = 0; j < n; ++j) {
- ans.push(dfs(0, j));
- }
- return ans;
+ return Array.from({ length: n }, (_, j) => dfs(0, j));
}
```
@@ -279,15 +275,45 @@ impl Solution {
pub fn find_ball(grid: Vec>) -> Vec {
let m = grid.len();
let n = grid[0].len();
- let mut res = vec![0; n];
+ let mut ans = vec![0; n];
for i in 0..n {
- res[i] = Self::dfs(&grid, 0, i);
+ ans[i] = Self::dfs(&grid, 0, i);
}
- res
+ ans
}
}
```
+#### JavaScript
+
+```js
+/**
+ * @param {number[][]} grid
+ * @return {number[]}
+ */
+var findBall = function (grid) {
+ const m = grid.length;
+ const n = grid[0].length;
+ const dfs = (i, j) => {
+ if (i === m) {
+ return j;
+ }
+ if (grid[i][j] === 1) {
+ if (j === n - 1 || grid[i][j + 1] === -1) {
+ return -1;
+ }
+ return dfs(i + 1, j + 1);
+ } else {
+ if (j === 0 || grid[i][j - 1] === 1) {
+ return -1;
+ }
+ return dfs(i + 1, j - 1);
+ }
+ };
+ return Array.from({ length: n }, (_, j) => dfs(0, j));
+};
+```
+
diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.js b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.js
new file mode 100644
index 0000000000000..4c95d25ee3303
--- /dev/null
+++ b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.js
@@ -0,0 +1,25 @@
+/**
+ * @param {number[][]} grid
+ * @return {number[]}
+ */
+var findBall = function (grid) {
+ const m = grid.length;
+ const n = grid[0].length;
+ const dfs = (i, j) => {
+ if (i === m) {
+ return j;
+ }
+ if (grid[i][j] === 1) {
+ if (j === n - 1 || grid[i][j + 1] === -1) {
+ return -1;
+ }
+ return dfs(i + 1, j + 1);
+ } else {
+ if (j === 0 || grid[i][j - 1] === 1) {
+ return -1;
+ }
+ return dfs(i + 1, j - 1);
+ }
+ };
+ return Array.from({ length: n }, (_, j) => dfs(0, j));
+};
diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.rs b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.rs
index f2b7620fd18f0..2522351337a6b 100644
--- a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.rs
+++ b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.rs
@@ -19,10 +19,10 @@ impl Solution {
pub fn find_ball(grid: Vec>) -> Vec {
let m = grid.len();
let n = grid[0].len();
- let mut res = vec![0; n];
+ let mut ans = vec![0; n];
for i in 0..n {
- res[i] = Self::dfs(&grid, 0, i);
+ ans[i] = Self::dfs(&grid, 0, i);
}
- res
+ ans
}
}
diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.ts b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.ts
index d8a275b8dbfbb..c8ad783bb9b91 100644
--- a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.ts
+++ b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.ts
@@ -17,9 +17,5 @@ function findBall(grid: number[][]): number[] {
return dfs(i + 1, j - 1);
}
};
- const ans: number[] = [];
- for (let j = 0; j < n; ++j) {
- ans.push(dfs(0, j));
- }
- return ans;
+ return Array.from({ length: n }, (_, j) => dfs(0, j));
}