[프로그래머스] 다음에 올 숫자

김준영·2023년 3월 11일
1

코딩테스트

목록 보기
5/22

문제


등차수열 혹은 등비수열 common이 매개변수로 주어질 때, 마지막 원소 다음으로 올 숫자를 return 하도록 solution 함수를 완성해보세요.

https://school.programmers.co.kr/learn/courses/30/lessons/120924

내 풀이


class Solution {
    public int solution(int[] common) {
        int answer = 0;
        int next1 = common[1] - common[0];
        int next2 = common[2] - common[1];
        if(next1 == next2){
            return answer = common[common.length-1] + next1;
        }else
            return answer = common[common.length-1] * (next2 / next1);
    
    }
}
  1. 첫 번째, 두 번째 수의 차이와, 세 번째, 두 번째 수의 차이를 비교.
  2. 등빈지, 등찬지 확인 후 마지막 값에 적용.
profile
ㅎㅎ

0개의 댓글