Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Math
- Counting
- Binary Tree
- simulation
- hash table
- geometry
- database
- SQL
- string
- Method
- Class
- array
- 자바
- greedy
- Number Theory
- java
- sorting
- two pointers
- Data Structure
- Tree
- dynamic programming
- 구현
- 코딩테스트
- 파이썬
- 코테
- Matrix
- implement
- Binary Search
- Stack
- bit manipulation
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 219. Contains Duplicate II 본문
1. Input
1) int[] nums
2) int k
2. Output
1) 다음을 만족하는 두 정수가 있다면 true, 없다면 false를 반환
// 조건: 서로 다른 i, j에 대하여
- nums[i] == nums[j]
- abs(i-j) <= k
3. Constraint
1) 1 <= nums.length <= 10^5
2) - 10^9 <= nums[i] <= 10^9
3) 0 <= k <= 10^5
4. Example
Input: nums = [1,2,3,1], k = 3 -> Output: true
설명: nums[0] == nums[3]이고, abs(0-3)<=3이므로 true를 반환한다.
5. Code
1) 첫 코드(2023/01/23)
boolean answer = false;
if(nums.length==2){
answer = nums[0]==nums[1];
} else{
for(int i=0 ; i<nums.length-1 ; i++)
for(int j=i+1 ; j<=Math.min(i+k,nums.length-1) ; j++)
if(nums[i]==nums[j]){
answer = true; break;
}
}
return answer;
- 성능을 보아하니 공부가 더 필요해 보인다..
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Medium] 229. Majority Element II (0) | 2023.01.23 |
---|---|
[LeetCode/Hard] 220. Contains Duplicate III (0) | 2023.01.23 |
[LeetCode/Easy] 205. Isomorphic Strings (0) | 2023.01.23 |
[LeetCode/Easy] 203. Remove Linked List Elements (0) | 2023.01.23 |
[LeetCode/Easy] 202. Happy Number (0) | 2023.01.23 |