[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] LV.2 ์ˆซ์ž์˜ ํ‘œํ˜„ (์ž๋ฐ” java)

 

1. ๋ฌธ์ œ : https://school.programmers.co.kr/learn/courses/30/lessons/12924?language=java

 

2. ํ’€์ด

1๋ถ€ํ„ฐ 14๊นŒ์ง€ ์‹œ์ž‘ํ•˜๋Š” ์ผ€์ด์Šค๋ฅผ ๋‘ฌ์„œ 15๊ฐ€ ๋˜๋ฉด answer +1

ํšจ์œจ์„ฑ ์œ„ํ•ด 15๋ณด๋‹ค ํฌ๋‹ค๋ฉด break

 

3. ์ฝ”๋“œ

class Solution {
    public int solution(int n) {
        int answer = 1;
        for(int i=1;i<n;i++){
            int temp = 0;
            for(int j=i;j<n+1;j++){
                temp += j;
                if(temp == n){
                    answer += 1;
                    break;
                }
                else if(temp>n) break;
            }
        }
        return answer;
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ