LeetCode【217. 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.
思路1.
对每一个数字都与其后序的数字进行比较,如果相同则返回true,如果不同,则指导遍历完最后一个数字返回false,时间复杂度为(n-1)+(n-2)+....2+1,所以是O(n2),代码就不写了,这方法不好。
思路2.
先排序,然后在遍历vector,如果有相邻的值相同,则返回true,负责将pivot设置为当前的值。总的时间复杂度为排序O(nlogn)+遍历O(n)
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
bool flag = false;
int len = nums.size();
if(len > 1)
{
sort(nums.begin(),nums.end());
int MarkNum = nums[0];
for(int i = 1; i<= len; i++)
{
if(nums[i] == MarkNum)
flag = true;
else
MarkNum=nums[i];
}
}
else
flag = false;
return flag;
}
};
思路3,利用额外空间,unordered_map,如果元素不存在,计数器的值加1,如果存在,则返回true
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int,int> cmpMap;
for( int i = 0; i < nums.size(); i++ ){
if( cmpMap.find(nums[i]) == cmpMap.end() ){
cmpMap[nums[i]]++;
}
else{
return true;
}
}
return false;
}
};
思路3我本来以为时间复杂度应该在O(n)但提交以后,速度还没思路2的快,不知道是什么原因。
LeetCode【217. Contains Duplicate】的更多相关文章
- LeetCode【第1题】Two Sum
准备刷一刷LeetCode了. 题目: ''' Given an array of integers, return indices of the two numbers such that they ...
- LeetCode 【31. Next Permutation】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 【190. Reverse Bits】
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- LeetCode【169. Majority Element】
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode OJ 217.Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode【112. 路径总和】
思路就是从根节点开始向下选节点,依次与sum比较大小,若小,则向下选左右节点其中一个,若大,则接下来判断是否是叶子节点,若是,则返回false 若不是,则上一步选另一节点,再将上述重新执行. 对于叶子 ...
- LeetCode【101. 对称二叉树】
对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...
- leetcode 【 Linked List Cycle 】 python 实现
题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...
随机推荐
- caffe中python接口的使用
下面是基于我自己的接口,我是用来分类一维数据的,可能不具通用性: (前提,你已经编译了caffe的python的接口) 添加 caffe塻块的搜索路径,当我们import caffe时,可以找到. 对 ...
- mysql简介
1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅仅是存储和管理数 ...
- C语言输出时的各种%
d 以十进制形式输出带符号整数(正数不输出符号) o 以八进制形式输出无符号整数(不输出前缀O) x 以十六进制形式输出无符号整数(不输出前缀OX) u 以十进制形式输出无符号整数 f 以小 ...
- 用JDBC访问MySQL
/* 在数据库中创建一个Employee的类 create table Employee( id int primary key, name varchar(20), age int); */ imp ...
- 读<jQuery 权威指南>[6]--实用工具函数
官方地址:http://api.jquery.com/category/utilities/ 一.数组和对象操作 1. $.each——遍历 $.each(obj,function(param1,pa ...
- Java类的成员函数调用顺序
class A { public A() { System.out.println("----------A 构造-------------"); } static void sb ...
- powershell脚本闪电输入神器
如图所示: 用autohotkey,把下列常用按键互唤.来达到快速输入目的.并且只对powershell ise,power gui,visual studio code,powershell stu ...
- AsyncTask的基本使用和各个参数的说明
AsyncTask 的执行分为四个步骤,每一步都对应一个回调方法,需要注意的是这些方法不应该由应用程序调用,开发者需要做的就是实现这些方法.在任务的执行过程中,这些方法被自动调用. * onPreEx ...
- RichTextBox文字处理控件属性介绍
RichTextBox控件是一种既能够输入文本. 又能够修改文本的文字处理控件, 与TextBox控件比较, RichTextBox控件的文字处理功用更加丰厚, 不只能够设定文字的色彩. 字体, 还具 ...
- MVC乱码可能的原因
1.数据传输不对,或者根本没有进控制器 2.分部视图建立时一定要选择