ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • algorithm
    ๐Ÿ’ป ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ๋‹คํ•ญ์‹ ๋”ํ•˜๊ธฐ
    2023. 9. 15. 11:37
    728x90

    ๐Ÿ“• ๋ฌธ์ œ ์„ค๋ช…

    ํ•œ ๊ฐœ ์ด์ƒ์˜ ํ•ญ์˜ ํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์‹์„ ๋‹คํ•ญ์‹์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹คํ•ญ์‹์„ ๊ณ„์‚ฐํ•  ๋•Œ๋Š” ๋™๋ฅ˜ํ•ญ๋ผ๋ฆฌ ๊ณ„์‚ฐํ•ด ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ง์…ˆ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹คํ•ญ์‹ polynomial์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋™๋ฅ˜ํ•ญ๋ผ๋ฆฌ ๋”ํ•œ ๊ฒฐ๊ด๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”. ๊ฐ™์€ ์‹์ด๋ผ๋ฉด ๊ฐ€์žฅ ์งง์€ ์ˆ˜์‹์„ return ํ•ฉ๋‹ˆ๋‹ค.

     

    ๐Ÿ“• ์ œํ•œ ์‚ฌํ•ญ

    • 0 < polynomial์— ์žˆ๋Š” ์ˆ˜ < 100
    • polynomial์— ๋ณ€์ˆ˜๋Š” 'x'๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
    • polynomial์€ ์–‘์˜ ์ •์ˆ˜, ๊ณต๋ฐฑ, ‘x’, ‘+'๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ•ญ๊ณผ ์—ฐ์‚ฐ๊ธฐํ˜ธ ์‚ฌ์ด์—๋Š” ํ•ญ์ƒ ๊ณต๋ฐฑ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
    • ๊ณต๋ฐฑ์€ ์—ฐ์†๋˜์ง€ ์•Š์œผ๋ฉฐ ์‹œ์ž‘์ด๋‚˜ ๋์—๋Š” ๊ณต๋ฐฑ์ด ์—†์Šต๋‹ˆ๋‹ค.
    • ํ•˜๋‚˜์˜ ํ•ญ์—์„œ ๋ณ€์ˆ˜๊ฐ€ ์ˆซ์ž ์•ž์— ์˜ค๋Š” ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
    • " + 3xx + + x7 + "์™€ ๊ฐ™์€ ์ž˜๋ชป๋œ ์ž…๋ ฅ์€ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.
    • ๋ฌธ์ž์™€ ์ˆซ์ž ์‚ฌ์ด์˜ ๊ณฑํ•˜๊ธฐ๋Š” ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.
    • polynomial์—๋Š” ์ผ์ฐจ ํ•ญ๊ณผ ์ƒ์ˆ˜ํ•ญ๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
    • ๊ณ„์ˆ˜ 1์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.
    • ๊ฒฐ๊ด๊ฐ’์— ์ƒ์ˆ˜ํ•ญ์€ ๋งˆ์ง€๋ง‰์— ๋‘ก๋‹ˆ๋‹ค.
    • 0 < polynomial์˜ ๊ธธ์ด < 50

     

    ๐Ÿ“ ๋‹ต์•ˆ

    class Solution {
        public String solution(String polynomial) {
            String answer = "";
            int xCount = 0;
            int nCount = 0;
            String [] arr = polynomial.replace(" +","").split(" ");
    
            for(String var : arr) {
                if(var.contains("x")) {
                    var = var.replace("x","");
                    xCount += var!="" ? Integer.parseInt(var.replace("x","")) : 1;
                }
                else {
                    nCount += Integer.parseInt(var);                
                }
            }
    
            if(xCount==1) {
                answer = "x";
            }else if(xCount > 1){
                answer = xCount+"x";
            }else {
                return ""+nCount;
            }
    
            return (nCount>0) ? answer + " + " + nCount : answer;
        }
    }

    x๊ฐ€ ๋ถ™์€ ์š”์†Œ์˜ ์ˆซ์ž๋ผ๋ฆฌ ๋”ํ•ด์ฃผ๊ณ  ์ƒ์ˆ˜ํ•ญ๋ผ๋ฆฌ ๋”ํ•œ ํ›„ ๋ฆฌํ„ดํ•˜๋ฉด ๋˜๋Š” ์‰ฌ์šด ๋ฌธ์ œ์ง€๋งŒ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‹ค์–‘ํ•ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ์˜ค๋ž˜ํ–ˆ๋‹ค.

     

    ์šฐ์„  polynomial์˜ ' +'๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๋ณ€ํ™˜ ํ›„ split()์„ ์ด์šฉํ•ด์„œ String ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋Œ๋ฉด์„œ 'x'๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ํฌํ•จ๋๋‹ค๋ฉด x๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ๋‚จ์€ ๋ฌธ์ž์—ด(์ˆซ์ž)์ด ์žˆ๋‹ค๋ฉด ๋”ํ•ด์ฃผ๊ณ  ๋‚จ์€ ๋ฌธ์ž์—ด์ด ์—†๋‹ค๋ฉด 1์„ ๋”ํ•œ๋‹ค.

    'x'๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์ฆ‰ ์ƒ์ˆ˜์ธ ๊ฒฝ์šฐ nCount์— ์ƒ์ˆ˜ํ•ญ์„ ๋”ํ•ด์ค€๋‹ค.

     

    ๊ทธ๋ฆฌ๊ณ  xํ•ญ์ด 1์ผ ๊ฒฝ์šฐ ์ˆซ์ž๋ฅผ ์ƒ๋žตํ•ด์•ผ ๋˜๊ธฐ ๋•Œ๋ฌธ์— answer์— x๋งŒ ๋„ฃ์–ด์ฃผ๊ณ  1 ์ด์ƒ์ผ ๊ฒฝ์šฐ์—๋Š” answer์— xCount์™€ x๋ฅผ ๋ถ™์—ฌ์„œ ๋Œ€์ž…ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  xํ•ญ์ด 0์ผ ๊ฒฝ์šฐ์—” ์ƒ์ˆ˜ ๊ณ„์‚ฐ๊ฐ’๋งŒ์„ ๋ฆฌํ„ดํ•œ๋‹ค.

     

    ๋งˆ์ง€๋ง‰ return์—์„  ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ์ƒ์ˆ˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๊ฐ€ 0 ์ด์ƒ์ผ ๊ฒฝ์šฐ xํ•ญ ๊ฒฐ๊ณผ๊ฐ’์ธ answer์™€ ๋ฌธ์ž์—ด +, ์ƒ์ˆ˜ ๊ฒฐ๊ณผ๊ฐ’์„ ์ฐจ๋ก€๋กœ ๋ถ™์—ฌ์„œ ๋ฆฌํ„ดํ•˜๊ณ , 0 ์ด์ƒ์ด ์•„๋‹๊ฒฝ์šฐ์—” xํ•ญ ๊ฒฐ๊ณผ๊ฐ’์ธ answer๋งŒ ๋ฆฌํ„ดํ•œ๋‹ค.

     

    ๐Ÿ’ก ์ฐธ๊ณ 

    • A๋ฌธ์ž์—ด.replace("๋ฌธ์ž์—ดB","๋ฌธ์ž์—ดC") : A๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž์—ดB๋ฅผ ์ฐพ์•„ ๋ฌธ์ž์—ดC๋กœ ์น˜ํ™˜ํ•ด์„œ ๋ฆฌํ„ด
    • ๋ฌธ์ž์—ด.split("๊ตฌ๋ถ„์ž") : ๊ตฌ๋ถ„์ž๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด์„ ๋Š์–ด์„œ ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ด, ๊ตฌ๋ถ„์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ํ•œ๊ธ€์ž์”ฉ ๋Š์–ด์„œ ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ด
    • Integer.parseInt("๋ฌธ์ž์—ด") : ๋ฌธ์ž์—ด์„ ์ˆซ์žํ˜•์œผ๋กœ ํ˜•๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜
    728x90
Designed by Tistory.