[프로그래머스, 120863] 다항식 더하기 (java)
Problem Solving/Programmers

[프로그래머스, 120863] 다항식 더하기 (java)

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/120863

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

728x90

메모리: 79.6 MB, 시간: 18.75 ms

사용 알고리즘: 문자열

import java.util.*;

class Solution {
    public String solution(String polynomial) {
        
        // +를 기준으로 문자열 자르기
        StringTokenizer st = new StringTokenizer(polynomial, "+ ");
        
        // x의 개수
        int x = 0;
        // 상수의 개수
        int n = 0;
        
        String tmp;
        while(st.hasMoreTokens()) {
            tmp = st.nextToken();
            
            // x인 경우
            if(tmp.equals("x")) x += 1;
            else if(tmp.charAt(tmp.length() - 1) == 'x')
                x += Integer.parseInt(tmp.substring(0, tmp.length() - 1));
            else
                n += Integer.parseInt(tmp);
        }
        
        String answer = "";
        if(x != 0) { // x값을 출력해야 하는 경우
            if(x != 1) { // 계수 1은 생략
                answer += x;
            }
            answer += "x";
            
            if(n != 0) // 뒤에 상수 값도 출력해야 하는 경우
                answer += " + ";
        }
        if(n != 0) answer += n;
        
        return answer;
    }
}
728x90