diff --git a/solution/1500-1599/1550.Three Consecutive Odds/README.md b/solution/1500-1599/1550.Three Consecutive Odds/README.md index 7a86894d76374..36d12e53d91a7 100644 --- a/solution/1500-1599/1550.Three Consecutive Odds/README.md +++ b/solution/1500-1599/1550.Three Consecutive Odds/README.md @@ -51,11 +51,15 @@ tags: -### 方法一:遍历数组 +### 方法一:遍历 + 计数 -直接遍历数组,统计连续奇数的个数,如果个数达到 3,则返回 `true`,否则遍历结束,返回 `false`。 +我们用一个变量 $\text{cnt}$ 记录当前连续奇数的个数。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 为数组 `arr` 的长度。 +接下来,我们遍历数组,如果当前元素是奇数,则 $\text{cnt}$ 加一,如果 $\text{cnt}$ 等于 3,则返回 $\text{True}$。如果当前元素是偶数,则 $\text{cnt}$ 置零。 + +遍历结束后,如果没有找到连续三个奇数,则返回 $\text{False}$。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\text{arr}$ 的长度。空间复杂度 $O(1)$。 @@ -65,13 +69,13 @@ tags: class Solution: def threeConsecutiveOdds(self, arr: List[int]) -> bool: cnt = 0 - for v in arr: - if v & 1: + for x in arr: + if x & 1: cnt += 1 + if cnt == 3: + return True else: cnt = 0 - if cnt == 3: - return True return False ``` @@ -81,15 +85,14 @@ class Solution: class Solution { public boolean threeConsecutiveOdds(int[] arr) { int cnt = 0; - for (int v : arr) { - if (v % 2 == 1) { - ++cnt; + for (int x : arr) { + if (x % 2 == 1) { + if (++cnt == 3) { + return true; + } } else { cnt = 0; } - if (cnt == 3) { - return true; - } } return false; } @@ -103,12 +106,14 @@ class Solution { public: bool threeConsecutiveOdds(vector& arr) { int cnt = 0; - for (int v : arr) { - if (v & 1) - ++cnt; - else + for (int x : arr) { + if (x & 1) { + if (++cnt == 3) { + return true; + } + } else { cnt = 0; - if (cnt == 3) return true; + } } return false; } @@ -120,15 +125,15 @@ public: ```go func threeConsecutiveOdds(arr []int) bool { cnt := 0 - for _, v := range arr { - if v%2 == 1 { + for _, x := range arr { + if x&1 == 1 { cnt++ + if cnt == 3 { + return true + } } else { cnt = 0 } - if cnt == 3 { - return true - } } return false } @@ -139,15 +144,14 @@ func threeConsecutiveOdds(arr []int) bool { ```ts function threeConsecutiveOdds(arr: number[]): boolean { let cnt = 0; - for (const v of arr) { - if (v & 1) { - ++cnt; + for (const x of arr) { + if (x & 1) { + if (++cnt == 3) { + return true; + } } else { cnt = 0; } - if (cnt == 3) { - return true; - } } return false; } @@ -159,7 +163,13 @@ function threeConsecutiveOdds(arr: number[]): boolean { -### 方法二 +### 方法二:遍历 + 位运算 + +根据位运算的性质,两个数进行按位与运算是奇数,当且仅当两个数都是奇数。如果有连续三个数按位与运算的结果是奇数,那么这三个数都是奇数。 + +因此,我们只需要遍历数组,判断是否存在连续三个数的按位与结果是否是奇数即可,如果存在则返回 $\text{True}$,否则返回 $\text{False}$。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\text{arr}$ 的长度。空间复杂度 $O(1)$。 @@ -168,10 +178,65 @@ function threeConsecutiveOdds(arr: number[]): boolean { ```python class Solution: def threeConsecutiveOdds(self, arr: List[int]) -> bool: - for i in range(len(arr) - 2): - if arr[i] % 2 + arr[i + 1] % 2 + arr[i + 2] % 2 == 3: - return True - return False + return any(x & arr[i + 1] & arr[i + 2] & 1 for i, x in enumerate(arr[:-2])) +``` + +#### Java + +```java +class Solution { + public boolean threeConsecutiveOdds(int[] arr) { + for (int i = 2, n = arr.length; i < n; ++i) { + if ((arr[i - 2] & arr[i - 1] & arr[i] & 1) == 1) { + return true; + } + } + return false; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + bool threeConsecutiveOdds(vector& arr) { + for (int i = 2, n = arr.size(); i < n; ++i) { + if (arr[i - 2] & arr[i - 1] & arr[i] & 1) { + return true; + } + } + return false; + } +}; +``` + +#### Go + +```go +func threeConsecutiveOdds(arr []int) bool { + for i, n := 2, len(arr); i < n; i++ { + if arr[i-2]&arr[i-1]&arr[i]&1 == 1 { + return true + } + } + return false +} +``` + +#### TypeScript + +```ts +function threeConsecutiveOdds(arr: number[]): boolean { + const n = arr.length; + for (let i = 2; i < n; ++i) { + if (arr[i - 2] & arr[i - 1] & arr[i] & 1) { + return true; + } + } + return false; +} ``` diff --git a/solution/1500-1599/1550.Three Consecutive Odds/README_EN.md b/solution/1500-1599/1550.Three Consecutive Odds/README_EN.md index 407fbacf7bf52..f6b6381f1ceea 100644 --- a/solution/1500-1599/1550.Three Consecutive Odds/README_EN.md +++ b/solution/1500-1599/1550.Three Consecutive Odds/README_EN.md @@ -51,7 +51,15 @@ Given an integer array arr, return true if there -### Solution 1 +### Solution 1: Iteration + Counting + +We use a variable $\text{cnt}$ to record the current count of consecutive odd numbers. + +Next, we iterate through the array. If the current element is odd, then $\text{cnt}$ is incremented by one. If $\text{cnt}$ equals 3, then return $\text{True}$. If the current element is even, then $\text{cnt}$ is reset to zero. + +After the iteration, if three consecutive odd numbers are not found, then return $\text{False}$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\text{arr}$. The space complexity is $O(1)$. @@ -61,13 +69,13 @@ Given an integer array arr, return true if there class Solution: def threeConsecutiveOdds(self, arr: List[int]) -> bool: cnt = 0 - for v in arr: - if v & 1: + for x in arr: + if x & 1: cnt += 1 + if cnt == 3: + return True else: cnt = 0 - if cnt == 3: - return True return False ``` @@ -77,15 +85,14 @@ class Solution: class Solution { public boolean threeConsecutiveOdds(int[] arr) { int cnt = 0; - for (int v : arr) { - if (v % 2 == 1) { - ++cnt; + for (int x : arr) { + if (x % 2 == 1) { + if (++cnt == 3) { + return true; + } } else { cnt = 0; } - if (cnt == 3) { - return true; - } } return false; } @@ -99,12 +106,14 @@ class Solution { public: bool threeConsecutiveOdds(vector& arr) { int cnt = 0; - for (int v : arr) { - if (v & 1) - ++cnt; - else + for (int x : arr) { + if (x & 1) { + if (++cnt == 3) { + return true; + } + } else { cnt = 0; - if (cnt == 3) return true; + } } return false; } @@ -116,15 +125,15 @@ public: ```go func threeConsecutiveOdds(arr []int) bool { cnt := 0 - for _, v := range arr { - if v%2 == 1 { + for _, x := range arr { + if x&1 == 1 { cnt++ + if cnt == 3 { + return true + } } else { cnt = 0 } - if cnt == 3 { - return true - } } return false } @@ -135,15 +144,14 @@ func threeConsecutiveOdds(arr []int) bool { ```ts function threeConsecutiveOdds(arr: number[]): boolean { let cnt = 0; - for (const v of arr) { - if (v & 1) { - ++cnt; + for (const x of arr) { + if (x & 1) { + if (++cnt == 3) { + return true; + } } else { cnt = 0; } - if (cnt == 3) { - return true; - } } return false; } @@ -155,7 +163,13 @@ function threeConsecutiveOdds(arr: number[]): boolean { -### Solution 2 +### Solution 2: Iteration + Bitwise Operation + +Based on the properties of bitwise operations, the result of a bitwise AND operation between two numbers is odd if and only if both numbers are odd. If there are three consecutive numbers whose bitwise AND result is odd, then these three numbers are all odd. + +Therefore, we only need to iterate through the array and check if there exists three consecutive numbers whose bitwise AND result is odd. If such numbers exist, return $\text{True}$; otherwise, return $\text{False}$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\text{arr}$. The space complexity is $O(1)$. @@ -164,10 +178,65 @@ function threeConsecutiveOdds(arr: number[]): boolean { ```python class Solution: def threeConsecutiveOdds(self, arr: List[int]) -> bool: - for i in range(len(arr) - 2): - if arr[i] % 2 + arr[i + 1] % 2 + arr[i + 2] % 2 == 3: - return True - return False + return any(x & arr[i + 1] & arr[i + 2] & 1 for i, x in enumerate(arr[:-2])) +``` + +#### Java + +```java +class Solution { + public boolean threeConsecutiveOdds(int[] arr) { + for (int i = 2, n = arr.length; i < n; ++i) { + if ((arr[i - 2] & arr[i - 1] & arr[i] & 1) == 1) { + return true; + } + } + return false; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + bool threeConsecutiveOdds(vector& arr) { + for (int i = 2, n = arr.size(); i < n; ++i) { + if (arr[i - 2] & arr[i - 1] & arr[i] & 1) { + return true; + } + } + return false; + } +}; +``` + +#### Go + +```go +func threeConsecutiveOdds(arr []int) bool { + for i, n := 2, len(arr); i < n; i++ { + if arr[i-2]&arr[i-1]&arr[i]&1 == 1 { + return true + } + } + return false +} +``` + +#### TypeScript + +```ts +function threeConsecutiveOdds(arr: number[]): boolean { + const n = arr.length; + for (let i = 2; i < n; ++i) { + if (arr[i - 2] & arr[i - 1] & arr[i] & 1) { + return true; + } + } + return false; +} ``` diff --git a/solution/1500-1599/1550.Three Consecutive Odds/Solution.cpp b/solution/1500-1599/1550.Three Consecutive Odds/Solution.cpp index 93ea48ab51c14..e7d1c971422e4 100644 --- a/solution/1500-1599/1550.Three Consecutive Odds/Solution.cpp +++ b/solution/1500-1599/1550.Three Consecutive Odds/Solution.cpp @@ -2,13 +2,15 @@ class Solution { public: bool threeConsecutiveOdds(vector& arr) { int cnt = 0; - for (int v : arr) { - if (v & 1) - ++cnt; - else + for (int x : arr) { + if (x & 1) { + if (++cnt == 3) { + return true; + } + } else { cnt = 0; - if (cnt == 3) return true; + } } return false; } -}; \ No newline at end of file +}; diff --git a/solution/1500-1599/1550.Three Consecutive Odds/Solution.go b/solution/1500-1599/1550.Three Consecutive Odds/Solution.go index ffcb2363af09c..e08ef5207890b 100644 --- a/solution/1500-1599/1550.Three Consecutive Odds/Solution.go +++ b/solution/1500-1599/1550.Three Consecutive Odds/Solution.go @@ -1,14 +1,14 @@ func threeConsecutiveOdds(arr []int) bool { cnt := 0 - for _, v := range arr { - if v%2 == 1 { + for _, x := range arr { + if x&1 == 1 { cnt++ + if cnt == 3 { + return true + } } else { cnt = 0 } - if cnt == 3 { - return true - } } return false -} \ No newline at end of file +} diff --git a/solution/1500-1599/1550.Three Consecutive Odds/Solution.java b/solution/1500-1599/1550.Three Consecutive Odds/Solution.java index 82a56aef16f60..e578aab7468a8 100644 --- a/solution/1500-1599/1550.Three Consecutive Odds/Solution.java +++ b/solution/1500-1599/1550.Three Consecutive Odds/Solution.java @@ -1,16 +1,15 @@ class Solution { public boolean threeConsecutiveOdds(int[] arr) { int cnt = 0; - for (int v : arr) { - if (v % 2 == 1) { - ++cnt; + for (int x : arr) { + if (x % 2 == 1) { + if (++cnt == 3) { + return true; + } } else { cnt = 0; } - if (cnt == 3) { - return true; - } } return false; } -} \ No newline at end of file +} diff --git a/solution/1500-1599/1550.Three Consecutive Odds/Solution.py b/solution/1500-1599/1550.Three Consecutive Odds/Solution.py index ee2e71c3c26c7..017973cde2213 100644 --- a/solution/1500-1599/1550.Three Consecutive Odds/Solution.py +++ b/solution/1500-1599/1550.Three Consecutive Odds/Solution.py @@ -1,11 +1,11 @@ class Solution: def threeConsecutiveOdds(self, arr: List[int]) -> bool: cnt = 0 - for v in arr: - if v & 1: + for x in arr: + if x & 1: cnt += 1 + if cnt == 3: + return True else: cnt = 0 - if cnt == 3: - return True return False diff --git a/solution/1500-1599/1550.Three Consecutive Odds/Solution.ts b/solution/1500-1599/1550.Three Consecutive Odds/Solution.ts index faa220eddedf9..6d7b3aa0cd67f 100644 --- a/solution/1500-1599/1550.Three Consecutive Odds/Solution.ts +++ b/solution/1500-1599/1550.Three Consecutive Odds/Solution.ts @@ -1,14 +1,13 @@ function threeConsecutiveOdds(arr: number[]): boolean { let cnt = 0; - for (const v of arr) { - if (v & 1) { - ++cnt; + for (const x of arr) { + if (x & 1) { + if (++cnt == 3) { + return true; + } } else { cnt = 0; } - if (cnt == 3) { - return true; - } } return false; } diff --git a/solution/1500-1599/1550.Three Consecutive Odds/Solution2.cpp b/solution/1500-1599/1550.Three Consecutive Odds/Solution2.cpp new file mode 100644 index 0000000000000..6b627251bc4bf --- /dev/null +++ b/solution/1500-1599/1550.Three Consecutive Odds/Solution2.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + bool threeConsecutiveOdds(vector& arr) { + for (int i = 2, n = arr.size(); i < n; ++i) { + if (arr[i - 2] & arr[i - 1] & arr[i] & 1) { + return true; + } + } + return false; + } +}; diff --git a/solution/1500-1599/1550.Three Consecutive Odds/Solution2.go b/solution/1500-1599/1550.Three Consecutive Odds/Solution2.go new file mode 100644 index 0000000000000..69f0d8b3ac93d --- /dev/null +++ b/solution/1500-1599/1550.Three Consecutive Odds/Solution2.go @@ -0,0 +1,8 @@ +func threeConsecutiveOdds(arr []int) bool { + for i, n := 2, len(arr); i < n; i++ { + if arr[i-2]&arr[i-1]&arr[i]&1 == 1 { + return true + } + } + return false +} diff --git a/solution/1500-1599/1550.Three Consecutive Odds/Solution2.java b/solution/1500-1599/1550.Three Consecutive Odds/Solution2.java new file mode 100644 index 0000000000000..f32544742b00e --- /dev/null +++ b/solution/1500-1599/1550.Three Consecutive Odds/Solution2.java @@ -0,0 +1,10 @@ +class Solution { + public boolean threeConsecutiveOdds(int[] arr) { + for (int i = 2, n = arr.length; i < n; ++i) { + if ((arr[i - 2] & arr[i - 1] & arr[i] & 1) == 1) { + return true; + } + } + return false; + } +} diff --git a/solution/1500-1599/1550.Three Consecutive Odds/Solution2.py b/solution/1500-1599/1550.Three Consecutive Odds/Solution2.py index a0fa200a72249..a30985ae73238 100644 --- a/solution/1500-1599/1550.Three Consecutive Odds/Solution2.py +++ b/solution/1500-1599/1550.Three Consecutive Odds/Solution2.py @@ -1,6 +1,3 @@ class Solution: def threeConsecutiveOdds(self, arr: List[int]) -> bool: - for i in range(len(arr) - 2): - if arr[i] % 2 + arr[i + 1] % 2 + arr[i + 2] % 2 == 3: - return True - return False + return any(x & arr[i + 1] & arr[i + 2] & 1 for i, x in enumerate(arr[:-2])) diff --git a/solution/1500-1599/1550.Three Consecutive Odds/Solution2.ts b/solution/1500-1599/1550.Three Consecutive Odds/Solution2.ts new file mode 100644 index 0000000000000..eac001621e185 --- /dev/null +++ b/solution/1500-1599/1550.Three Consecutive Odds/Solution2.ts @@ -0,0 +1,9 @@ +function threeConsecutiveOdds(arr: number[]): boolean { + const n = arr.length; + for (let i = 2; i < n; ++i) { + if (arr[i - 2] & arr[i - 1] & arr[i] & 1) { + return true; + } + } + return false; +}