diff --git a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md
index fb25fd9622540..d3c8e340c3d81 100644
--- a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md
+++ b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md
@@ -50,7 +50,7 @@ X++:X 加 1 ,X = 0 + 1 = 1
输入:operations = ["++X","++X","X++"]
输出:3
-解释:操作按下述步骤执行:
+解释:操作按下述步骤执行:
最初,X = 0
++X:X 加 1 ,X = 0 + 1 = 1
++X:X 加 1 ,X = 1 + 1 = 2
@@ -85,11 +85,11 @@ X--:X 减 1 ,X = 1 - 1 = 0
-### 方法一:模拟
+### 方法一:计数
-遍历数组 `operations`,对于每个操作 $operations[i]$,如果包含 `'+'`,那么答案加 $1$,否则答案减 $1$。
+我们遍历数组 $\textit{operations}$,对于每个操作 $\textit{operations}[i]$,如果包含 `'+'`,那么答案加 $1$,否则答案减 $1$。
-时间复杂度为 $O(n)$,其中 $n$ 为数组 `operations` 的长度。空间复杂度 $O(1)$。
+时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{operations}$ 的长度。空间复杂度 $O(1)$。
@@ -122,7 +122,9 @@ class Solution {
public:
int finalValueAfterOperations(vector& operations) {
int ans = 0;
- for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
+ for (auto& s : operations) {
+ ans += s[1] == '+' ? 1 : -1;
+ }
return ans;
}
};
@@ -147,11 +149,7 @@ func finalValueAfterOperations(operations []string) (ans int) {
```ts
function finalValueAfterOperations(operations: string[]): number {
- let ans = 0;
- for (let operation of operations) {
- ans += operation.includes('+') ? 1 : -1;
- }
- return ans;
+ return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
}
```
@@ -177,11 +175,7 @@ impl Solution {
* @return {number}
*/
var finalValueAfterOperations = function (operations) {
- let ans = 0;
- for (const s of operations) {
- ans += s[1] === '+' ? 1 : -1;
- }
- return ans;
+ return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
};
```
@@ -201,22 +195,4 @@ int finalValueAfterOperations(char** operations, int operationsSize) {
-
-
-### 方法二
-
-
-
-#### TypeScript
-
-```ts
-function finalValueAfterOperations(operations: string[]): number {
- return operations.reduce((r, v) => r + (v[1] === '+' ? 1 : -1), 0);
-}
-```
-
-
-
-
-
diff --git a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README_EN.md b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README_EN.md
index ade0b3ec54916..6a28786b3e0a3 100644
--- a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README_EN.md
+++ b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README_EN.md
@@ -83,11 +83,11 @@ X--: X is decremented by 1, X = 1 - 1 = 0.
-### Solution 1: Simulation
+### Solution 1: Counting
-Traverse the array `operations`. For each operation $operations[i]$, if it contains `'+'`, then the answer increases by $1$, otherwise the answer decreases by $1$.
+We traverse the array $\textit{operations}$. For each operation $\textit{operations}[i]$, if it contains `'+'`, we increment the answer by $1$, otherwise, we decrement the answer by $1$.
-The time complexity is $O(n)$, where $n$ is the length of the array `operations`. The space complexity is $O(1)$.
+The time complexity is $O(n)$, where $n$ is the length of the array $\textit{operations}$. The space complexity is $O(1)$.
@@ -120,7 +120,9 @@ class Solution {
public:
int finalValueAfterOperations(vector& operations) {
int ans = 0;
- for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
+ for (auto& s : operations) {
+ ans += s[1] == '+' ? 1 : -1;
+ }
return ans;
}
};
@@ -145,11 +147,7 @@ func finalValueAfterOperations(operations []string) (ans int) {
```ts
function finalValueAfterOperations(operations: string[]): number {
- let ans = 0;
- for (let operation of operations) {
- ans += operation.includes('+') ? 1 : -1;
- }
- return ans;
+ return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
}
```
@@ -175,11 +173,7 @@ impl Solution {
* @return {number}
*/
var finalValueAfterOperations = function (operations) {
- let ans = 0;
- for (const s of operations) {
- ans += s[1] === '+' ? 1 : -1;
- }
- return ans;
+ return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
};
```
@@ -199,22 +193,4 @@ int finalValueAfterOperations(char** operations, int operationsSize) {
-
-
-### Solution 2
-
-
-
-#### TypeScript
-
-```ts
-function finalValueAfterOperations(operations: string[]): number {
- return operations.reduce((r, v) => r + (v[1] === '+' ? 1 : -1), 0);
-}
-```
-
-
-
-
-
diff --git a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.cpp b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.cpp
index 7e05fc3bd911a..75fb624cc8acc 100644
--- a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.cpp
+++ b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.cpp
@@ -2,7 +2,9 @@ class Solution {
public:
int finalValueAfterOperations(vector& operations) {
int ans = 0;
- for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
+ for (auto& s : operations) {
+ ans += s[1] == '+' ? 1 : -1;
+ }
return ans;
}
-};
\ No newline at end of file
+};
diff --git a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.js b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.js
index c23c95d361e84..02583d004cbe2 100644
--- a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.js
+++ b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.js
@@ -3,9 +3,5 @@
* @return {number}
*/
var finalValueAfterOperations = function (operations) {
- let ans = 0;
- for (const s of operations) {
- ans += s[1] === '+' ? 1 : -1;
- }
- return ans;
+ return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
};
diff --git a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.ts b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.ts
index acc0823407810..3595a617d6b1e 100644
--- a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.ts
+++ b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.ts
@@ -1,7 +1,3 @@
function finalValueAfterOperations(operations: string[]): number {
- let ans = 0;
- for (let operation of operations) {
- ans += operation.includes('+') ? 1 : -1;
- }
- return ans;
+ return operations.reduce((acc, op) => acc + (op[1] === '+' ? 1 : -1), 0);
}
diff --git a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution2.ts b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution2.ts
deleted file mode 100644
index c5c55d14ccd3e..0000000000000
--- a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution2.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-function finalValueAfterOperations(operations: string[]): number {
- return operations.reduce((r, v) => r + (v[1] === '+' ? 1 : -1), 0);
-}
diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md
index db36e13eca450..38ace23072d25 100644
--- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md
+++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md
@@ -52,7 +52,7 @@ tags:
输入:nums = [1,2,3,4]
输出:1
解释:
-子数组按位与运算的最大值是 4 。
+子数组按位与运算的最大值是 4 。
能得到此结果的最长子数组是 [4],所以返回 1 。
@@ -77,9 +77,9 @@ tags:
题目可以转换为求最大值在数组中最多连续出现的次数。
-先遍历一遍数组,求出最大值,然后再遍历一遍数组,求出最大值连续出现的次数,最后返回这个次数即可。
+我们先遍历数组 $\textit{nums}$ 找到最大值 $\textit{mx}$,然后再遍历数组一次,找到最大值连续出现的次数,最后返回这个次数即可。
-时间复杂度 $O(n)$。其中 $n$ 为数组的长度。
+时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
@@ -90,8 +90,8 @@ class Solution:
def longestSubarray(self, nums: List[int]) -> int:
mx = max(nums)
ans = cnt = 0
- for v in nums:
- if v == mx:
+ for x in nums:
+ if x == mx:
cnt += 1
ans = max(ans, cnt)
else:
@@ -104,15 +104,11 @@ class Solution:
```java
class Solution {
public int longestSubarray(int[] nums) {
- int mx = 0;
- for (int v : nums) {
- mx = Math.max(mx, v);
- }
+ int mx = Arrays.stream(nums).max().getAsInt();
int ans = 0, cnt = 0;
- for (int v : nums) {
- if (v == mx) {
- ++cnt;
- ans = Math.max(ans, cnt);
+ for (int x : nums) {
+ if (x == mx) {
+ ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
@@ -128,12 +124,11 @@ class Solution {
class Solution {
public:
int longestSubarray(vector& nums) {
- int mx = *max_element(nums.begin(), nums.end());
+ int mx = ranges::max(nums);
int ans = 0, cnt = 0;
- for (int v : nums) {
- if (v == mx) {
- ++cnt;
- ans = max(ans, cnt);
+ for (int x : nums) {
+ if (x == mx) {
+ ans = max(ans, ++cnt);
} else {
cnt = 0;
}
@@ -146,18 +141,18 @@ public:
#### Go
```go
-func longestSubarray(nums []int) int {
+func longestSubarray(nums []int) (ans int) {
mx := slices.Max(nums)
- ans, cnt := 0, 0
- for _, v := range nums {
- if v == mx {
+ cnt := 0
+ for _, x := range nums {
+ if x == mx {
cnt++
ans = max(ans, cnt)
} else {
cnt = 0
}
}
- return ans
+ return
}
```
@@ -167,38 +162,59 @@ func longestSubarray(nums []int) int {
function longestSubarray(nums: number[]): number {
const mx = Math.max(...nums);
let [ans, cnt] = [0, 0];
-
for (const x of nums) {
if (x === mx) {
- cnt++;
- ans = Math.max(ans, cnt);
+ ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}
-
return ans;
}
```
+#### Rust
+
+```rust
+impl Solution {
+ pub fn longest_subarray(nums: Vec) -> i32 {
+ let mx = *nums.iter().max().unwrap();
+ let mut ans = 0;
+ let mut cnt = 0;
+
+ for &x in nums.iter() {
+ if x == mx {
+ cnt += 1;
+ ans = ans.max(cnt);
+ } else {
+ cnt = 0;
+ }
+ }
+
+ ans
+ }
+}
+```
+
#### JavaScript
```js
-function longestSubarray(nums) {
+/**
+ * @param {number[]} nums
+ * @return {number}
+ */
+var longestSubarray = function (nums) {
const mx = Math.max(...nums);
let [ans, cnt] = [0, 0];
-
for (const x of nums) {
if (x === mx) {
- cnt++;
- ans = Math.max(ans, cnt);
+ ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}
-
return ans;
-}
+};
```
diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md
index 83dd92a8226b9..c95729b44a910 100644
--- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md
+++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md
@@ -69,15 +69,15 @@ The longest subarray with that value is [4], so we return 1.
-### Solution 1: Quick Thinking
+### Solution 1: Brain Teaser
-Due to the bitwise AND operation, the number will not get larger, so the maximum value is the maximum value in the array.
+Since the bitwise AND operation does not increase the number, the maximum value is the maximum value in the array.
-The problem can be transformed into finding the maximum number of consecutive occurrences of the maximum value in the array.
+The problem can be converted to finding the maximum number of consecutive occurrences of the maximum value in the array.
-First, traverse the array once to find the maximum value, then traverse the array again to find the number of consecutive occurrences of the maximum value, and finally return this count.
+First, traverse the array $\textit{nums}$ to find the maximum value $\textit{mx}$, then traverse the array again to find the maximum number of consecutive occurrences of the maximum value. Finally, return this count.
-The time complexity is $O(n)$, where $n$ is the length of the array.
+The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
@@ -88,8 +88,8 @@ class Solution:
def longestSubarray(self, nums: List[int]) -> int:
mx = max(nums)
ans = cnt = 0
- for v in nums:
- if v == mx:
+ for x in nums:
+ if x == mx:
cnt += 1
ans = max(ans, cnt)
else:
@@ -102,15 +102,11 @@ class Solution:
```java
class Solution {
public int longestSubarray(int[] nums) {
- int mx = 0;
- for (int v : nums) {
- mx = Math.max(mx, v);
- }
+ int mx = Arrays.stream(nums).max().getAsInt();
int ans = 0, cnt = 0;
- for (int v : nums) {
- if (v == mx) {
- ++cnt;
- ans = Math.max(ans, cnt);
+ for (int x : nums) {
+ if (x == mx) {
+ ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
@@ -126,12 +122,11 @@ class Solution {
class Solution {
public:
int longestSubarray(vector& nums) {
- int mx = *max_element(nums.begin(), nums.end());
+ int mx = ranges::max(nums);
int ans = 0, cnt = 0;
- for (int v : nums) {
- if (v == mx) {
- ++cnt;
- ans = max(ans, cnt);
+ for (int x : nums) {
+ if (x == mx) {
+ ans = max(ans, ++cnt);
} else {
cnt = 0;
}
@@ -144,18 +139,18 @@ public:
#### Go
```go
-func longestSubarray(nums []int) int {
+func longestSubarray(nums []int) (ans int) {
mx := slices.Max(nums)
- ans, cnt := 0, 0
- for _, v := range nums {
- if v == mx {
+ cnt := 0
+ for _, x := range nums {
+ if x == mx {
cnt++
ans = max(ans, cnt)
} else {
cnt = 0
}
}
- return ans
+ return
}
```
@@ -165,38 +160,59 @@ func longestSubarray(nums []int) int {
function longestSubarray(nums: number[]): number {
const mx = Math.max(...nums);
let [ans, cnt] = [0, 0];
-
for (const x of nums) {
if (x === mx) {
- cnt++;
- ans = Math.max(ans, cnt);
+ ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}
-
return ans;
}
```
+#### Rust
+
+```rust
+impl Solution {
+ pub fn longest_subarray(nums: Vec) -> i32 {
+ let mx = *nums.iter().max().unwrap();
+ let mut ans = 0;
+ let mut cnt = 0;
+
+ for &x in nums.iter() {
+ if x == mx {
+ cnt += 1;
+ ans = ans.max(cnt);
+ } else {
+ cnt = 0;
+ }
+ }
+
+ ans
+ }
+}
+```
+
#### JavaScript
```js
-function longestSubarray(nums) {
+/**
+ * @param {number[]} nums
+ * @return {number}
+ */
+var longestSubarray = function (nums) {
const mx = Math.max(...nums);
let [ans, cnt] = [0, 0];
-
for (const x of nums) {
if (x === mx) {
- cnt++;
- ans = Math.max(ans, cnt);
+ ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}
-
return ans;
-}
+};
```
diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.cpp b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.cpp
index 257bd360db5d9..4150badd4cfb5 100644
--- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.cpp
+++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.cpp
@@ -1,16 +1,15 @@
class Solution {
public:
int longestSubarray(vector& nums) {
- int mx = *max_element(nums.begin(), nums.end());
+ int mx = ranges::max(nums);
int ans = 0, cnt = 0;
- for (int v : nums) {
- if (v == mx) {
- ++cnt;
- ans = max(ans, cnt);
+ for (int x : nums) {
+ if (x == mx) {
+ ans = max(ans, ++cnt);
} else {
cnt = 0;
}
}
return ans;
}
-};
\ No newline at end of file
+};
diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.go b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.go
index b0a6a2dc19d18..90851d9be9877 100644
--- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.go
+++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.go
@@ -1,13 +1,13 @@
-func longestSubarray(nums []int) int {
+func longestSubarray(nums []int) (ans int) {
mx := slices.Max(nums)
- ans, cnt := 0, 0
- for _, v := range nums {
- if v == mx {
+ cnt := 0
+ for _, x := range nums {
+ if x == mx {
cnt++
ans = max(ans, cnt)
} else {
cnt = 0
}
}
- return ans
-}
\ No newline at end of file
+ return
+}
diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.java b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.java
index 00970ca639bd8..655523166ca00 100644
--- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.java
+++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.java
@@ -1,18 +1,14 @@
class Solution {
public int longestSubarray(int[] nums) {
- int mx = 0;
- for (int v : nums) {
- mx = Math.max(mx, v);
- }
+ int mx = Arrays.stream(nums).max().getAsInt();
int ans = 0, cnt = 0;
- for (int v : nums) {
- if (v == mx) {
- ++cnt;
- ans = Math.max(ans, cnt);
+ for (int x : nums) {
+ if (x == mx) {
+ ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}
return ans;
}
-}
\ No newline at end of file
+}
diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.js b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.js
index c949ce8d97802..05978ce953fdb 100644
--- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.js
+++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.js
@@ -1,15 +1,16 @@
-function longestSubarray(nums) {
+/**
+ * @param {number[]} nums
+ * @return {number}
+ */
+var longestSubarray = function (nums) {
const mx = Math.max(...nums);
let [ans, cnt] = [0, 0];
-
for (const x of nums) {
if (x === mx) {
- cnt++;
- ans = Math.max(ans, cnt);
+ ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}
-
return ans;
-}
+};
diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.py b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.py
index 490d34af6eeac..3e916dad58a08 100644
--- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.py
+++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.py
@@ -2,8 +2,8 @@ class Solution:
def longestSubarray(self, nums: List[int]) -> int:
mx = max(nums)
ans = cnt = 0
- for v in nums:
- if v == mx:
+ for x in nums:
+ if x == mx:
cnt += 1
ans = max(ans, cnt)
else:
diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.rs b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.rs
new file mode 100644
index 0000000000000..62e5eb18f3749
--- /dev/null
+++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.rs
@@ -0,0 +1,18 @@
+impl Solution {
+ pub fn longest_subarray(nums: Vec) -> i32 {
+ let mx = *nums.iter().max().unwrap();
+ let mut ans = 0;
+ let mut cnt = 0;
+
+ for &x in nums.iter() {
+ if x == mx {
+ cnt += 1;
+ ans = ans.max(cnt);
+ } else {
+ cnt = 0;
+ }
+ }
+
+ ans
+ }
+}
diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.ts b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.ts
index 4bd008ce7efba..149ff6a1b27a8 100644
--- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.ts
+++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.ts
@@ -1,15 +1,12 @@
function longestSubarray(nums: number[]): number {
const mx = Math.max(...nums);
let [ans, cnt] = [0, 0];
-
for (const x of nums) {
if (x === mx) {
- cnt++;
- ans = Math.max(ans, cnt);
+ ans = Math.max(ans, ++cnt);
} else {
cnt = 0;
}
}
-
return ans;
}