두 정수 사이의 합

김덕근·2023년 5월 14일
0

algorithm

목록 보기
12/12


일반항

an = a + ( n − 1 ) d

등차수열의 합

Sn = n( a + l ) / 2


import java.lang.Math;

class Solution {
    public long solution(int a, int b) {
        return sumAtoB(Math.min(a, b), Math.max(a, b));
    }
    
    private long sumAtoB(long a, long b) {
        return (b - a + 1) * (a + b) / 2;
    }
    
}
profile
안녕하세요!

0개의 댓글