[프로그래머스] 12924 숫자의 표현 / Java 정답

programmers

쉬운 문제라도 문제를 푸는 방법은 다양합니다.

더 명확하고 깔끔한 문제 해결 방법을 아신다면 자유롭게 말씀해주세요.

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

프로그래머스 저작권을 침해하지 않기 위해

문제 보시고 싶으시면 버튼을 눌러주세요.

답변 형식
프로그래머스 12924 자바
프로그래머스 12924 파이썬

숫자 N을 받아 연속된 숫자로 N을 표현 할 수 있는 경우의 수(ANSWER 갯수)를 반환합니다.

풀이 조건

투포인터 개념이 활용된다.

효율성에서 통과가 되지 않은 분들은 투포인터 문제일 확률이 높다.

 

for문으로 시작점마다 가능한지 불가능한지 찾는다면 문제를 해결 할 수 없다.

 

시작점, 끝점, 총 합을 선언해서

총 합보다 적다면 끝점을 하나 뒤로해서 총 합을 늘리고,

총 합과 같다면 answer+1을 한 뒤 시작점을 하나 뒤로 늘려 경우를 바꿔주고,

총 합보다 크다면 시작점 하나 뒤로 이동하여 총 합을 줄여주는 과정이 필요하다.

    class Solution {
        public int solution(int n) {
            int answer = 0;
           
            int start = 1;
            int end = 1;
            int sum = 1;
           
            while(true) {
                if(sum<n) {
                    end++;
                    sum += end;
                } else if(sum == n) {
                    answer++;
                    sum-=start;
                    start++;
                } else if(sum>n) {
                    sum-=start;
                    start++;                    
                }
                if(start > n) break;
            }  
            return answer;
        }
    }
class Solution {
        public int solution(int n) {
            int answer = 0;
           
            int start = 1;
            int end = 1;
            int sum = 1;
            // 시작점, 끝점, 총합을 선언

            while(true) {
                if(sum<n) {
                    end++;
                    sum += end;
                    // 총합보다 적다면 끝점을 하나 뒤로해서 총합을 늘림

                } else if(sum == n) {
                    answer++;
                    sum-=start;
                    start++;
                    // 총합과 같다면 answer+1을 한 뒤 시작점을 하나 뒤로 늘려 경우를 바꿈

                } else if(sum>n) {
                    sum-=start;
                    start++;
                    // 총 합보다 크다면 시작점 하나 뒤로 이동하여 총 합을 줄임            
                }

                if(start > n) break;
                // 시작점이 N보다 크다면 반복 종료
            }  
            return answer;
        }
    }

“[프로그래머스] 12924 숫자의 표현 / Java 정답”의 338개의 댓글

  1. [7884]Crazywin Official Login | Best Online Slots and GCash Bonuses,Join CrazyWin, the top-rated online casino in the Philippines. Register today to access official login, fast app downloads, and the best casino slots with massive bonuses. Play and win big now! visit: crazywin

  2. [4840]৫৭৭বেট বাংলাদেশ (577bet) – অনলাইন ক্যাসিনো ও বেটিং অফিশিয়াল সাইট,৫৭৭বেট বাংলাদেশ (577bet) অফিশিয়াল সাইটে সেরা অনলাইন ক্যাসিনো ও স্পোর্টস বেটিং-এর অভিজ্ঞতা নিন। নিরাপদ পেমেন্ট এবং আকর্ষণীয় বোনাস পেতে আজই আমাদের সাথে যুক্ত হোন। visit: 577bet

  3. [2563]mxgana Login Oficial | Descarga la APK y Slots en Vivo,mxgana es tu portal de entretenimiento con los mejores slots en vivo. Recarga fácil en casinos con depósitos en Oxxo y descubre cómo jugar ruleta online. ¡Únete ya! visit: mxgana

  4. [2149]বাংলাদেশের সেরা ক্যাসিনো (tap555bet)-অনলাইন ক্রিকেট বেটিং ও লগইন,বাংলাদেশের সেরা ক্যাসিনো tap555bet-এ উপভোগ করুন অনলাইন ক্রিকেট বেটিং ও লাইভ গেমসের রোমাঞ্চ। দ্রুত লগইন করুন এবং নিরাপদ প্ল্যাটফর্মে সেরা সব বোনাস নিয়ে আজই খেলা শুরু করুন! visit: tap555bet

  5. [1288]7000 bet লগইন এবং অ্যাপ ডাউনলোড | বিকাশ ক্যাসিনো ডিপোজিট,7000 bet লগইন করে অনলাইন স্লট গেম নিয়ম জানুন এবং লাইভ ক্যাসিনো বোনাস অফার উপভোগ করুন। এখনই অ্যাপ ডাউনলোড করে গেম শুরু করুন। visit: 7000 bet

  6. [5560]PhilBet: Official Login, Register & Casino Bonus | Download App & Slots,Join PhilBet, the Philippines’ premier online casino. Register today to claim your exclusive casino bonus and enjoy a wide range of slots and betting games. Download the app for a seamless mobile experience and secure login. visit: philbet

  7. Xin chao all my gaming buddies here in Vietnam. I have tested quite a few platforms lately and this one really stands out for the simple reason that it actually respects its players. Lightning fast deposits and withdrawals plus a massive library of slots that keep me hooked every single day. Definitely my go to from now on. vnnew88ok1

댓글 달기

이메일 주소는 공개되지 않습니다.