두 정수 사이의 합

less than 1 minute read

1. 문제 설명

두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요. 예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다.

2. 해결 Point

3. 이론

4. Source Code

class Solution {
    public long solution(int a, int b) {
        long answer = 0;
        
        if(a>b){
            int temp = b;
            b=a;
            a=temp;
        }
        
        for(int i=a; i<=b; i++){
            answer+=i;
        }
        
        return answer;
    }
}

Tags:

Categories:

Updated: