|
1 | 1 | package hackerrank; |
2 | 2 |
|
3 | 3 | import java.util.Arrays; |
| 4 | +import java.util.Scanner; |
4 | 5 |
|
5 | 6 | public class BigSorting { |
| 7 | + |
6 | 8 | static String[] bigSorting(String[] unsorted) { |
| 9 | + //String ํ์
์ ์ ๋ ฌ์๋ ๋ฐฐ์ด์ ์ซ์์ค๋ฆ์ฐจ์์ผ๋ก ์ซ์๋ฅผ ์ ๋ ฌํ๋ ๋ฌธ์ . |
| 10 | + |
| 11 | + //์ฒซ์๋ -์คํจ-> Exception in thread "main" java.lang.NumberFormatException: For input string: "31415926535897932384626433832795" |
| 12 | + /* |
| 13 | + //String arr๋ฅผ long arr๋ก ๋ฐ๊พผ๋ค. |
7 | 14 | long[] arr = new long[unsorted.length]; |
8 | 15 |
|
9 | | - for(int i = 0; i<unsorted.length; i++){ |
| 16 | + for (int i = 0; i < unsorted.length; i++) { |
10 | 17 | arr[i] = Long.parseLong(unsorted[i]); |
11 | 18 | } |
| 19 | +
|
| 20 | + //long ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์์ ๋ ฌํ๋ค. |
12 | 21 | Arrays.sort(arr); |
13 | 22 |
|
14 | | - for(int i=0; i<arr.length; i++){ |
15 | | - unsorted[i] = Object.toString(arr[i]); |
| 23 | + //์ ๋ ฌํ long๋ฐฐ์ด์ String ๋ฐฐ์ด๋ก ๋ฐ๊พผ ๋ค ๋ฆฌํดํ๋ค. |
| 24 | + String[] result = new String[unsorted.length]; |
| 25 | + for (int i = 0; i < unsorted.length; i++) { |
| 26 | + result[i] = Long.toString(arr[i]); |
16 | 27 | } |
| 28 | + System.out.println(Arrays.toString(result)); |
| 29 | + return result; |
| 30 | + */ |
| 31 | + |
| 32 | + //sol4 ๋๋ค์ + sort |
| 33 | +// System.out.println(Arrays.toString(unsorted)); |
| 34 | + Arrays.sort(unsorted, (x, y) -> { |
| 35 | +// System.out.print("์์ arr: "+Arrays.toString(unsorted) + " x: "+x+" y:"+y); |
| 36 | + if (x.length() == y.length()) { |
| 37 | +// System.out.print(" x.compareTo(y): "+x.compareTo(y)+"\n"); |
| 38 | + return x.compareTo(y); //์๋ฆฌ์์ ์๊ด์์ด ์ฒซ์งธ์๋ฆฌ๋ง ๋ณด๊ณ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ |
| 39 | + }else { |
| 40 | +// System.out.print(" x.length() - y.length(): "+ (x.length() - y.length()) +"\n"); |
| 41 | + return x.length() - y.length(); //์๋ฆฌ์์ ์๊ดํ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ |
| 42 | + } |
| 43 | + }); |
| 44 | + |
17 | 45 | return unsorted; |
18 | 46 | } |
19 | 47 |
|
20 | 48 | public static void main(String[] args) { |
21 | | - System.out.println(bigSorting(new String[]{"1", "200", "150", "3"}) + ", ans: [1 3 150 200]"); |
| 49 | + Scanner sc = new Scanner(System.in); |
| 50 | + int n = sc.nextInt(); |
| 51 | + String[] arr = new String[n]; |
| 52 | + for (int i = 0; i < n; i++) { |
| 53 | + arr[i] = sc.next(); |
| 54 | + |
| 55 | + } |
| 56 | + arr = bigSorting(arr); |
| 57 | + for (int i = 0; i < arr.length; i++) { |
| 58 | + System.out.println(arr[i]); |
| 59 | + } |
| 60 | + sc.close(); |
| 61 | +/* sample input |
| 62 | +6 |
| 63 | +31415926535897932384626433832795 |
| 64 | +1 |
| 65 | +3 |
| 66 | +10 |
| 67 | +3 |
| 68 | +5 |
| 69 | +*/ |
22 | 70 | } |
23 | 71 | } |
0 commit comments