diff --git a/solution/0400-0499/0485.Max Consecutive Ones/README.md b/solution/0400-0499/0485.Max Consecutive Ones/README.md index 4246519b860bc..5379ec2227f87 100644 --- a/solution/0400-0499/0485.Max Consecutive Ones/README.md +++ b/solution/0400-0499/0485.Max Consecutive Ones/README.md @@ -52,9 +52,13 @@ tags: ### 方法一:一次遍历 -遍历数组,记录当前连续 $1$ 的个数 `cnt`,以及最大连续 $1$ 的个数 `ans`。如果当前元素为 $1$,则 `cnt++`,否则更新 `ans`,并且 `cnt=0`。最后返回 `max(ans, cnt)` 即可。 +我们可以遍历数组,用一个变量 $\textit{cnt}$ 记录当前连续的 1 的个数,用另一个变量 $\textit{ans}$ 记录最大连续 1 的个数。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。 +当遍历到一个 1 时,将 $\textit{cnt}$ 加一,然后更新 $\textit{ans}$ 的值为 $\textit{cnt}$ 和 $\textit{ans}$ 本身的最大值,即 $\textit{ans} = \max(\textit{ans}, \textit{cnt})$。否则,将 $\textit{cnt}$ 重置为 0。 + +遍历结束后,返回 $\textit{ans}$ 的值即可。 + +时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。 @@ -63,14 +67,14 @@ tags: ```python class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - cnt = ans = 0 - for v in nums: - if v == 1: + ans = cnt = 0 + for x in nums: + if x: cnt += 1 - else: ans = max(ans, cnt) + else: cnt = 0 - return max(ans, cnt) + return ans ``` #### Java @@ -78,16 +82,15 @@ class Solution: ```java class Solution { public int findMaxConsecutiveOnes(int[] nums) { - int cnt = 0, ans = 0; - for (int v : nums) { - if (v == 1) { - ++cnt; + int ans = 0, cnt = 0; + for (int x : nums) { + if (x == 1) { + ans = Math.max(ans, ++cnt); } else { - ans = Math.max(ans, cnt); cnt = 0; } } - return Math.max(cnt, ans); + return ans; } } ``` @@ -98,16 +101,15 @@ class Solution { class Solution { public: int findMaxConsecutiveOnes(vector& nums) { - int cnt = 0, ans = 0; - for (int v : nums) { - if (v == 1) { - ++cnt; + int ans = 0, cnt = 0; + for (int x : nums) { + if (x) { + ans = max(ans, ++cnt); } else { - ans = max(ans, cnt); cnt = 0; } } - return max(ans, cnt); + return ans; } }; ``` @@ -115,17 +117,17 @@ public: #### Go ```go -func findMaxConsecutiveOnes(nums []int) int { - ans, cnt := 0, 0 - for _, v := range nums { - if v == 1 { +func findMaxConsecutiveOnes(nums []int) (ans int) { + cnt := 0 + for _, x := range nums { + if x == 1 { cnt++ - } else { ans = max(ans, cnt) + } else { cnt = 0 } } - return max(ans, cnt) + return } ``` @@ -133,17 +135,15 @@ func findMaxConsecutiveOnes(nums []int) int { ```ts function findMaxConsecutiveOnes(nums: number[]): number { - let res = 0; - let count = 0; - for (const num of nums) { - if (num === 0) { - res = Math.max(res, count); - count = 0; + let [ans, cnt] = [0, 0]; + for (const x of nums) { + if (x) { + ans = Math.max(ans, ++cnt); } else { - count++; + cnt = 0; } } - return Math.max(res, count); + return ans; } ``` @@ -152,17 +152,19 @@ function findMaxConsecutiveOnes(nums: number[]): number { ```rust impl Solution { pub fn find_max_consecutive_ones(nums: Vec) -> i32 { - let mut res = 0; - let mut count = 0; - for num in nums { - if num == 0 { - res = res.max(count); - count = 0; + let mut ans = 0; + let mut cnt = 0; + + for &x in nums.iter() { + if x == 1 { + cnt += 1; + ans = ans.max(cnt); } else { - count += 1; + cnt = 0; } } - res.max(count) + + ans } } ``` @@ -175,17 +177,15 @@ impl Solution { * @return {number} */ var findMaxConsecutiveOnes = function (nums) { - let res = 0, - t = 0; - for (let num of nums) { - if (num == 1) { - ++t; + let [ans, cnt] = [0, 0]; + for (const x of nums) { + if (x) { + ans = Math.max(ans, ++cnt); } else { - res = Math.max(res, t); - t = 0; + cnt = 0; } } - return Math.max(res, t); + return ans; }; ``` @@ -198,16 +198,18 @@ class Solution { * @return Integer */ function findMaxConsecutiveOnes($nums) { - $tmp = $max = 0; - for ($i = 0; $i < count($nums); $i++) { - if ($nums[$i] == 1) { - $tmp++; + $ans = $cnt = 0; + + foreach ($nums as $x) { + if ($x == 1) { + $cnt += 1; + $ans = max($ans, $cnt); } else { - $max = max($tmp, $max); - $tmp = 0; + $cnt = 0; } } - return max($tmp, $max); + + return $ans; } } ``` diff --git a/solution/0400-0499/0485.Max Consecutive Ones/README_EN.md b/solution/0400-0499/0485.Max Consecutive Ones/README_EN.md index d40464c7e9a6e..1096ec919acc8 100644 --- a/solution/0400-0499/0485.Max Consecutive Ones/README_EN.md +++ b/solution/0400-0499/0485.Max Consecutive Ones/README_EN.md @@ -48,7 +48,15 @@ tags: -### Solution 1 +### Solution 1: Single Pass + +We can iterate through the array, using a variable $\textit{cnt}$ to record the current number of consecutive 1s, and another variable $\textit{ans}$ to record the maximum number of consecutive 1s. + +When we encounter a 1, we increment $\textit{cnt}$ by one, and then update $\textit{ans}$ to be the maximum of $\textit{cnt}$ and $\textit{ans}$ itself, i.e., $\textit{ans} = \max(\textit{ans}, \textit{cnt})$. Otherwise, we reset $\textit{cnt}$ to 0. + +After the iteration ends, we return the value of $\textit{ans}$. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -57,14 +65,14 @@ tags: ```python class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - cnt = ans = 0 - for v in nums: - if v == 1: + ans = cnt = 0 + for x in nums: + if x: cnt += 1 - else: ans = max(ans, cnt) + else: cnt = 0 - return max(ans, cnt) + return ans ``` #### Java @@ -72,16 +80,15 @@ class Solution: ```java class Solution { public int findMaxConsecutiveOnes(int[] nums) { - int cnt = 0, ans = 0; - for (int v : nums) { - if (v == 1) { - ++cnt; + int ans = 0, cnt = 0; + for (int x : nums) { + if (x == 1) { + ans = Math.max(ans, ++cnt); } else { - ans = Math.max(ans, cnt); cnt = 0; } } - return Math.max(cnt, ans); + return ans; } } ``` @@ -92,16 +99,15 @@ class Solution { class Solution { public: int findMaxConsecutiveOnes(vector& nums) { - int cnt = 0, ans = 0; - for (int v : nums) { - if (v == 1) { - ++cnt; + int ans = 0, cnt = 0; + for (int x : nums) { + if (x) { + ans = max(ans, ++cnt); } else { - ans = max(ans, cnt); cnt = 0; } } - return max(ans, cnt); + return ans; } }; ``` @@ -109,17 +115,17 @@ public: #### Go ```go -func findMaxConsecutiveOnes(nums []int) int { - ans, cnt := 0, 0 - for _, v := range nums { - if v == 1 { +func findMaxConsecutiveOnes(nums []int) (ans int) { + cnt := 0 + for _, x := range nums { + if x == 1 { cnt++ - } else { ans = max(ans, cnt) + } else { cnt = 0 } } - return max(ans, cnt) + return } ``` @@ -127,17 +133,15 @@ func findMaxConsecutiveOnes(nums []int) int { ```ts function findMaxConsecutiveOnes(nums: number[]): number { - let res = 0; - let count = 0; - for (const num of nums) { - if (num === 0) { - res = Math.max(res, count); - count = 0; + let [ans, cnt] = [0, 0]; + for (const x of nums) { + if (x) { + ans = Math.max(ans, ++cnt); } else { - count++; + cnt = 0; } } - return Math.max(res, count); + return ans; } ``` @@ -146,17 +150,19 @@ function findMaxConsecutiveOnes(nums: number[]): number { ```rust impl Solution { pub fn find_max_consecutive_ones(nums: Vec) -> i32 { - let mut res = 0; - let mut count = 0; - for num in nums { - if num == 0 { - res = res.max(count); - count = 0; + let mut ans = 0; + let mut cnt = 0; + + for &x in nums.iter() { + if x == 1 { + cnt += 1; + ans = ans.max(cnt); } else { - count += 1; + cnt = 0; } } - res.max(count) + + ans } } ``` @@ -169,17 +175,15 @@ impl Solution { * @return {number} */ var findMaxConsecutiveOnes = function (nums) { - let res = 0, - t = 0; - for (let num of nums) { - if (num == 1) { - ++t; + let [ans, cnt] = [0, 0]; + for (const x of nums) { + if (x) { + ans = Math.max(ans, ++cnt); } else { - res = Math.max(res, t); - t = 0; + cnt = 0; } } - return Math.max(res, t); + return ans; }; ``` @@ -192,16 +196,18 @@ class Solution { * @return Integer */ function findMaxConsecutiveOnes($nums) { - $tmp = $max = 0; - for ($i = 0; $i < count($nums); $i++) { - if ($nums[$i] == 1) { - $tmp++; + $ans = $cnt = 0; + + foreach ($nums as $x) { + if ($x == 1) { + $cnt += 1; + $ans = max($ans, $cnt); } else { - $max = max($tmp, $max); - $tmp = 0; + $cnt = 0; } } - return max($tmp, $max); + + return $ans; } } ``` diff --git a/solution/0400-0499/0485.Max Consecutive Ones/Solution.cpp b/solution/0400-0499/0485.Max Consecutive Ones/Solution.cpp index 029c098233363..8f30fd8aefa9d 100644 --- a/solution/0400-0499/0485.Max Consecutive Ones/Solution.cpp +++ b/solution/0400-0499/0485.Max Consecutive Ones/Solution.cpp @@ -1,15 +1,14 @@ class Solution { public: int findMaxConsecutiveOnes(vector& nums) { - int cnt = 0, ans = 0; - for (int v : nums) { - if (v == 1) { - ++cnt; + int ans = 0, cnt = 0; + for (int x : nums) { + if (x) { + ans = max(ans, ++cnt); } else { - ans = max(ans, cnt); cnt = 0; } } - return max(ans, cnt); + return ans; } }; \ No newline at end of file diff --git a/solution/0400-0499/0485.Max Consecutive Ones/Solution.go b/solution/0400-0499/0485.Max Consecutive Ones/Solution.go index b7f62d8694e90..294a937327526 100644 --- a/solution/0400-0499/0485.Max Consecutive Ones/Solution.go +++ b/solution/0400-0499/0485.Max Consecutive Ones/Solution.go @@ -1,12 +1,12 @@ -func findMaxConsecutiveOnes(nums []int) int { - ans, cnt := 0, 0 - for _, v := range nums { - if v == 1 { +func findMaxConsecutiveOnes(nums []int) (ans int) { + cnt := 0 + for _, x := range nums { + if x == 1 { cnt++ - } else { ans = max(ans, cnt) + } else { cnt = 0 } } - return max(ans, cnt) + return } \ No newline at end of file diff --git a/solution/0400-0499/0485.Max Consecutive Ones/Solution.java b/solution/0400-0499/0485.Max Consecutive Ones/Solution.java index 45eec679c1e47..ac54ff9b4cd95 100644 --- a/solution/0400-0499/0485.Max Consecutive Ones/Solution.java +++ b/solution/0400-0499/0485.Max Consecutive Ones/Solution.java @@ -1,14 +1,13 @@ class Solution { public int findMaxConsecutiveOnes(int[] nums) { - int cnt = 0, ans = 0; - for (int v : nums) { - if (v == 1) { - ++cnt; + int ans = 0, cnt = 0; + for (int x : nums) { + if (x == 1) { + ans = Math.max(ans, ++cnt); } else { - ans = Math.max(ans, cnt); cnt = 0; } } - return Math.max(cnt, ans); + return ans; } } \ No newline at end of file diff --git a/solution/0400-0499/0485.Max Consecutive Ones/Solution.js b/solution/0400-0499/0485.Max Consecutive Ones/Solution.js index 7c0e2c2f45d9c..c5bc657b533ec 100644 --- a/solution/0400-0499/0485.Max Consecutive Ones/Solution.js +++ b/solution/0400-0499/0485.Max Consecutive Ones/Solution.js @@ -3,15 +3,13 @@ * @return {number} */ var findMaxConsecutiveOnes = function (nums) { - let res = 0, - t = 0; - for (let num of nums) { - if (num == 1) { - ++t; + let [ans, cnt] = [0, 0]; + for (const x of nums) { + if (x) { + ans = Math.max(ans, ++cnt); } else { - res = Math.max(res, t); - t = 0; + cnt = 0; } } - return Math.max(res, t); + return ans; }; diff --git a/solution/0400-0499/0485.Max Consecutive Ones/Solution.php b/solution/0400-0499/0485.Max Consecutive Ones/Solution.php index 143d507224ae5..fc1a27a5b7a0a 100644 --- a/solution/0400-0499/0485.Max Consecutive Ones/Solution.php +++ b/solution/0400-0499/0485.Max Consecutive Ones/Solution.php @@ -4,15 +4,17 @@ class Solution { * @return Integer */ function findMaxConsecutiveOnes($nums) { - $tmp = $max = 0; - for ($i = 0; $i < count($nums); $i++) { - if ($nums[$i] == 1) { - $tmp++; + $ans = $cnt = 0; + + foreach ($nums as $x) { + if ($x == 1) { + $cnt += 1; + $ans = max($ans, $cnt); } else { - $max = max($tmp, $max); - $tmp = 0; + $cnt = 0; } } - return max($tmp, $max); + + return $ans; } -} +} \ No newline at end of file diff --git a/solution/0400-0499/0485.Max Consecutive Ones/Solution.py b/solution/0400-0499/0485.Max Consecutive Ones/Solution.py index 945552679fc40..707a4a65b1bbd 100644 --- a/solution/0400-0499/0485.Max Consecutive Ones/Solution.py +++ b/solution/0400-0499/0485.Max Consecutive Ones/Solution.py @@ -1,10 +1,10 @@ class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: - cnt = ans = 0 - for v in nums: - if v == 1: + ans = cnt = 0 + for x in nums: + if x: cnt += 1 - else: ans = max(ans, cnt) + else: cnt = 0 - return max(ans, cnt) + return ans diff --git a/solution/0400-0499/0485.Max Consecutive Ones/Solution.rs b/solution/0400-0499/0485.Max Consecutive Ones/Solution.rs index 03e8a8ad405f8..9ee7036b480e1 100644 --- a/solution/0400-0499/0485.Max Consecutive Ones/Solution.rs +++ b/solution/0400-0499/0485.Max Consecutive Ones/Solution.rs @@ -1,15 +1,17 @@ impl Solution { pub fn find_max_consecutive_ones(nums: Vec) -> i32 { - let mut res = 0; - let mut count = 0; - for num in nums { - if num == 0 { - res = res.max(count); - count = 0; + let mut ans = 0; + let mut cnt = 0; + + for &x in nums.iter() { + if x == 1 { + cnt += 1; + ans = ans.max(cnt); } else { - count += 1; + cnt = 0; } } - res.max(count) + + ans } } diff --git a/solution/0400-0499/0485.Max Consecutive Ones/Solution.ts b/solution/0400-0499/0485.Max Consecutive Ones/Solution.ts index 07d873c7552a8..db5813ab01533 100644 --- a/solution/0400-0499/0485.Max Consecutive Ones/Solution.ts +++ b/solution/0400-0499/0485.Max Consecutive Ones/Solution.ts @@ -1,13 +1,11 @@ function findMaxConsecutiveOnes(nums: number[]): number { - let res = 0; - let count = 0; - for (const num of nums) { - if (num === 0) { - res = Math.max(res, count); - count = 0; + let [ans, cnt] = [0, 0]; + for (const x of nums) { + if (x) { + ans = Math.max(ans, ++cnt); } else { - count++; + cnt = 0; } } - return Math.max(res, count); + return ans; }