New Problem Solution "1550. Three Consecutive Odds"
This commit is contained in:
parent
ec2298a889
commit
1889a62f80
@ -10,6 +10,7 @@ LeetCode
|
||||
| # | Title | Solution | Difficulty |
|
||||
|---| ----- | -------- | ---------- |
|
||||
|1573|[Number of Ways to Split a String](https://leetcode.com/problems/number-of-ways-to-split-a-string/) | [C++](./algorithms/cpp/NumberOfWaysToSplitString/NumberOfWaysToSplitString.cpp)|Medium|
|
||||
|1550|[Three Consecutive Odds](https://leetcode.com/problems/three-consecutive-odds/) | [C++](./algorithms/cpp/threeConsecutiveOdds/ThreeConsecutiveOdds.cpp)|Easy|
|
||||
|1541|[Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/) | [C++](./algorithms/cpp/minimumInsertionsToBalanceAParenthesesString/MinimumInsertionsToBalanceAParenthesesString.cpp)|Medium|
|
||||
|1535|[Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/) | [C++](./algorithms/cpp/findTheWinnerOfAnArrayGame/FindTheWinnerOfAnArrayGame.cpp)|Medium|
|
||||
|1470|[Shuffle the Array](https://leetcode.com/problems/shuffle-the-array/) | [C++](./algorithms/cpp/shuffleTheArray/ShuffleTheArray.cpp)|Easy|
|
||||
|
40
algorithms/cpp/threeConsecutiveOdds/ThreeConsecutiveOdds.cpp
Normal file
40
algorithms/cpp/threeConsecutiveOdds/ThreeConsecutiveOdds.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
// Source : https://leetcode.com/problems/three-consecutive-odds/
|
||||
// Author : Hao Chen
|
||||
// Date : 2020-10-03
|
||||
|
||||
/*****************************************************************************************************
|
||||
*
|
||||
* Given an integer array arr, return true if there are three consecutive odd numbers in the array.
|
||||
* Otherwise, return false.
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: arr = [2,6,4,1]
|
||||
* Output: false
|
||||
* Explanation: There are no three consecutive odds.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: arr = [1,2,34,3,4,5,7,23,12]
|
||||
* Output: true
|
||||
* Explanation: [5,7,23] are three consecutive odds.
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= arr.length <= 1000
|
||||
* 1 <= arr[i] <= 1000
|
||||
******************************************************************************************************/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
|
||||
bool threeConsecutiveOdds(vector<int>& arr) {
|
||||
int cnt = 0;
|
||||
for (auto n : arr) {
|
||||
if ( n % 2 ) cnt++;
|
||||
else cnt = 0;
|
||||
if (cnt >=3) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user