[Leetcode]217. Contains Duplicate
2024. 7. 30. 19:32ㆍjava/javaAlgorithm
1. problem :
https://leetcode.com/problems/contains-duplicate/
2. solution 1 :
public class Solution {
public boolean containsDuplicate(int[] nums) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i=0; i < nums.length;i++) {
if (hashMap.containsKey(nums[i])) {
return true;
} else {
hashMap.put(nums[i],i);
}
}
return false;
}
}
HashMap을 써서, 값이 들어있다면, true 반환, 없다면 추가, 루프가 정상적으로 끝난다면, false 반환
'java > javaAlgorithm' 카테고리의 다른 글
[Leetcode]1. Two Sum (0) | 2024.07.30 |
---|---|
[Leetcode]242. Valid Anagram (0) | 2024.07.30 |
[Leetcode]3146. Permutation Difference between Two Strings (0) | 2024.07.30 |
[Leetcode]1679. Max Number of K-Sum Pairs(x) (0) | 2024.07.30 |
[Leetcode]1111. Maximum Nesting Depth of Two Valid(x) (0) | 2024.07.30 |