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 ...
随机推荐
- java 中linq 的使用方式 筛选 查找 去重
1.筛选 $.Enumerable.From(value).Where(function(x) {//value 为被操作的内容 return x.name == name;//第一个name为val ...
- C# Winform App 获取当前路径
直接双击执行 D:\test1.exeSystem.Diagnostics.Process.GetCurrentProcess().MainModule.FileName D:\Test1.exe S ...
- 使用Jmeter进行接口测试和压力测试的配置和使用
1. Jmeter简介 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. JMeter 可 ...
- Architecture pattern & Architecture style
Architecture pattern: context + problem -> solution Architecture style: solution part of architec ...
- 牛客网多校训练第三场 C - Shuffle Cards(Splay / rope)
链接: https://www.nowcoder.com/acm/contest/141/C 题意: 给出一个n个元素的序列(1,2,...,n)和m个操作(1≤n,m≤1e5),每个操作给出两个数p ...
- Win32线程——优先权
<Win32多线程程序设计>–Jim Beveridge & Robert Wiener Win32 优先权是以数值表现的,并以进程的“优先权类别(priority class)” ...
- 持续集成之Jenkins+Gitlab实现持续集成
项目使用git+jenkins实现持续集成 开始构建 General 源码管理 我们安装的是Git插件,还可以安装svn插件 我们将git路径存在这里还需要权限认证,否则会出现error 我们 ...
- centos中java安装跟配置
安装配置java环境 [root@JxateiLinux src]# Wget http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c ...
- 2018年暑假ACM个人训练题6 解题报告
A:水题 https://www.cnblogs.com/yinbiao/p/9311834.html B:考察进制的转化 https://www.cnblogs.com/yinbiao/p/9311 ...
- HDU 2097 sky数 (进制转化)
传送门: Sky数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...