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

고급정렬 알고리즘 - 수 정렬하기 2 (백준 2751번) Java 코드 제출시 시간 초과 발생합니다. #5

Open
vel1024 opened this issue Oct 22, 2021 · 0 comments

Comments

@vel1024
Copy link

vel1024 commented Oct 22, 2021

출력시에 System.out.println(x) 로 하나씩 출력하는게 아니라
StringBuilder 를 사용해서 한번에 출력하면 시간초과를 피할 수 있습니다.

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());
        ArrayList<Integer> data = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            int x = Integer.parseInt(br.readLine());
            data.add(x);
        }

        Collections.sort(data);

        StringBuilder sb = new StringBuilder();

        for (int x: data) {
            sb.append(x).append("\n");
        }

        System.out.println(sb);

        br.close();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant