博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 217. Contains Duplicate
阅读量:4614 次
发布时间:2019-06-09

本文共 948 字,大约阅读时间需要 3 分钟。

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.

标签 
 
类似题目 
 
 
 
 
代码:
方法一:
1 public class Solution { 2     public boolean containsDuplicate(int[] nums) { 3         Set
hs = new HashSet
(); 4 for (int i = 0; i < nums.length; ++i) { 5 if (hs.contains(nums[i])) { 6 return true; 7 } 8 hs.add(nums[i]); 9 }10 return false;11 }12 }

 

方法二:

1 public class Solution { 2     public boolean containsDuplicate(int[] nums) { 3         Arrays.sort(nums); 4         for (int i = 1; i < nums.length; ++i) { 5             if (nums[i] == nums[i - 1]) { 6                 return true; 7             } 8         } 9         return false;10     }11 }

 

转载于:https://www.cnblogs.com/Deribs4/p/6616888.html

你可能感兴趣的文章
checkbox和文字对齐
查看>>
%s的用法
查看>>
java中==和equals
查看>>
CCActionPageTurn3D
查看>>
python random
查看>>
esp32-智能语音-cli(调试交互命令)
查看>>
netty与MQ使用心得
查看>>
关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
查看>>
swoolefy PHP的异步、并行、高性能网络通信引擎内置了Http/WebSocket服务器端/客户端...
查看>>
Python学习笔记
查看>>
unshift()与shift()
查看>>
使用 NPOI 、aspose实现execl模板公式计算
查看>>
行为型模式:中介者模式
查看>>
How to Notify Command to evaluate in mvvmlight
查看>>
33. Search in Rotated Sorted Array
查看>>
461. Hamming Distance
查看>>
Python垃圾回收机制详解
查看>>
jquery 编程的最佳实践
查看>>
MeetMe
查看>>
IP报文格式及各字段意义
查看>>