등차수열 혹은 등비수열 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);
}
}
- 첫 번째, 두 번째 수의 차이와, 세 번째, 두 번째 수의 차이를 비교.
- 등빈지, 등찬지 확인 후 마지막 값에 적용.