leetcode:217 Contains Duplicate-每日编程第九题
2015-12-10 14:37
387 查看
Contains Duplicate
Total Accepted: 54264 Total Submissions: 139273 Difficulty: EasyGiven 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).排序。
2).遍历数组,看是否有相邻的数相等
class Solution { public: bool containsDuplicate(vector<int>& nums) { int len=nums.size(); if(len<2){ return false; } sort(nums.begin(),nums.end()); for(int i=0;i<len-1;i++){ if(nums[i]==nums[i+1]){ return true; } } return false; } };
相关文章推荐
- C#XML的序列化与反序列化
- C# 通过Hook的方法 屏蔽快捷键
- leetcode:242 Valid Anagram-每日编程第八题
- eclipse设置项目默认编码
- (java) Path Sum
- java中线程创建的第二种方法
- myeclipse中xml自动补全
- leetcode:226 Invert Binary Tree-每日编程第七题
- MyEclipse SVN的使用
- Mac系统的JDK8默认安装路径
- leetcode:100 Same Tree-每日编程第六题
- java异常处理【转载】
- Spring mvc 接收json对象
- Eclipse最最最常用的快捷键
- leetcode:283 Move Zeroes-每日编程第五题
- SpringMVC的Date与String互转
- Spring Security 自定义登录页面
- Java之旅--ConcurrentModificationException关于异常原因和解决方法
- eclipse_mars不支持testng插件
- (java)Pascal's Triangle II