본문 바로가기
알고리즘/문제풀이

[JAVA]프로그래머스 - 두 정수 사이의 합

by 겅아링 2020. 8. 24.
반응형

 

 

 

 

나의 풀이>

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

 

 

 

 

다른 풀이1>

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

 

 

 

 

다른 풀이2>

class Solution {
    public long solution(int a, int b) {
        long answer = 0;
	    for(int i=Math.min(a,b);i<=Math.max(a,b);i++){
	        answer+=i;
	    }
        return answer;
    }
}
반응형