본문 바로가기
IT/알고리즘

24] Leetcode 1502. Can Make Arithmetic Progression From Sequence

by 깻잎쌈 2020. 7. 5.
반응형
 

Can Make Arithmetic Progression From Sequence - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

먼저 정렬해주고,

간격 일정한지 판단.

class Solution {
public:
    bool canMakeArithmeticProgression(vector<int>& arr) {
        sort(arr.begin(),arr.end());
        bool flag = true;
        int diff = arr[1]-arr[0];
        for(int i = 2;i<arr.size();i++){
            if(diff != arr[i]-arr[i-1]){
                flag = false;
                break;
            }
               
        }
        
        return flag;
    }
};
반응형

댓글