์ฃผ์ํด์ผ ํ ๊ฒ์.. 1x๋ x๋ก ๋์์ผ ํ๋ค๋๊ฒ..
์ถ๋ ฅ๋ฌธ์ด ๋๋ฝ์ง๋ง.....
class Solution {
public String solution(String polynomial) {
String answer = "";
String[] arr=polynomial.split(" \\+ ");
int xSum=0;
int Sum=0;
for(String x:arr){
if(x.contains("x")){
if(x.length()==1){
xSum+=1;
}else {
String temp = x.replace("x","");
xSum+=Integer.parseInt(temp);
}
}else{
Sum+=Integer.parseInt(x);
}
}
if(xSum==0&&Sum!=0) answer=String.valueOf(Sum);
else if(xSum!=0&&Sum==0) {
if(xSum==1) answer="x";
else answer = String.valueOf(xSum) + 'x';
}
else {
if(xSum==1) answer="x"+" + "+ Sum;
else answer = String.valueOf(xSum) + 'x' + " + " + Sum;
}
return answer;
}
}
'๐ ์ฝ๋ฉํ ์คํธ > ๋ฐฑ์ค & ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : ๋ถ์์ ๋ง์ (0) | 2022.12.23 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : OXํด์ฆ (0) | 2022.12.22 |
[๋ฐฑ์ค][JAVA]1181๋ฒ : ๋จ์ด ์ ๋ ฌ (0) | 2022.12.20 |
[๋ฐฑ์ค][JAVA]2101๋ฒ : ํต๊ณํ (0) | 2022.12.15 |
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : ํน์ดํ ์ ๋ ฌ (0) | 2022.12.15 |