File tree Expand file tree Collapse file tree 1 file changed +10
-19
lines changed Expand file tree Collapse file tree 1 file changed +10
-19
lines changed Original file line number Diff line number Diff line change 1
- /*
2
- Problem: https://leetcode.com/problems/contains-duplicate/
3
- Description: return true if any value appears at least twice in the array
4
- Concept: Array, Hash Table, Sorting
5
- Time Complexity: O(n), Runtime: 10ms
6
- Space Complexity: O(n), Memory: 58.6MB
7
- */
8
- import java .util .HashSet ;
9
- import java .util .Set ;
1
+ /**
2
+ <a href="https://leetcode.com/problems/contains-duplicate/">week01-1.contains-duplicate</a>
3
+ <li> Description: return true if any value appears at least twice in the array </li>
4
+ <li> Concept: Array, Hash Table, Sorting </li>
5
+ <li> Time Complexity: O(n), Runtime: 11ms </li>
6
+ <li> Space Complexity: O(n), Memory: 59.27MB </li>
7
+ */
10
8
11
9
class Solution {
10
+ Set <Integer > set = new HashSet <>();
11
+
12
12
public boolean containsDuplicate (int [] nums ) {
13
- Set <Integer > count = new HashSet <>();
14
- boolean answer = false ;
15
- for (int num : nums ){
16
- if (count .contains (num )) {
17
- answer = true ;
18
- break ;
19
- }
20
- count .add (num );
21
- }
22
- return answer ;
13
+ return !Arrays .stream (nums ).allMatch (set ::add );
23
14
}
24
15
}
You can’t perform that action at this time.
0 commit comments