์ฒด๊ฐ Level : โ
โ
โ Review: ์กฐ๊ฑด์ด ์ ๋ง ์ ๋ง ๊น๋ค๋กญ๋ค. ๋ฌธ์๋ ์ซ์๋ ์์ฌ์์ด์ ๋ณํํ ๋ ์ค๋ฅ๋๊ณ ์ฅ๋์๋์๋ค. ๋ฐ๋ก ์์ฃผ๋ก ์ฐพ์๋ณด๋ฉด์ ์ฌ๋ฌ์ฐจ๋ก ์์ ํ๋๊น ๊ฒฐ๊ตญ ์ฑ๊ณต !! ใ ใ |
๐ก ๋คํญ์์ด ์ฃผ์ด์ง๋ ๋๋ฅํญ๋ผ๋ฆฌ ๊ณ์ฐํด์ ์ถ๋ ฅํ๋ ์ฝ๋๋ฅผ ์ง์์ค.
[์ฑ๊ณต]
class Solution {
public String solution(String polynomial) {
String answer = "";
String[] str = polynomial.split("\\+");
String s1 = "";
for (String s : str){
s1 += s;
}
int x = 0, xsum = 0, n = 0, nsum = 0;
int xflag =0, nflag = 0;
int cnt = 0;
String[] str2 = s1.split(" ");
for (String s : str2){
if (s.contains("x")){
xflag = 1;
String tmp = "";
// x ์๋ฆฟ์ ํ์ธ
for (int i =0 ; i<s.length(); i++){
if(s.charAt(i) != 'x'){
cnt++;
}
}
// x ๋ ๊ฐ๋ค๋ฉด
if (s.equals("x")){
//answer += s;
xsum += 1;
}else { // x ๋ ๊ฐ์ง ์๋ค๋ฉด.. (์๋ฆฌ์)
if (cnt == 1){
xsum += (int)s.charAt(0)-48;
cnt = 0;
}else if (cnt == 2){
xsum += Integer.valueOf(s.substring(0,2));
cnt = 0;
}
//answer += s;
}
}else{
if (!s.isEmpty()){
nsum += Integer.valueOf(s);
nflag=1;
}
}
}
if ( nflag == 1 && xflag == 1){
if (xsum == 1){
answer += "x + " + nsum;
}else{
answer += String.valueOf(xsum) + "x";
answer += " + " + nsum;
}
}else if ( nflag == 1){
answer += nsum;
}else if ( xflag == 1){
if (xsum == 1){
answer += "x";
}else{
answer += String.valueOf(xsum) + "x";
}
}
return answer;
}
}
[์คํจ] 1x -> x ๊ฐ ๋์์ผํ๋ค.
class Solution {
public String solution(String polynomial) {
String answer = "";
String[] str = polynomial.split("\\+");
String s1 = "";
for (String s : str){
s1 += s;
}
int x = 0;
int xsum = 0;
int n = 0;
int nsum = 0;
int xflag =0, nflag = 0;
int cnt = 0;
String[] str2 = s1.split(" ");
for (String s : str2){
if (s.contains("x")){
xflag = 1;
String tmp = "";
// x ์๋ฆฟ์ ํ์ธ
for (int i =0 ; i<s.length(); i++){
if(s.charAt(i) != 'x'){
cnt++;
}
}
// x ๋ ๊ฐ๋ค๋ฉด
if (s.equals("x")){
//answer += s;
xsum += 1;
}else { // x ๋ ๊ฐ์ง ์๋ค๋ฉด.. (์๋ฆฌ์)
if (cnt == 1){
xsum += (int)s.charAt(0)-48;
cnt = 0;
}else if (cnt == 2){
xsum += Integer.valueOf(s.substring(0,2));
cnt = 0;
}
//answer += s;
}
}else{
if (!s.isEmpty()){
nsum += Integer.valueOf(s);
nflag=1;
}
}
}
if ( nflag == 1 && xflag == 1){
if (xsum == 1){
answer += "x + " + nsum;
}else{
answer += String.valueOf(xsum) + "x";
answer += " + " + nsum;
}
}else if ( nflag == 1){
answer += nsum;
}else if ( xflag == 1){
answer += String.valueOf(xsum) + "x";
}
return answer;
}
}
[์คํจ]
class Solution {
public String solution(String polynomial) {
String answer = "";
String[] str = polynomial.split("\\+");
String s1 = "";
for (String s : str){
s1 += s;
}
int x = 0;
int xsum = 0;
int n = 0;
int nsum = 0;
int xflag =0, nflag = 0;
String[] str2 = s1.split(" ");
for (String s : str2){
if (s.contains("x")){
xflag = 1;
if (s.equals("x")){
//answer += s;
xsum += 1;
}else {
xsum += (int)s.charAt(0)-48;
//answer += s;
}
}else{
if (!s.isEmpty()){
nsum += Integer.valueOf(s);
nflag=1;
}
}
}
if ( nflag == 1 && xflag == 1){
if (xsum == 1){
answer += "x + " + nsum;
}else{
answer += String.valueOf(xsum) + "x";
answer += " + " + nsum;
}
}else if ( nflag == 1){
answer += nsum;
}else if ( xflag == 1){
answer += String.valueOf(xsum) + "x";
}
return answer;
}
}
[์คํจ]
class Solution {
public String solution(String polynomial) {
String answer = "";
String[] str = polynomial.split("\\+");
String s1 = "";
for (String s : str){
s1 += s;
}
int x = 0;
int xsum = 0;
int n = 0;
int nsum = 0;
int xflag =0, nflag = 0;
String[] str2 = s1.split(" ");
for (String s : str2){
if (s.contains("x")){
xflag = 1;
if (s.equals("x")){
//answer += s;
xsum += 1;
}else {
xsum += (int)s.charAt(0)-48;
//answer += s;
}
}else{
if (!s.isEmpty()){
nsum += Integer.valueOf(s);
nflag=1;
}
}
}
if ( nflag == 1 && xflag == 1){
answer += String.valueOf(xsum) + "x";
answer += " + " + nsum;
}else if ( nflag == 1){
answer += nsum;
}else if ( xflag == 1){
answer += String.valueOf(xsum) + "x";
}
return answer;
}
}