当前位置:网站首页>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).
边栏推荐
- Spark's RDD (elastic distributed data set) returns a large result set
- Problem - 922D、Robot Vacuum Cleaner - Codeforces
- QT simulates mouse events and realizes clicking, double clicking, moving and dragging
- 图像处理一百题(11-20)
- 字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
- Codeforces Round #803 (Div. 2)A~C
- 第5章 消费者组详解
- 业务系统从Oracle迁移到openGauss数据库的简单记录
- JS encapsulates the method of array inversion -- Feng Hao's blog
- (lightoj - 1370) Bi shoe and phi shoe (Euler function tabulation)
猜你喜欢
OneForAll安装使用
Install Jupiter notebook under Anaconda
Advancedinstaller installation package custom action open file
解决Intel12代酷睿CPU单线程调度问题(二)
业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
<li>圆点样式 list-style-type
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
去掉input聚焦时的边框
第7章 __consumer_offsets topic
第5章 NameNode和SecondaryNameNode
随机推荐
Market trend report, technological innovation and market forecast of double door and multi door refrigerators in China
Click QT button to switch qlineedit focus (including code)
Kubernetes集群部署
两个礼拜速成软考中级软件设计师经验
第一章 MapReduce概述
Codeforces Round #801 (Div. 2)A~C
input 只能输入数字,限定输入
Chapter 5 namenode and secondarynamenode
(lightoj - 1323) billiard balls (thinking)
SQL快速入门
Market trend report, technical innovation and market forecast of double-sided foam tape in China
Li Kou - 298th weekly match
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
(lightoj - 1370) Bi shoe and phi shoe (Euler function tabulation)
Hbuilder x format shortcut key settings
Date plus 1 day
< li> dot style list style type
第5章 消费者组详解
Codeforces Round #799 (Div. 4)A~H
JS time function Daquan detailed explanation ----- AHAO blog