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 ...
随机推荐
- hbase高性能读取数据
有时需要从hbase中一次读取大量的数据,同时对实时性有较高的要求.可以从两方面进行考虑:1.hbase提供的get方法提供了批量获取数据方法,通过组装一个list<Get> gets即可 ...
- 第一行代码阅读笔记---详解分析第一个Android程序
以下是我根据作者的思路,创建的第一个Android应用程序,由于工具强大,代码都自动生成了,如下: package com.example.first_app; import android.os.B ...
- OpenCV相机标定
标签(空格分隔): Opencv 相机标定是图像处理的基础,虽然相机使用的是小孔成像模型,但是由于小孔的透光非常有限,所以需要使用透镜聚焦足够多的光线.在使用的过程中,需要知道相机的焦距.成像中心以及 ...
- 14 Fragment的V4包的使用
activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- TCP的发送系列 — tcp_sendmsg()的实现(一)
主要内容:Socket发送函数在TCP层的实现 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 上一篇blog讲的是send().sendto().sen ...
- bash与ksh数组使用
区别: bash与ksh在数组的使用中,最大的不同在于数组的定义. bash: declare -a arrayname ksh:set -A arrayname 其实,数组不用非要定义,在赋值的时候 ...
- ASP.net 路径问题 详解
各位有没有碰到在日常工作中经常在路径设置的时候把 "~/ ../ .../ . / .http://www.cnblogs.com/"这些符号搞混搞乱了?偶尔还会因路径的问题郁闷了 ...
- UNIX网络编程——UDP缺乏流量控制(改进版)
现在我们查看无任何流量控制的UDP对数据报传输的影响.首先我们把dg_cli函数修改为发送固定数目的数据报,并不再从标准输入读.如下,它写2000个1400字节大小的UDP数据报给服务器. 客户端程序 ...
- svn propset svn:ignore
1 添加svn:ignore svn propset svn:ignore ".settings target .classpath .project element.iml" . ...
- CSS 控制table 滑动及调整列宽等问题总结
一. 通过css控制table y方向上滚动 html中没有滚动条,可以根据overflow属性的scroll来对table显示不完全的内容进行滚动. 只是y方向上滚动,很简单,只要设置div的hei ...