-
프로그래머스 - 두 정수 사이의 합 [자바]Algorithms/- 프로그래머스 2022. 2. 3. 23:24
- 문제 링크 : 두 정수 사이의 합
정답
class Solution { public long solution(int a, int b) { if(a == b){ return a; }else{ int t = 0; if(b < a){ t = a; a = b; b = t; } int len = b - a; long res = 0; for(int i=0; i<=len; i++){ res += a; a++; } return res; } } }
분석
- 단순 합
참고할 만한 정답
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; } }
비교 분석
- for문에서 동일할 경우도 한번에 처리 가능
반응형