알고리즘
(JAVA) 두 정수 사이의 합
Daniel환
2018. 10. 24. 20:23
class Solution {
public long solution(int a, int b) {
long answer = 0;
if(a==b){
answer=a;
}else if(a<b){
for(int i=a; i<=b; i++){
answer+=i;
}
}else{
for(int i=b; i<=a; i++){
answer+=i;
}
}
return answer;
}
}
------------------------
class Solution { public long solution(int a, int b) { long answer = 0; for (int i = ((a < b) ? a : b); i <= ((a < b) ? b : a); i++) answer += i; return answer; } }
------------------------------
class Solution { public long solution(int a, int b) { return sumAtoB(Math.min(a, b), Math.max(b, a)); } private long sumAtoB(long a, long b) { return (b - a + 1) * (a + b) / 2; } }출처: www.programmers.co.kr