本文主要是介绍LeetCode217 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.
一个稍微简单的做法
public class solution_new {public boolean containsDuplicate(int[] nums) {if(nums.length == 0 || nums.length == 1 ) return false;if(nums.length == 2) {if(nums[0] == nums[1]) return true;}Arrays.sort(nums);for(int i = 1; i < nums.length - 1; i++) {if(nums[i] == nums[i - 1] || nums[i] == nums[i + 1]) return true;}return false;}
}
这篇关于LeetCode217 Contains Duplicate的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!