์ด ๋ฌธ์ ๋ ์ฝ๊ฒ ์ ํ๋ ธ๋ค๊ณ ์๊ฐํ๊ณ , ๋ฌธ์ ์ ์์ ๊ฐ ๋ด๊ฐ ๋์ ํ ์ซ์๋ก๋ ๊ฒฐ๊ณผ๊ฐ ์ ๋์๋ค.
ํ์ง๋ง ํ ์คํธ 33.3 ์ ์ ๋ฐ์๋ค..ใ ใ ์ด ์ ๋๋ฉด ์์ ๊ฐ ์ด ์ข๊ฒ ๋ง์ ์์ค..
public String solution(int[] numbers) {
String answer = "";
Integer[]numbers2 = Arrays.stream(numbers).boxed().toArray(Integer[]::new);
Arrays.sort(numbers2, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if(o1.toString().charAt(0) == o2.toString().charAt(0)) {
if(o1.toString().length() != o2.toString().length()) {
if(o1.toString().length() > o2.toString().length()) {
String temp = null;
for(int i= 0; i<o1.toString().length()-1; i++) {
if(i==0) {
temp = o2.toString();
}
temp = temp + o2.toString();
}
return Integer.parseInt(temp) - o1;
}else {
String temp = null;
for(int i= 0; i<o2.toString().length()-1; i++) {
if(i==0) {
temp = o1.toString();
}
temp = temp + o1.toString();
}
return o2 - Integer.parseInt(temp);
}
}
return o2 - o1;
}else {
return o2.toString().charAt(0) - o1.toString().charAt(0);
}
}
});
for(int num : numbers2) {
answer = answer + num;
}
return answer;
}
๊ทธ๋์ ์ด๋ฒ์ Integer Array๊ฐ ์๋ String Array๋ก ๋ณํํ์ฌ Sortingํ์๋ค.
์๋ฅผ๋ค์ด 3, 34๋ฅผ ๋น๊ตํ ๋ "3"+"34" = "334"์ "34"+"3" = "343" ์ ๋น๊ตํ์ฌ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ๋๋๋ก ์ฒ๋ฆฌํ๋ค.
public String solution(int[] numbers) {
String answer = "";
String[] strArr = Arrays.stream(numbers).mapToObj(String :: valueOf).toArray(String[]::new);
Arrays.sort(strArr, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int a = Integer.parseInt(o2+o1);
int b = Integer.parseInt(o1+o2);
return a-b;
}
});
for(String str : strArr) {
answer = answer + str;
}
return answer;
}
ํ์ง๋ง ๋ ํ ์คํธ ์ฝ๋ ํ๋๊ฐ ํต๊ณผํ์ง ๋ชปํ๊ณ , ์ด์ ๋ฅผ ์ฐพ์๋ณด๋ 0๋ง ์๋ ๋ฐฐ์ด์ ๊ฒฝ์ฐ 0์ ๋ฆฌํดํด์ผํ๋๋ฐ 000 ์ด๋ฐ์์ผ๋ก ๋ฆฌํดํ๊ณ ์์๋ค.
int i = Integer.parseInt(answer);
if(i == 0) {
return "0";
}
์ด ์ฝ๋๋ฅผ ์ถ๊ฐํ๋๋ฐ ๋ฐํ์์๋ฌ๊ฐ ์ฐ์์ ๋จ์ด์ง๋๋ผ.. ํ ์คํธ ์ผ์ด์ค ์ค ์ซ์๊ฐ ํฐ ๊ฒฝ์ฐ int ๋ฒ์ ๋ฐ์ด๋ผ ๊ทธ๋ฐ๋ฏ..
์๋์ ๊ฐ์ด ์์ค๋ฅผ ์์ ํ์ฌ ํ ์คํธ์ ํต๊ณผํ์๋ค!
public String solution(int[] numbers) {
String answer = "";
String[] strArr = Arrays.stream(numbers).mapToObj(String :: valueOf).toArray(String[]::new);
Arrays.sort(strArr, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int a = Integer.parseInt(o2+o1);
int b = Integer.parseInt(o1+o2);
return a-b;
}
});
int zeroCnt = 0;
for(String str : strArr) {
int i = Integer.parseInt(str);
if(i ==0) {
zeroCnt++;
}
answer = answer + str;
}
if(zeroCnt == strArr.length) {
return "0";
}
return answer;
}
'๊ฐ๋ฐ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ (0) | 2022.07.24 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ (1) | 2022.07.24 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] K๋ฒ์งธ์ - ์ ๋ ฌ (0) | 2022.02.20 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ๋ฒ ์คํธ ์จ๋ฒ - ํด์ (0) | 2022.02.20 |
[ํ๋ก๊ทธ๋๋จธ์ค/JAVA] ์์ฅ - ํด์ (0) | 2022.02.18 |
๋๊ธ