프로그래머스 12924번 숫자의 표현 Java

: ) YOUNG·2024년 2월 23일
1

알고리즘

목록 보기
323/370
post-thumbnail

프로그래머스 12924번
https://school.programmers.co.kr/learn/courses/30/lessons/12924

문제



생각하기


  • 투 포인터로 간단하게 해결할 수 있다.


동작


        int n = 3;
        int ans = 0;
        for (int i = 1; i <= n; i += 2) {
            if (n % i == 0) {
                System.out.println(" i : " + i);
                ans++;
            }
        }

더 간단한 풀이

정수론으로 풀이가능하다.



결과


코드



import java.util.*;

class Solution {
    public int solution(int n) {
        int answer = 0;
        
        return twoPointer(n);
    } // End of solution()
    
    public int twoPointer(int n) {
        int count = 0;
        int low = 1;
        int high = 1;

        while(low <= n) {            
            int sum = 0;
            for(int i=low; i<high; i++) {
                sum += i;
            }
            
            if(sum == n) {
                count++;
                low++;
            } else if(sum > n) {
                low++;
            } else if(sum < n) {
                high++;
            }   
        }

        return count;
    } // End of twoPointer()
} // End of Solution class

0개의 댓글