강의

멘토링

커뮤니티

Inflearn Community Q&A

inuit57's profile image
inuit57

asked

Introduction to Java Algorithm Problem Solving: Coding Test Preparation

2. Finding Common Elements (Two Pointers Algorithm)

HashSet을 사용한 풀이

Written on

·

244

0

public static void main(String[] args){
Scanner in=new Scanner(System.in);
int input1 = in.nextInt();
Set<Integer> arr1 = new HashSet<>();
for(int i =0 ; i< input1; i++){
arr1.add(in.nextInt());
}

int input2 = in.nextInt();
List<Integer> answer = new ArrayList<>();
for(int i =0 ; i< input2; i++){
int tmp = in.nextInt();
if( arr1.contains(tmp)){
answer.add(tmp);
}
}

Collections.sort(answer);
for(int ans : answer){
System.out.print(ans +" ");
}
}

계속 시간 문제가 발생해서
HashSet 에 한 쪽을 저장하고 contains 를 사용하는 방법으로
구현해서 정답 판정을 받았는데

이 풀이는 별로 효율적이지 않은걸까요?
hashsetjava코테 준비 같이 해요!

Answer

This question is waiting for answers
Be the first to answer!
inuit57's profile image
inuit57

asked

Ask a question