Leecode刷题之旅-C语言/python-217存在重复元素
/*
* @lc app=leetcode.cn id=217 lang=c
*
* [217] 存在重复元素
*
* https://leetcode-cn.com/problems/contains-duplicate/description/
*
* algorithms
* Easy (47.25%)
* Total Accepted: 46K
* Total Submissions: 97.2K
* Testcase Example: '[1,2,3,1]'
*
* 给定一个整数数组,判断是否存在重复元素。
*
* 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。
*
* 示例 1:
*
* 输入: [1,2,3,1]
* 输出: true
*
* 示例 2:
*
* 输入: [1,2,3,4]
* 输出: false
*
* 示例 3:
*
* 输入: [1,1,1,3,3,4,3,2,4,2]
* 输出: true
*
*/
bool containsDuplicate(int* nums, int numsSize) {
int i,j;
for(i=;i<numsSize;i++){
for(j=i+;j<numsSize;j++){
if(nums[j]==nums[i]){
return true;
}
}
}
return false;
}
超级耗时的算法。。=。=但最好理解。
-----------------------------------------------------
python:
#
# @lc app=leetcode.cn id=217 lang=python3
#
# [217] 存在重复元素
#
# https://leetcode-cn.com/problems/contains-duplicate/description/
#
# algorithms
# Easy (47.25%)
# Total Accepted: 46K
# Total Submissions: 97.2K
# Testcase Example: '[1,2,3,1]'
#
# 给定一个整数数组,判断是否存在重复元素。
#
# 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。
#
# 示例 1:
#
# 输入: [1,2,3,1]
# 输出: true
#
# 示例 2:
#
# 输入: [1,2,3,4]
# 输出: false
#
# 示例 3:
#
# 输入: [1,1,1,3,3,4,3,2,4,2]
# 输出: true
#
#
class Solution:
def containsDuplicate(self, nums):
return len(nums) != len(set(nums))
set函数相当于把数组转换成一个集合,所以里面没有重复的元素。
这里判断原数组和集合的长度。
真的简单=。=
Leecode刷题之旅-C语言/python-217存在重复元素的更多相关文章
- Leecode刷题之旅-C语言/python-203移除链表元素
/* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...
- Leecode刷题之旅-C语言/python-83删除排序链表中的重复元素
/* * @lc app=leetcode.cn id=83 lang=c * * [83] 删除排序链表中的重复元素 * * https://leetcode-cn.com/problems/rem ...
- Leecode刷题之旅-C语言/python-35.搜索插入位置
/* * @lc app=leetcode.cn id=35 lang=c * * [35] 搜索插入位置 * * https://leetcode-cn.com/problems/search-in ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
随机推荐
- Useful WCF Behaviors - IErrorHandler
Behaviors in WCF are so stinking useful, and once you get past the basics of WCF they're arguably a ...
- 根据ip抓 包
tcpdump -i any -s 0 host 101.81.134.53 -c 1000 -w ./zhj.cap
- Eigen参考资料
Getting started https://eigen.tuxfamily.org/dox/GettingStarted.html long tutorial https://eigen.tuxf ...
- CSV 文件读写
转自:http://www.cnblogs.com/Clin/archive/2013/03/14/2959022.html public class CSVFileHelper { /// < ...
- maven工程src/main/java目录无法创建问题
在项目上右键选择properties,然后点击Javabuild path,在Librarys下,编辑JRE System Library,选择 workspace default jre 即可
- 【剑指offer】数组中的逆序对
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/mmc_maodun/article/details/27520535 转载请注明出处:http:// ...
- Join Resig's “Simple JavaScript Inheritance ”
======================Enein翻译========================= John Resig 写了一篇关于 JavaScript 里类似其它语 ...
- docker-1-简介
Dockers的出现: 一款产品从开发到上线,从操作系统,到运行环境,再到应用配置.作为开发+运维之间的协作我们需要关心很多东西, 这也是很多互联网公司都不得不面对的问题,特别是各种版本的迭代之后,不 ...
- Module、__init__.py 文件解析
一.什么是Module? 一个python Module就是一个模块,本质就是一个.py文件,其中包含了python对象的定义和python语句. 在模块内部,模块名存储在全局变量__name__中, ...
- 用LinkedList list实现栈的功能
package homework; public class Dog extends Pet { String strain = "dogxx"; int love=80; pub ...