Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4주차] 최서현 #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 4주차/가장큰수/가장큰수_최서현.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** 가장 큰 수
* 0 또는 양의정수 주어졌을때 이어붙여서 만들수있는 가장 큰 수 구하기
* input : numbers[] 배열
* output : String
* 풀이 : 숫자가 큰것부터 선택해야됨
* CompareTo를 구현하여 두 숫자를 이어 붙였을때 큰 수가 되는걸 선택하면 된다.
*
* 구현 안하고 사전순으로 만든뒤 삽입정렬하며 예외처리 하려했다가 머리터짐..ㅜ;;
*/
import java.util.*;
class Solution {
public String solution(int[] numbers) {
//1. int를 String으로 전환
String[] arr = new String[numbers.length];
for(int i=0; i<numbers.length; i++){
String str = String.valueOf(numbers[i]);
arr[i] = str;
}
//2. 이어붙였을때 가장 큰 숫자가 앞에오도록 정렬
Arrays.sort(arr, new Comparator<String>(){
@Override
public int compare(String a, String b){
return (b+a).compareTo(a+b);
}
});


//3. 예외처리
if(arr[0].equals("0")) return "0";

String answer = "";
for(String str : arr) answer+=str;
return answer;
}
}