Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- leetcode
- PYTHON
- Docker
- 금융문자분석경진대회
- 파이썬
- 캐치카페
- Kaggle
- 데이콘
- 백준
- 자연어처리
- github
- SW Expert Academy
- hackerrank
- 맥북
- 편스토랑 우승상품
- 더현대서울 맛집
- 코로나19
- gs25
- 우분투
- 프로그래머스 파이썬
- ChatGPT
- 편스토랑
- ubuntu
- Baekjoon
- dacon
- programmers
- AI 경진대회
- Real or Not? NLP with Disaster Tweets
- 프로그래머스
- Git
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 219. Contains Duplicate II (Python) 본문
728x90
반응형

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
Solution
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
if len(list(set(nums))) == len(nums):
return False
for i in range(len(nums)):
for j in range(1, k+1):
if i+j > len(nums) -1:
continue
if nums[i] == nums[i+j]:
return True
return False



SOMJANG/CODINGTEST_PRACTICE
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 509. Fibonacci Number (Python) (0) | 2020.11.01 |
---|---|
[leetCode] 482. License Key Formatting (Python) (0) | 2020.10.31 |
[leetCode] 217. Contains Duplicate (Python) (0) | 2020.10.26 |
[leetCode] 179. Largest Number (Python) (0) | 2020.10.25 |
[leetCode] 27. Remove Element (Python) (0) | 2020.10.24 |