题目:

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. Cocos2D在Xcode7和iOS 9.2上IMP调用出错

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 原来的代码一直在Xcode6.4上和iOS 8.4上运行,没有 ...

  2. [cacti]nginx+php+cacti+mysql+php-fpm 安装小记

    网上教程很多,但是nginx不太多,下面安装时候主要参考的篇文章: http://54im.com/linux/linux-cacti-cn-install.html http://www.tecmi ...

  3. 2、Android构建本地单元测试

    如果你的单元测试在Android中没有依赖或者只有简单的以来,你可以在你的本地开发环境中运行你的测试.这种测试比较高效因为它能让你避免将整个app安装到物理设备或虚拟机中执行单元测试.最后,执行单元测 ...

  4. [ExtJS5学习笔记]第四节 欢迎来到extjs5-手把手教你实现你的第一个应用

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38331347 本文作者:sushengmiyan ------------------ ...

  5. Dynamics CRM 2015 Online Update1 UI界面的更新变化

    听说出  Dynamics CRM 2015 Online  Update1了,立马跑去申请了个30天试用版简单的看了下,UI上的变化还是让人耳目一新的,也可能是被CRM2013的UI蹂躏太久了没 ...

  6. Android必知必会-Handler可能引起的内存泄露

    在Android开发中,编写多线程通常会使用到Thread和Handler,细心的朋友会发现,很常见的写法会被编辑器提示有问题,new Handler(){} 内的代码背景颜色会变成黄色.Androi ...

  7. JS中回调函数的写法

    <!DOCTYPE HTML> <html><head>  <meta charset="GBK" /><title>回 ...

  8. 对“传统BIOS”与“EFI/UEFI BIOS”的基本认识

    硬盘(MBR磁盘)分区基本认识+Windows启动原理 大家常会看到硬盘分区中这样的几种说法:系统分区.启动分区.活动分区.主分区.拓展分区.逻辑分区,MBR.PBR.DPT.主引导扇区等.尤其是看到 ...

  9. 版本控制之最佳实践(Git版)

    现如今,应该每个开发者都在使用版本控制工具了吧.然而,如果你理解版本控制的基本规则,你便能更好地发挥它的效用.在此,我们汇总了一些最佳实践,希望你在使用Git做版本控制时能够了然于心.得心应手. 1. ...

  10. sed-加速你在Linux的文件编辑

    1. Sed简介 sed是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区中 ...