Leetcode_217_Contains Duplicate
2015-05-30 09:16
393 查看
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46271159
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)题意为判断给定的整数数组中是否包含相同的元素,如果包含则返回false,否则返回true。
(2)该题比较简单。通过构造一个Map来存放遍历得到的数值,如果在后续的遍历中从Map能够获取到value,则说明有重复的,返回false;否则继续遍历直到结束,返回true。
(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)题意为判断给定的整数数组中是否包含相同的元素,如果包含则返回false,否则返回true。
(2)该题比较简单。通过构造一个Map来存放遍历得到的数值,如果在后续的遍历中从Map能够获取到value,则说明有重复的,返回false;否则继续遍历直到结束,返回true。
(3)详情见下方代码。希望对你有所帮助。
算法代码实现如下:
/** * @author liqqc */ import java.util.HashMap; import java.util.Map; public class ContainsDuplicate { public boolean containsDuplicate(int[] nums) { if (nums == null || nums.length == 0) { return false; } Map<Integer, Integer> maps = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) { if (maps.get(nums[i]) == null) { maps.put(nums[i], nums[i]); } else if (maps.get(nums[i]) != null) { return true; } } return false; } }
相关文章推荐
- Lettcode_217_Contains Duplicate
- microsoft office 2016 preview 下载试用
- retain/assign/strong/weak/copy/mutablecopy/autorelease区别
- 217 Contains Duplicate
- Contains Duplicate II
- leetcode#70 Climbing Stairs
- LeetCode "Contains Duplicate II"
- [LeetCode] Contains Duplicate II 包含重复值之二
- Contains Duplicate II
- [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database
- POJ 3250 Bad Hair Day 简单DP 好题
- Andriod SDK出现Failed to parse the output of 'adb version'问题
- leetcode 11 -- Container With Most Water
- Chinese Remainder Theorem
- POJ_3421_X-factor Chains(素数筛法)
- Android快捷开关实现【http://blog.csdn.net/stevenhu_223/article/details/9052083】
- solr-DIH:dataimport增量全量创建索引
- CCNA training notes
- STM32使用J-Link烧写出错:Error: Flash Download failed - Cortex-M3
- Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别