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 ...
随机推荐
- 解决maven工程无法创建src/main/java包名的方法
我的maven工程不知道为什么无法创建src/main/java这样的包,我创建好的maven工程只有src/main/resources包,其他的主要包都没有,而且不能创建包,new出来的包都是一个 ...
- Web API 2 入门——Web API 2中的操作结果(谷歌翻译)
在这篇文章中 空虚 HttpResponseMessage IHttpActionResult 其他返回类型 作者:Mike Wasson 本主题描述ASP.NET Web API如何将控制器操作的返 ...
- iptables:no config file
防火墙规则默认都是在/etc/sysconfig/iptables这个文件中的 出现这个问题,是因为在/etc/sysconfig/目录下没有找到iptables这个文件 可以使用service ip ...
- shell语句
一.判断目录是否存在:#!/bin/bash#/bin/bash -n filename.sh 判断语句是否正确 DIR=/root/zhjif [ ! -d $DIR ];then mkdir $D ...
- nginx的开机自启、server命令启动、nginx.conf配置
1.将Nginx设置为开机自动启动 a.当上面6步完成之后,说明安装已经完全成功了,但是每次开机我们面临的一个问题,就是每次都要执行命令(1: cd /usr/local/nginx/sbin/ ...
- CentOS 6 网络yum源配置
# CentOS-Base.repo## The mirror system uses the connecting IP address of the client and the# update ...
- GO语言(八) defer注意点
package main import ( "net" "os" "fmt" "io/ioutil" ) func Cl ...
- ZT 类与类之间的四种关系
csdn上一个好贴子:http://bbs.csdn.net/topics/390646332 类与类之间的四种关系1.依赖(Dependency) 类A在类B中作为一个成员函数的参数或者是返回值 ...
- Manifold Learning: ISOMAP
转:http://hi.baidu.com/chb_seaok/item/faa54786a3ddd1d7d1f8cd0b 在常见的降维方法中,PCA和LDA是最为常用的两种降维方法.PCA是一种无监 ...
- 在WAS控制台,环境下添加新的虚拟主机别名
错误现象: 11/16/13 16:52:22:612 CST] 00000021 util W com.ibm.ws.webcontainer.util.VirtualHostCo ...