leetcode笔记:Contains Duplicate
一. 题目描写叙述
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
二. 题目分析
题目的大意是,给定一个整数数组,推断数组中是否包括反复的元素。若数组中随意一个数字出现了至少两次,函数返回true
;否则,返回false
。
这里提供两个方法:
- 先对数组进行排序。然后遍历数组,若出现两个相邻元素的值同样时。表明有反复元素。返回true。反之返回false。
- 使用map来实现。遍历数组。每訪问一个元素。看其是否在map中出现。如已出现过。则存在反复元素,返回
true
;如没有,则将元素增加到map中。
三. 演示样例代码
// 方法一
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int n = nums.size();
if (n < 2) return false;
sort(nums.begin(), nums.end());
for (int i = 1; i < n; ++i)
if (nums[i] == nums[i - 1]) return true;
return false;
}
};
// 方法二
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int, int> numsMap;
for (int i = 0; i < nums.size(); ++i) {
if(numsMap.count(nums[i])){
return true;
}
numsMap.insert(pair<int, int>(nums[i], i));
}
return false;
}
};
四. 小结
相关的题目有:Contains Duplicate II 和 Contains Duplicate III。
leetcode笔记:Contains Duplicate的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
随机推荐
- node.js---对文件操作
1. var fs=require('fs'); fs.open(path,flag,[mode],callback); path:要打开的文件路径 flags:要打开文件的方式 读/写 mode:设 ...
- 【转】4w+1h 教你如何做用户画像
记得14年开始做用户画像的时候,对于用户画像完全没有概念,以为是要画一幅幅图画,经过两年多的学习和理解,渐渐的总结出了一些方法和技巧,在这里就通过4个W英文字母开头和1个H英文字母开头的单词和大家分享 ...
- cdev_add
初始化 cdev 后,需要把它添加到系统中去.为此可以调用 cdev_add()函数.传入cdev 结构的指针,起始设备编号,以及设备编号范围. 函数首先将分配的设备号与设备数目保存进cdev结构体中 ...
- PAT Basic 1047
1047 编程团体赛 编程团体赛的规则为:每个参赛队由若干队员组成:所有队员独立比赛:参赛队的成绩为所有队员的成绩和:成绩最高的队获胜. 现给定所有队员的比赛成绩,请你编写程序找出冠军队. 输入格式: ...
- Wp检查手机网络状态
/// <summary> /// 检查网络状态 /// </summary> private void CheckNetworkState() { if (DeviceNet ...
- HDU-4612 Warm up,tarjan求桥缩点再求树的直径!注意重边
Warm up 虽然网上题解这么多,感觉写下来并不是跟别人竞争访问量的,而是证明自己从前努力过,以后回头复习参考! 题意:n个点由m条无向边连接,求加一条边后桥的最少数量. 思路:如标题,tarjan ...
- docker命令解析
1.docker run --name lllllll -d -p 8080:8080 -p 9000:9000 镜像id 将docker8080端口映射到服务器的8080端口 ...
- Redis集群模式配置
redis集群部署安装: https://blog.csdn.net/huwh_/article/details/79242625 https://www.cnblogs.com/mafly/p/re ...
- poj 3311 状压dp 最短路
C - Hie with the Pie Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- Python入门---易错已错易混淆----知识点
1.not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9 结果会输出啥? 根据优先级:(not 1) or (0 and 1) or (3 a ...