728x90
https://www.acmicpc.net/problem/2750
📝 유튜브 풀이
https://www.youtube.com/watch?v=MP9WJpngMvY
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int[] A = new int[N];
for(int i=0; i<N; i++) {
A[i] = scanner.nextInt();
}
//버블 정렬 구현
for(int i=0; i<N-1; i++) {
for(int j=0; j<N-1-i; j++) {
//현재 A배열 값보다 1칸 오른쪽 배열의 값이 더 작으면 두 수 바꾸기
if(A[j]>A[j+1]) {
int tmp = A[j];
A[j] = A[j+1];
A[j+1] = tmp;
}
}
}
for(int i=0; i<N; i++) {
System.out.println(A[i]);
}
}
}
📝 나의 풀이
- 틀린풀이
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
ArrayList<Integer> arr = new ArrayList<>();
for(int i=0; i<N; i++) {
arr.add(scanner.nextInt());
}
Collections.sort(arr);
HashSet<Integer> hashSet = new HashSet<>();
for(int i=0; i<arr.size(); i++) {
hashSet.add(arr.get(i));
}
System.out.println(hashSet);
}
}
- 수정한 풀이
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int[] arr = new int[N];
for(int i=0; i<N; i++) {
arr[i] = scanner.nextInt();
}
Arrays.sort(arr);
for(int i=0; i<N; i++) {
System.out.println(arr[i]);
}
}
}
정말 쉬운건데 수는 중복되지 않게 입력 된다는 문제를 중복을 없애라는 줄 알고 왜 틀렸지 ..? 하고있었다..ㅠㅠ
728x90
'코딩테스트 > [JAVA] 백준' 카테고리의 다른 글
[백준 | JAVA | 10828번] 스택 (0) | 2024.01.03 |
---|---|
[백준 | JAVA | 1427번] 소트인사이드 (0) | 2023.12.30 |
[백준 | JAVA | 2953번] 나는 요리사다 (0) | 2023.12.28 |
[백준 | JAVA | 11286번] 절댓값 힙 (1) | 2023.12.27 |
[백준 | JAVA | 2920번] 음계 (0) | 2023.12.19 |