반응형
1. 문제 번호 5597번
2. 문제 풀이
- HashSet
- 1~30 Index자리에 1,true처리
- 내가 선호하는 방법이며 처음 시도한 방법(*** 결국 해냄*** 처음과 끝을 한정 지을 것***)
나의 문제풀이 방식 및 순서
- 이중 for문을 사용해서 비교하려고 생각했었는데 불가능에 가깝다는걸 2시간정도 만에 깨달음..ㅠㅠㅜ
약간 생각하는게 귀찮아서 손으로 먼저 움직이려고 하는 경향이 많다.
3. 소스 인증
import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
List<Integer> submitMember = new ArrayList<>();
List<Integer> notSubmitMember = new ArrayList<>();
String inputLine = null;
while ( (inputLine=br.readLine()) != null && !inputLine.isEmpty()){
submitMember.add(Integer.parseInt(inputLine));
}
Set<Integer> submitSet = new HashSet<>(submitMember);
for (int j = 1; j <= 30; j++){
if(!submitSet.contains(j)){
System.out.println(j);
}
}
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean [] isSubmitMember = new boolean[31];
for (int i = 1; i <= 28; i ++){
int inputLine = Integer.parseInt(br.readLine());
isSubmitMember[inputLine] = true;
}
for (int j = 1; j <= 30; j++){
if(!isSubmitMember[j]){
System.out.println(j);
}
}
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
List<Integer> submitMember = new ArrayList<>();
List<Integer> notSubmitMember = new ArrayList<>();
String inputLine = null;
while ( (inputLine=br.readLine()) != null && !inputLine.isEmpty()){
submitMember.add(Integer.parseInt(inputLine));
}
//시작과 끝을 한정해주기!!! 매우 중요!!!
submitMember.add(31);
//Sorting
Collections.sort(submitMember);
/*
i = 1,4,7
j = 1 일때
while ( 1 < 1 ) 는 false
while ( 2 < 4 ) 는 true로 notSubmitMember 에 2가 저장. j ++ 이니깐 j = 3으로 증가되어 while 안에서
while ( 3 < 4 ) 는 true로 notSubmitMember 에 3가 저장.
while ( 4 < 4 ) 는 false 라서 for문 으로 나와서 i는 7로 다음 index저장
while ( 5 < 7 ) 는 false
*/
int j = 1;
for (int i : submitMember){
while(j < i){
notSubmitMember.add(j++);
}
j++;
}
for (int memberId : notSubmitMember) {
System.out.println(memberId);
}
}
}
- 실패 소스코드 -
/********************************************************************
...실패한 소스코드...
*********************************************************************/
import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
List<Integer> submitMember = new ArrayList<>();
List<Integer> notSubmitMember = new ArrayList<>();
String inputLine = null;
while ( (inputLine=br.readLine()) != null && !inputLine.isEmpty()){
submitMember.add(Integer.parseInt(inputLine));
}
Collections.sort(submitMember);
for (int j = 1; j <= 30; j++){
for(int i = 0; i < j; i++){
if(j == submitMember.get(i)){
break;
}
}
notSubmitMember.add(i)
}
for (int i = 0; i <= ; i++){
for (int j = 1; j <= 30; j++){
if(submitMember.get(i) == j){
continue;
}
notSubmitMember.add(i)
}
}
for (int memberId : notSubmitMember) {
System.out.println(memberId);
}
}
}
4. 추가 개념
HashSet
HashSet의 성질
1. HashSet 중복 허용 X
값 존재 유무 파악할 때 사용 가능
2. List 등과는 다르게 순서 보장 X
저장 순서를 유지하고자 한다면 LinkedHashSet 사용
3. Null값을 허용
HashSet의 구현
// 타입을 지정 가능
HashSet<String> alphabet1 = new HashSet<String>();
// 타입을 생략하여 사용 가능 -> 빈 HashSet생성 시 사용
HashSet<String> alphabet2 = new HashSet<>();
// 초기 용량(Capacity) 설정
HashSet<String> alphabet3 = new HashSet<>(10);
// animal의 모든 값을 가진 HashSet 생성
HashSet<String> alphabet4 = new HashSet<>(alphabet1);
//초기값 지정 가능
HashSet<String> alphabet5 = new HashSet<>(Arrays.asList("A", "B", "C"));
HashSet의 값 추가 및 삭제, 사이즈 확인
//순서가 없으므로 특정 위치에 값 추가 불가
HashSet<String> alphabet = new HashSet<>()
alphabet.add("A");
alphabet.add("B");
alphabet.add("C");
//값 B 제거
set.remove(2);
//모든 값을 제거
set.clear();
/********사이즈 확인********/
System.out.println(alphabet.size());
- HashSet 내부에 존재하지 않는다면 그 값을 HashSet에 추가/삭제 하고 true 를 반환한다.
- HashSet 내부에 존재한다면 false를 반환한다.
HashSet의 요소 값 검색 및 출력
HashSet<String> alphabet = new HashSet<String>(Arrays.asList("A","B","C"));
////set내부에 값 1이 있다면 true 출력, 없다면 false 출력
System.out.println(alphabet.contains("B"));
//출력결과 : [A,B,C]
System.out.println(alphabet);
Iterator iter = set.iterator();
//hasNext() : 가져올 객체가 있다면 true 리턴, 없다면 false 리턴
// next() : Iterator에서 하나의 객체를 가져올 수 있는 메소드
while(iter.hasNext()) {
System.out.println(iter.next());
}
5. 참조 블로그
불편함을 느끼실 경우 연락 주시면 곧 바로 삭제하도록 하겠습니다.
https://velog.io/@acacia__u/hashSet
[Java] HashSet의 개념과 사용법 정리
Set 이란? Set 인터페이스 구현 클래스 객체를 중복해서 저장할 수 없으며, 하나의 null 값만 저장할 수 있다. 중복을 자동으로 제거해준다. Set은 비선형 구조이기 때문에 '순서'의 개념과 '인덱스'
velog.io
728x90
반응형
'알고리즘(BOJ) 문제풀이' 카테고리의 다른 글
[BOJ/백준] 1차원 배열_1546번 (0) | 2024.05.14 |
---|---|
[BOJ/백준] 1차원 배열_3052번 (0) | 2024.05.14 |
[BOJ/백준] 1차원 배열_10810번_실패 (0) | 2024.05.10 |
[BOJ/백준] 1차원 배열_2562번_미완성 (0) | 2024.05.09 |
[BOJ/백준] 1차원 배열_10818번_미완성 (0) | 2024.05.09 |