넘치게 채우기

[LeetCode] 3151. Special Array I 본문

PS/LeetCode

[LeetCode] 3151. Special Array I

riveroverflow 2025. 2. 1. 15:58
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
반응형