当前位置:网站首页>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).
边栏推荐
- Chapter 2 shell operation of hfds
- Codeforces round 797 (Div. 3) no f
- Market trend report, technological innovation and market forecast of desktop electric tools in China
- Li Kou leetcode 280 weekly match
- Base dice (dynamic programming + matrix fast power)
- Hbuilder x format shortcut key settings
- 新手必会的静态站点生成器——Gridsome
- 第一章 MapReduce概述
- Simple records of business system migration from Oracle to opengauss database
- 300th weekly match - leetcode
猜你喜欢
ByteDance new programmer's growth secret: those glittering treasures mentors
Tree of life (tree DP)
(lightoj - 1323) billiard balls (thinking)
Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
Chapter 5 detailed explanation of consumer groups
Chapter 1 overview of MapReduce
解决Intel12代酷睿CPU单线程调度问题(二)
Basic principles of video compression coding and audio compression coding
JS encapsulates the method of array inversion -- Feng Hao's blog
随机推荐
Base dice (dynamic programming + matrix fast power)
Chapter 6 rebalance details
Codeforces Round #801 (Div. 2)A~C
业务系统从Oracle迁移到openGauss数据库的简单记录
生成随机密码/验证码
Chapter 5 yarn resource scheduler
第2章 HFDS的Shell操作
useEffect,函數組件掛載和卸載時觸發
<li>圆点样式 list-style-type
(lightoj - 1354) IP checking (Analog)
Acwing: Game 58 of the week
Research Report on market supply and demand and strategy of double drum magnetic separator industry in China
300th weekly match - leetcode
875. Leetcode, a banana lover
Installation and configuration of MariaDB
Classic application of stack -- bracket matching problem
The concept of spark independent cluster worker and executor
js封装数组反转的方法--冯浩的博客
Educational Codeforces Round 122 (Rated for Div. 2)
Hbuilder x format shortcut key settings