当前位置:网站首页>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 #800 (Div. 2)AC
- Input can only input numbers, limited input
- Tencent interview algorithm question
- Market trend report, technical innovation and market forecast of tabletop dishwashers in China
- CMake Error: Could not create named generator Visual Studio 16 2019解决方法
- 生成随机密码/验证码
- CMake速成
- Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
- 第7章 __consumer_offsets topic
- Acwing: the 56th weekly match
猜你喜欢

sublime text 代码格式化操作

Advancedinstaller installation package custom action open file

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

Cmake Express

Ffmpeg command line use

Codeforces round 797 (Div. 3) no f

图像处理一百题(1-10)

It is forbidden to trigger onchange in antd upload beforeupload

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

Chapter 2 shell operation of hfds
随机推荐
(lightoj - 1323) billiard balls (thinking)
Market trend report, technological innovation and market forecast of double door and multi door refrigerators in China
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
腾讯面试算法题
使用jq实现全选 反选 和全不选-冯浩的博客
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
MariaDB的安装与配置
Discussion on QWidget code setting style sheet
生成随机密码/验证码
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
Installation and configuration of MariaDB
Simply try the new amp model of deepfacelab (deepfake)
Acwing - game 55 of the week
Research Report of desktop clinical chemical analyzer industry - market status analysis and development prospect prediction
Codeforces Round #797 (Div. 3)无F
Kubernetes cluster deployment
875. Leetcode, a banana lover
Audio and video development interview questions
业务系统从Oracle迁移到openGauss数据库的简单记录
sublime text 代码格式化操作