当前位置:网站首页>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).
边栏推荐
- FLV格式详解
- (lightoj - 1323) billiard balls (thinking)
- Tencent interview algorithm question
- 浏览器打印边距,默认/无边距,占满1页A4
- (lightoj - 1349) Aladdin and the optimal invitation (greed)
- Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
- Sublime text code formatting operation
- (POJ - 1458) common subsequence (longest common subsequence)
- 原生js实现全选和反选的功能 --冯浩的博客
- Install Jupiter notebook under Anaconda
猜你喜欢
业务系统从Oracle迁移到openGauss数据库的简单记录
Chapter 6 rebalance details
第7章 __consumer_offsets topic
QT implementation window gradually disappears qpropertyanimation+ progress bar
QT simulates mouse events and realizes clicking, double clicking, moving and dragging
sublime text 代码格式化操作
原生js实现全选和反选的功能 --冯浩的博客
Click QT button to switch qlineedit focus (including code)
(lightoj - 1323) billiard balls (thinking)
js封装数组反转的方法--冯浩的博客
随机推荐
顺丰科技智慧物流校园技术挑战赛(无t4)
Summary of game theory
Chapter 7__ consumer_ offsets topic
Effet d'utilisation, déclenché lorsque les composants de la fonction sont montés et déchargés
Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
Bisphenol based CE Resin Industry Research Report - market status analysis and development prospect forecast
Solve the single thread scheduling problem of intel12 generation core CPU (II)
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
业务系统从Oracle迁移到openGauss数据库的简单记录
Codeforces Round #798 (Div. 2)A~D
sublime text 代码格式化操作
Li Kou: the 81st biweekly match
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
Chapter 5 yarn resource scheduler
Oneforall installation and use
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Codeforces Global Round 19
Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
China double brightening film (dbef) market trend report, technical dynamic innovation and market forecast
Bidirectional linked list - all operations