LeetCode编程练习 - Contains Duplicate学习心得
2017-09-22 00:36
393 查看
题目:
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.
给定一个正数数组,查找该数组是否包含任何重复项,如果数组中的任何值至少出现两次,函数返回true,若每个元素都是不同的,则返回false。
思路:
这与之前给一个排序数组删除重复元素Remove Duplicates from Sorted Array的思路相似。循环遍历每一个元素若找到一个则返回true,若没有则继续寻找,直到程序结束仍然没有就返回false。
这样写似乎没有错,但是使用的时间较长,需要重复与前面的元素比较,换一种写法,先对数组进行排序,只要判断前后值是否相同即可。
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.
给定一个正数数组,查找该数组是否包含任何重复项,如果数组中的任何值至少出现两次,函数返回true,若每个元素都是不同的,则返回false。
思路:
这与之前给一个排序数组删除重复元素Remove Duplicates from Sorted Array的思路相似。循环遍历每一个元素若找到一个则返回true,若没有则继续寻找,直到程序结束仍然没有就返回false。
这样写似乎没有错,但是使用的时间较长,需要重复与前面的元素比较,换一种写法,先对数组进行排序,只要判断前后值是否相同即可。
相关文章推荐
- LeetCode编程练习 - Roman to Integer学习心得
- LeetCode编程练习 - Majority Element学习心得
- LeetCode编程练习 - Reverse Integer学习心得
- LeetCode编程练习 - Valid Palindrome学习心得
- LeetCode编程练习 - Add Binary学习心得
- LeetCode编程练习 - Factorial Trailing Zeroes学习心得
- LeetCode编程练习 - Two Sum学习心得
- LeetCode编程练习 - Remove Element学习心得
- LeetCode编程练习 - Plus One学习心得
- LeetCode编程练习 - Longest Harmonious Subsequence学习心得
- LeetCode编程练习 - Sqrt(x)学习心得
- LeetCode编程练习 - Power of Two学习心得
- LeetCode编程练习 - Rotate Array学习心得
- LeetCode编程练习 - Add Digits学习心得
- LeetCode编程练习 - Happy Number学习心得
- LeetCode编程练习 - Ugly Number学习心得
- LeetCode编程练习 - Merge Two Sorted Lists学习心得
- LeetCode编程练习 - Count Primes学习心得
- LeetCode编程练习 - Missing Number学习心得 4000
- LeetCode编程练习 - Reverse Vowels of a String学习心得