File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ class SolutionGotprgmer {
3+ // 해당 문제는 어느 한 숫자가 2개이상 존재할 경우 true를 그렇지 않을 경우, false를 반환하는 문제이다.
4+ // set을 사용해서 set에 이미 값이 존재한다면 개수가 2 이상이므로 true 그렇지 않으면 false를 출력한다.
5+
6+ // 각 숫자들을 저장해서 set으로 관리 -> distinctNums
7+ // nums의 각 숫자인 checkNum을 distinctNums에 넣어준다.
8+ // 만약 checkNum이 이미 distinctNums에 존재한다면 ans를 true로 만들어주고 답을 출력한다.
9+
10+
11+ // 시간복잡도 -> O(n)
12+ // 공간복잡도 -> O(n)
13+ static Set <Integer > distinctNums ;
14+ public boolean containsDuplicate (int [] nums ) {
15+ distinctNums = new HashSet <>();
16+ boolean ans = false ;
17+ for (int checkNum :nums ){
18+ if (distinctNums .contains (checkNum )){
19+ ans = true ;
20+ break ;
21+ };
22+ distinctNums .add (checkNum );
23+ }
24+ return ans ;
25+ }
26+ public static void main (String [] args ){
27+ Solution s = new Solution ();
28+ System .out .println (s );
29+
30+ }
31+
32+
33+ }
You can’t perform that action at this time.
0 commit comments