题目:

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的更多相关文章

  1. LeetCode之旅(13)-Valid Anagram

    题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  2. leetcode之旅(11)-Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. 【LeetCode】287. Find the Duplicate Number

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...

  5. 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...

  6. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  7. leetcode【sql】 Delete Duplicate Emails

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  8. LeetCode.1089-重复的0(Duplicate Zeros)

    这是小川的第392次更新,第423篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第255题(顺位题号是1089).给定一个固定长度的整数数组arr,复制每次出现的零,将剩 ...

  9. [算法学习]开始leetcode之旅

    在此记录一下用javascript刷leetcode的过程,每天都要坚持! 1.Two Sum Given an array of integers, find two numbers such th ...

随机推荐

  1. Dynamics CRM2013 用户进入系统所必需的那些权限

    本篇以CRM2013为例,在CRM中新建一个安全角色后该安全角色基本是空的,如果新建的安全角色作为一个账号的唯一安全角色时,那这个安全角色除了需要配置业务场景所需的权限外,是要优先具备进入CRM系统的 ...

  2. Linux--FTP和MAIL服务器

     1) FTP协议 FTP(FileTransfer Protocol,文件传输协议)用于管理计算机之间的文件传送.FTP 是Internet 上使用非常广泛的一种通讯协议,它是由支持Intern ...

  3. UE4联机烘焙

    联机烘焙就是为了利用多台电脑解决烘焙效率的问题 1.UE4的烘焙工具在安装目录下的\Engine\Binaries\DotNET,比如我这里是E:\UnrealEngine-release\Engin ...

  4. Java--Dom解析XML文件

          之前写过几篇关于Java中解析XML文件的方法,不过,感觉不够简单,今天重写了一遍代码,用到的是方法是Dom,其中加入了日志记录功能--Log4j.       好了,不多说了,先把XMl ...

  5. linux下字节对齐

    一,内存地址对齐的概念    计算机内存中排列.访问数据的一种方式,包含基本数据对齐和结构体数据对齐.    32位系统中,数据总线宽度为32,每次能够读取4字节数据.地址总线为32,最大寻址空间为4 ...

  6. 物料事务处理interface与temp解析

    MTL_TRANSACTIONS_INTERFACE MTL_TRANSACTIONS_INTERFACE is the interface point between non– Inventory ...

  7. Uva - 1594 - Ducci Sequence

    水题,算出每次的结果,比较是否全0,循环1000次还不是全0则LOOP AC代码: #include <iostream> #include <cstdio> #include ...

  8. Chapter 2 User Authentication, Authorization, and Security(4):限制SA帐号的管理权限

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/38817915,专题目录:http://blog.csdn.net/dba_huangzj ...

  9. C++ Primer 有感(标准库vector及迭代器)

    vector是同一种对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库将负责管理与存储元素相关的类存.引入头文件 #include<vector> 1.vecto ...

  10. android 的android httpClient详解

    AndroidHttpClient结构: public final class AndroidHttpClient extends Object implements HttpClient 前言: 这 ...