Notice
250x250
Recent Posts
Recent Comments
Link
넘치게 채우기
[LeetCode] 3151. Special Array I 본문
728x90
반응형
https://leetcode.com/problems/special-array-i/description/
leetcode - Special Array I
문제 유형: 구현
문제 난이도: Easy
문제
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
You are given an array of integers nums. Return true if nums is a special array, otherwise, return false.
배열의 모든 인접한 쌍들의 홀짝이 다르면 특별한 배열이다. 특별한지 아닌지 확인하시오.
풀이
i번과 i+1번의 홀짝여부가 한 번이라도 같다면 false, 아니면 true를 반환하면 된다.
코드
C++
class Solution {
public:
bool isArraySpecial(vector<int>& nums) {
int n = nums.size();
for(int i = 0; i < n-1; ++i) {
if(nums[i]%2 == nums[i+1]%2) return false;
}
return true;
}
};
728x90
반응형
'PS > LeetCode' 카테고리의 다른 글
[LeetCode] 3105. Longest Strictly Increasing or Strictly Decreasing Subarray (0) | 2025.02.03 |
---|---|
[LeetCode] 1752. Check if Array Is Sorted and Rotated (0) | 2025.02.02 |
[LeetCode] 827. Making A Large Island (0) | 2025.01.31 |
[LeetCode] 2493. Divide Nodes Into the Maximum Number of Groups (0) | 2025.01.31 |
[LeetCode] 684. Redundant Connection (0) | 2025.01.29 |