当前位置:网站首页>LeetCode 1550. There are three consecutive arrays of odd numbers
LeetCode 1550. There are three consecutive arrays of odd numbers
2022-07-06 16:42:00 【Daylight629】
1550. There are three consecutive odd arrays
Give you an array of integers arr
, Please judge whether three consecutive elements in the array are odd : If there is , Please return true
; otherwise , return false
.
Example 1:
Input :arr = [2,6,4,1]
Output :false
explain : There is no case where three consecutive elements are odd .
Example 2:
Input :arr = [1,2,34,3,4,5,7,23,12]
Output :true
explain : There is a case where three consecutive elements are odd , namely [5,7,23] .
Tips :
1 <= arr.length <= 1000
1 <= arr[i] <= 1000
Two 、 Method 1
enumeration
class Solution {
public boolean threeConsecutiveOdds(int[] arr) {
int k = 0;
for (int i = 0; i < arr.length; i++) {
if ((arr[i] & 1) == 1) {
k++;
if (k == 3) {
return true;
}
} else {
k = 0;
}
}
return false;
}
}
Complexity analysis
- Time complexity :O(n).
- Spatial complexity :O(1).
3、 ... and 、 Method 2
enumeration
class Solution {
public boolean threeConsecutiveOdds(int[] arr) {
for (int i = 0; i < arr.length - 2; i++) {
if ((arr[i] & 1) == 1 && (arr[i + 1] & 1) == 1 && (arr[i + 2] & 1) == 1) {
return true;
}
}
return false;
}
}
Complexity analysis
- Time complexity :O(n).
- Spatial complexity :O(1).
边栏推荐
- Base dice (dynamic programming + matrix fast power)
- Codeforces Round #797 (Div. 3)无F
- 力扣leetcode第 280 场周赛
- Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
- Basic principles of video compression coding and audio compression coding
- Spark's RDD (elastic distributed data set) returns a large result set
- Bisphenol based CE Resin Industry Research Report - market status analysis and development prospect forecast
- Educational Codeforces Round 122 (Rated for Div. 2)
- 第6章 DataNode
- ffmpeg命令行使用
猜你喜欢
Codeforces Round #802(Div. 2)A~D
Hbuilder x format shortcut key settings
Audio and video development interview questions
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
Tree of life (tree DP)
Mp4 format details
SF smart logistics Campus Technology Challenge (no T4)
Remove the border when input is focused
Base dice (dynamic programming + matrix fast power)
解决Intel12代酷睿CPU单线程调度问题(二)
随机推荐
Click QT button to switch qlineedit focus (including code)
(POJ - 3186) treatments for the cows (interval DP)
Mp4 format details
CMake Error: Could not create named generator Visual Studio 16 2019解决方法
Chapter 5 detailed explanation of consumer groups
SQL快速入门
(POJ - 1458) common subsequence (longest common subsequence)
QT implementation fillet window
AcWing——第55场周赛
Chapter 1 overview of MapReduce
Research Report on hearing health care equipment industry - market status analysis and development prospect prediction
(lightoj - 1349) Aladdin and the optimal invitation (greed)
腾讯面试算法题
Hbuilder X格式化快捷键设置
生成随机密码/验证码
Research Report on market supply and demand and strategy of Chinese table lamp industry
Summary of FTP function implemented by qnetworkaccessmanager
Ffmpeg command line use
875. Leetcode, a banana lover
Research Report on market supply and demand and strategy of double drum magnetic separator industry in China