[JAVA 문제 풀이] 224. 두 정수 사이의 합

프로그래머스 (12912)
Stupefyee's avatar
Apr 25, 2025
[JAVA 문제 풀이] 224. 두 정수 사이의 합
notion image
 

내가 작성한 코드

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; } }
 

다른 사람의 코드

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; } }
  • 등차수열의 합 공식 이
Share article

stupefyee