当前位置:网站首页>[leetcode] there are duplicate elements [217]
[leetcode] there are duplicate elements [217]
2022-07-02 22:52:00 【When camellia flowers bloom.】
problem : Give you an array of integers nums, If any value appears in the array At least twice , return True; If each element in the array is different from each other , return False
Example 1
Input : nums = [1,2,3,1]
Output : True
Example 2
Input : nums = [1,2,3,4]
Output : False
Example 3
Input : nums = [1,1,1,3,3,4,3,2,4,2]
Output : True
Python3 Problem solving
# solution 1:set duplicate removal , Compare the length
def containsDuplicate(nums):
len_set_nums = len(set(nums))
if (len_set_nums != len(nums)):
return True
else:
return False
# solution 2: Sort , Comparison is equal
def containsDuplicate(nums):
nums.sort()
for i in range(len(nums)-1):
if nums[i+1] == nums[i]:
return True
return False
# solution 3
def containsDuplicate(nums):
if len(nums) <= 1:
return False
hashset = set()
for i in nums:
if i in hashset:
return True
else:
hashset.add(i)
return False
containsDuplicate([1,2,3,1]) # True
containsDuplicate([1,2,3,4]) # False
containsDuplicate([1,1,1,3,3,4,3,2,4,2]) # True
边栏推荐
- Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
- go 4種單例模式
- #include errors detected. Please update your includePath.
- 【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
- 位的高阶运算
- Build your own website (22)
- 牛客网:最大子矩阵
- 加油站[问题分析->问题转换->贪心]
- Pointer - function pointer
- Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
猜你喜欢
手写ORM(对象关系映射)增删改查
NC24325 [USACO 2012 Mar S]Flowerpot
Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?
SimpleITK使用——4. 奇怪的問題
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
Socket socket c/s end process
【板栗糖GIS】global mapper 如何通过dsm批量制作贴地等高线
小鹏P7出事故,安全气囊未弹出,这正常吗?
wait解决僵尸进程
随机推荐
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
Simpleitk use - 4 Strange question
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
Golang面试整理 三 简历如何书写
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
php实现根据输入的年龄查询出生日期符合的数据
Zhong Xuegao responded that the product will not melt for 1 hour: it contains solid components and cannot melt into water
Golang的学习路线
[LeetCode] 反转字符串【344】
Introduction and response to high concurrency
uniapp微信登录返显用户名和头像
[LeetCode] 存在重复元素【217】
【外刊】睡眠与减肥
The threshold value of fusing proportion cannot be changed with sentinel, and setting the slow call proportion has no effect
Perceptron model and Application
NC24325 [USACO 2012 Mar S]Flowerpot
牛客网:最大子矩阵
Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
Build your own website (22)