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 | 31 |
Tags
- github
- Baekjoon
- 백준
- ChatGPT
- 더현대서울 맛집
- programmers
- 파이썬
- 데이콘
- hackerrank
- dacon
- 코로나19
- 프로그래머스
- 금융문자분석경진대회
- 프로그래머스 파이썬
- leetcode
- 편스토랑 우승상품
- 우분투
- Real or Not? NLP with Disaster Tweets
- 캐치카페
- AI 경진대회
- 맥북
- SW Expert Academy
- Git
- gs25
- 자연어처리
- Kaggle
- 편스토랑
- ubuntu
- Docker
- PYTHON
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
'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 |
Comments