leetcode之旅(8)-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.
Subscribe to see which companies asked this question
分析:
要求是找出整数数组的重复,如果重复是true;不重复返回false,先排序。判断相邻的元素是不是相同
思考:
可见排序的重要性,据说计算量的40%是排序
代码:
import java.util.Arrays;
public class Solution {
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
int length = nums.length;
for(int i = 0;i < (length-1);i++){
if(nums[i] == nums[i+1]){
return true;
}
}
return false;
}
}
leetcode之旅(8)-Contains Duplicate的更多相关文章
- LeetCode之旅(13)-Valid Anagram
题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- leetcode之旅(11)-Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- 【LeetCode】287. Find the Duplicate Number
Difficulty:medium More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...
- 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- leetcode【sql】 Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- LeetCode.1089-重复的0(Duplicate Zeros)
这是小川的第392次更新,第423篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第255题(顺位题号是1089).给定一个固定长度的整数数组arr,复制每次出现的零,将剩 ...
- [算法学习]开始leetcode之旅
在此记录一下用javascript刷leetcode的过程,每天都要坚持! 1.Two Sum Given an array of integers, find two numbers such th ...
随机推荐
- 集合框架之List接口
有序的 collection(也称为序列).此接口的用户可以对列表中每个元素的插入位置进行精确地控制.用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素. 与 set 不同,列表 ...
- Android水印相机
本篇文章实现的水印相机,类似于qq空间中的水印相机功能,因之前看过一个demo上实现了一个简陋的水印相机功能,觉得挺有意思,就在此基础上进行了修改,优化和完善,并增加了部分功能,使之更接近于qq水印相 ...
- UNIX网络编程——UDP 中的外出接口的确定
已连接UDP套接字还可用来确定用于特定目的地的外出接口.这是由connect函数应用到UDP套接字时的一个副作用造成的:内核选择本地IP地址.这个本地IP地址通过为目的IP地址搜索路由表得到外出接口, ...
- (八十六)使用系统自带的分享框架Social.framework
使用Social.framework十分简单,能够便捷的分享到主流的社交框架. ①导入主头文件 #import <Social/Social.h> ②以新浪微博为例,首先判断服务是否可用, ...
- UNIX网络编程——TCP回射服务器/客户端程序
下面通过最简单的客户端/服务器程序的实例来学习socket API. serv.c 程序的功能是从客户端读取字符然后直接回射回去: #include<stdio.h> #include&l ...
- 如何在Cocos2D 1.0 中掩饰一个精灵(六)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰一个精灵:实现代码 打开HelloWorldLayer.m并 ...
- Web Service进阶(二)如何用Apache TCPMon来截获SOAP消息
注:以下是关于TCPMon的一些使用常识,如果不需要或是已经熟悉就不用往下看了. 在WebService服务器和客户机之间会传递SOAP消息,有时我们需要得到这些消息以便调试,而Apache的TCPM ...
- Web开发的发展史
英文出处:arunr.欢迎加入翻译小组. 导读:Arunr 把过去 15 年以来,Web开发从最初的纯 HTML 到 CGI.PHP\JSP\ASP.Ajax.Rails.NodeJS 这个过程简要地 ...
- 【一天一道LeetCode】#100. Same Tree(100题大关)
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- Mybatis源码之(TypeAliasRegistry)TypeAlias别名实现机制
在Mybatis编程中我们经常会用到将某个bean作为参数类型parameterType或者结果返回值类型ResultType,所以很多时候我们需要把完成的Bean的包名在mapper文件中写上,如下 ...