흥미/코딩테스트

😊 #2 프로그래머스 - 다음에 올 숫자

RadderNepa 2023. 1. 30. 00:05

[문제]

출처 : https://school.programmers.co.kr


[풀이]
- 이건 내가 직접 풀었다. 내 풀이는 아래와 같다.

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

- 쉬운 문제라 다른 사람의 풀이도 다 비슷비슷했다.
- 아래의 풀이는 변수 선언을 적게하는 풀이다.(난 3개 선언, 아래는 2개 선언)

class Solution {
    public int solution(int[] common) {
        int answer = 0;

        int x = common[1] - common[0];
        int y = common[2] - common[1];

        if (x == y) {
            answer = common[common.length - 1] + y;
        } else {
            answer = common[common.length - 1] * common[2] / common[1];
        }
        return answer;
    }
}

- 모든 코테가 이랬으면 좋겠다.