leetcode 存在重复元素
给定一个整数数组,判断是否存在重复元素。
如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。
示例 1:
输入: [1,2,3,1]
输出: true
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function (nums) {
for (let i = 0; i !== nums.length; i++) {
let index = nums.indexOf(nums[i], i + 1);
if (index !== -1) {
if (nums.indexOf(nums[i], index) !== -1) {
return true;
}
}
}
return false;
};
不懂算法只能靠常识性逻辑思考了,先不管这么多了,能做出来就很开心(难过)…
var containsDuplicate = function (nums) {
nums.sort((a, b) => a - b);
if (!nums.length) {
return false;
}
for (let i = 0; i !== nums.length - 1; i++) {
if (nums[i] === nums[i + 1]) {
return true;
}
}
return false;
};
有逻辑的解决办法…
leetcode 存在重复元素的更多相关文章
- Leetcode 存在重复元素 (219,220)
219. 存在重复元素 II 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. / ...
- python(leetcode)-重复元素算法题
leetcode初级算法 问题描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 该问题表述非常简单 ...
- LeetCode:存在重复元素【217】
LeetCode:存在重复元素[217] 题目描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 ...
- LeetCode:删除排序链表中的重复元素【83】
LeetCode:删除排序链表中的重复元素[83] 题目描述 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示 ...
- 【Leetcode】【简单】【217. 存在重复元素】【JavaScript】
题目描述 217. 存在重复元素 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [ ...
- 力扣(LeetCode)删除排序链表中的重复元素II 个人题解
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 思路和上一题类似(参考 力扣(LeetCode)删除排序链表中的重复元素 个人题解)) 只不过这里需要用到一个前 ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
随机推荐
- halcon的长度和角度测量
halcon代码: 1: *读取并截取图片 2: dev_close_window() 3: read_image (Image, 'D:/MyFile/halcon/长度和角度测量/图.png') ...
- Windows下安装logstash
1. 下载 https://www.elastic.co/downloads/logstash https://www.elastic.co/downloads/past-releases 2. 文档 ...
- objective-C中的"非正式协议"和“正式协议”
objective-C中的接口与泛型 先承认我是标题党,因为在obj-c的世界中,官方根本没有"接口"与"泛型"这样的说法. 不过在obj-c中有二个与之接近的 ...
- 返回一个可用的名称如1_4.txt
); MessageBox.Show(tt); }
- 认识单元测试(jar包资源网址:http://search.maven.org/)
单元测试在3.8版本以前只需要导入一个jar包,3.8版本以后需要导入二个jar包.(网址:http://junit.org/junit4/) package com.huawei.junit; im ...
- linux开机自检配置文件fstab变只读无法修改问题
控制linux开机自检的配置文件是/etc/fstab,在最近用的服务器中,发现fstab变成了只读权限,无法修改. 解决方法:RH5下,因磁盘改变,而导致系统停在Ctrl+d,此时需输入密码进入修改 ...
- kalman处理realsense数据
代码来自:https://www.cnblogs.com/zjuhjm/archive/2012/12/29/2838472.html import numpy as npimport matplot ...
- windows下的phpunit安装
Windows Globally installing the PHAR involves the same procedure as manually installing Composer on ...
- C#中接口声明属性,但是提示“接口”中不能有属性。
C#中接口定义属性如下所示: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- List 组件简单示例及其onItemsDisclosure点击事件
来自<sencha touch权威指南>第9章,276页开始 ------------------------------------------------- app.js代码如下: E ...