当前位置:网站首页>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 <= 10001 <= 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).
边栏推荐
- Codeforces Round #799 (Div. 4)A~H
- Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog
- Educational Codeforces Round 130 (Rated for Div. 2)A~C
- Li Kou: the 81st biweekly match
- Codeforces round 797 (Div. 3) no f
- 第2章 HFDS的Shell操作
- 图像处理一百题(11-20)
- Research Report on market supply and demand and strategy of Chinese table lamp industry
- ByteDance new programmer's growth secret: those glittering treasures mentors
- (lightoj - 1349) Aladdin and the optimal invitation (greed)
猜你喜欢

Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog

Anaconda下安装Jupyter notebook

QT simulates mouse events and realizes clicking, double clicking, moving and dragging

软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客

简单尝试DeepFaceLab(DeepFake)的新AMP模型

Codeforces Round #801 (Div. 2)A~C

<li>圆点样式 list-style-type

Chapter 2 shell operation of hfds

Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
![Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)](/img/92/9465a6c9f1ab88c4851a47fabe750c.jpg)
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
随机推荐
Li Kou - 298th weekly match
input 只能输入数字,限定输入
js时间函数大全 详细的讲解 -----阿浩博客
SQL快速入门
Tert butyl hydroquinone (TBHQ) Industry Research Report - market status analysis and development prospect forecast
第5章 NameNode和SecondaryNameNode
Chapter 7__ consumer_ offsets topic
Codeforces Round #799 (Div. 4)A~H
Research Report on market supply and demand and strategy of double drum magnetic separator industry in China
Spark's RDD (elastic distributed data set) returns a large result set
Sublime text code formatting operation
Date plus 1 day
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
(lightoj - 1369) answering queries (thinking)
顺丰科技智慧物流校园技术挑战赛(无t4)
业务系统从Oracle迁移到openGauss数据库的简单记录
Research Report on hearing health care equipment industry - market status analysis and development prospect prediction
875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
Educational Codeforces Round 122 (Rated for Div. 2)
Remove the border when input is focused