[JAVA] 프로그래머스 Lv1. 두 정수 사이의 합 - 다른풀이

2020. 10. 19. 17:16
728x90

class Solution {

    public long solution(int a, int b) {

        return sumAtoB(Math.min(a, b), Math.max(b, a));   // 두 수를 비교해 더 큰수를 b값으로 

    }

 

    private long sumAtoB(long a, long b) {

        return (b - a + 1) * (a + b) / 2;      // 등차수열의 합.

    }

}

728x90

BELATED ARTICLES

more