본문 바로가기
Programming/>> Algorithm

[Lucky Algorithm] Big Sorting

by 니키ᕕ( ᐛ )ᕗ 2017. 10. 4.

Consider an array of numeric strings, , where each string is a positive number with anywhere from  to  digits. Sort the array's elements in non-decreasing (i.e., ascending) order of their real-world integer values and print each element of the sorted array on a new line.

Input Format

The first line contains an integer, , denoting the number of strings in 
Each of the  subsequent lines contains a string of integers describing an element of the array.

Constraints

  • Each string is guaranteed to represent a positive integer without leading zeros.
  • The total number of digits across all strings in  is between  and  (inclusive).

Output Format

Print each element of the sorted array on a new line.


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] unsorted = new String[n];
        for(int unsorted_i=0; unsorted_i < n; unsorted_i++){
            unsorted[unsorted_i] = in.next();
        }
        // your code goes here
        Comparator<String> bigSortComparator = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(o1.length() == o2.length()) {
                    return o1.compareTo(o2);
                }
                return o1.length() - o2.length();
            }

        };
        Arrays.sort(unsorted, bigSortComparator);

        for (int i = 0; i < n; i++)
            System.out.println(unsorted[i]);
    }
}


'Programming > >> Algorithm' 카테고리의 다른 글

[Lucky Algorithm] Super Reduced String  (0) 2017.10.04
[Lucky Algorithm] Time Conversion  (0) 2017.10.04
[Lucky Algorithm] Birthday Cake Candles  (0) 2017.10.03
[Lucky Algorithm] Staircase  (0) 2017.10.03
[Lucky Algorithm] Plus Minus  (0) 2017.10.03

댓글