当前位置:网站首页>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).
边栏推荐
- (POJ - 1458) common subsequence (longest common subsequence)
- 音视频开发面试题
- Mp4 format details
- 解决Intel12代酷睿CPU单线程调度问题(二)
- Codeforces Round #771 (Div. 2)
- Ffmpeg command line use
- Study notes of Tutu - process
- input 只能输入数字,限定输入
- Solve the single thread scheduling problem of intel12 generation core CPU (II)
- 第5章 NameNode和SecondaryNameNode
猜你喜欢

(lightoj - 1323) billiard balls (thinking)

浏览器打印边距,默认/无边距,占满1页A4

Chapter 5 namenode and secondarynamenode

Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)

Solve the problem that intel12 generation core CPU single thread only runs on small cores

Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines

顺丰科技智慧物流校园技术挑战赛(无t4)

(lightoj - 1369) answering queries (thinking)

QT realizes window topping, topping state switching, and multi window topping priority relationship

MP4格式详解
随机推荐
It is forbidden to trigger onchange in antd upload beforeupload
Codeforces Round #771 (Div. 2)
Kubernetes cluster deployment
Advancedinstaller installation package custom action open file
SF smart logistics Campus Technology Challenge (no T4)
Market trend report, technological innovation and market forecast of desktop electric tools in China
Base dice (dynamic programming + matrix fast power)
Spark独立集群Worker和Executor的概念
Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
Tencent interview algorithm question
Market trend report, technical innovation and market forecast of China's desktop capacitance meter
软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
Educational Codeforces Round 130 (Rated for Div. 2)A~C
Chapter 7__ consumer_ offsets topic
300th weekly match - leetcode
Research Report on market supply and demand and strategy of double drum magnetic separator industry in China
Gridhome, a static site generator that novices must know
第五章 Yarn资源调度器
Chapter 2 shell operation of hfds
(lightoj - 1323) billiard balls (thinking)