[Programmers][Java] 다음에 올 숫자

HyeBin, Park·2023년 1월 24일
0

Programmers

목록 보기
25/26

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

📒 문제

📝 코드

public int solution(int[] common) {
		int lastIndex = common.length - 1;

		if (common[1] - common[0] == common[2] - common[1]) {
			return common[lastIndex] + common[2] - common[1];
		} else {
			return common[lastIndex] * (common[1] / common[0]);
		}
}
class Solution {
   public int solution(int[] common) {
		int lastIndex = common.length - 1;

		if (isArithmetic(common[0], common[1], common[2])) {
			return common[lastIndex] + common[2] - common[1];
		}

		if (isGeometric(common[0], common[1], common[2])) {
			return common[lastIndex] * (common[1] / common[0]);
		}
		return -1;
	}

	private boolean isGeometric(int firstNum, int secondNum, int thirdNum) {
		return secondNum / firstNum == thirdNum / secondNum;
	}

	public boolean isArithmetic(int firstNum, int secondNum, int thirdNum) {
		return secondNum - firstNum == thirdNum - secondNum;
	}
}

0개의 댓글