https://school.programmers.co.kr/learn/courses/30/lessons/134240
Arrays.sort(answer2, (x,y) -> y-x);
람다식으로 내림차순을 하려면
Integer[] answer2 = new Integer[answer.length()];
와 같이 wrapper클래스로 int 말고 Integer로 선언해야한다
class Solution {
public String solution(int[] food) {
String answer = "";
for(int i =1; i<food.length; i++){
int num=food[i];
if(food[i]%2!=0){
num = food[i]-1;
for(int j =0; j<num/2; j++){
answer+=i ;
}
continue;
}
for(int j =0; j<num/2; j++){
answer+=i ;
}
}
Integer[] answer2 = new Integer[answer.length()];
answer +=0;
for(int i=0; i<answer.length()-1;i++){
answer2[i]= answer.charAt(i)-'0';
}
Arrays.sort(answer2, (x,y) -> y-x);
for(int i : answer2){
answer+=i;
}
return answer;
}
}
class Solution {
public String solution(int[] food) {
StringBuilder sb = new StringBuilder();
for(int i=1; i<food.length; i++) {
for(int q=0; q<food[i]/2; q++) {
sb.append(i);
}
}
String answer = sb.toString()+"0"+sb.reverse().toString();
return answer;
}
}
StringBuilder를 사용하는게 더 간단해 보인다
answer =sb.toString()+"0"+sb.reverse().toString() 코드가 유용해보이니 기억