leetcode [Contains Duplicate]//待整理多种解法
来源:互联网 发布:淘宝手机详情页宽度 编辑:程序博客网 时间:2024/06/08 15:42
public class Solution { public boolean containsDuplicate(int[] nums) { HashMap<Integer, Integer> map = new HashMap<>();//用map来记录数组中的元素出现的次数 for(int i = 0; i < nums.length; i++){//遍历数组,建立哈希表 if(map.containsKey(nums[i])){ map.put(nums[i], map.get(nums[i]) + 1); } else{ map.put(nums[i], 1); } } for(int count : map.values()){//遍历map的元素出现次数的集合,若有大于等于2的就返回正确 if(count >= 2){ return true; } } return false; }}
可以不用Map,而用Set,看这个元素之前装进去没有过,则只用写一个循环
public class Solution { public boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<>(); for(int num : nums){ if(set.contains(num)){ return true; } else{ set.add(num); } } return false; }}
0 0
- leetcode [Contains Duplicate]//待整理多种解法
- leetcode [Contains Duplicate II]//待整理多种解法
- leetcode [Path Sum]//待整理多种解法
- leetcode [Majority Element]//待整理多种解法
- leetcode [Rotate Array]//待整理多种解法
- leetcode [Reverse Bits]//待整理多种解法
- leetcode [Happy Number]//待整理多种解法
- leetcode [Isomorphic Strings]//待整理多种解法
- leetcode[Add Digits]//待整理多种解法
- leetcode[Ugly Number]//待整理多种解法
- leetcode[Missing Number]//待整理多种解法
- leetcode[Word Pattern]//待整理多种解法
- leetcode[Ransom Note]//待整理多种解法
- leetcode[Longest Palindrome]//待整理多种解法
- leetcode[Fizz Buzz]//待整理多种解法
- leetcode[Add Strings]//待整理多种解法
- leetcode[Hamming Distance]//待整理多种解法
- leetcode[Island Perimeter]//待整理多种解法
- LeetCode074 Search a 2D Matrix
- StringHelper--封转自己的字符串工具类
- Spring框架介绍和IoC容器中Bean配置(Spring框架)
- ICMP报文和ping命令
- 一些JavaScript小技巧
- leetcode [Contains Duplicate]//待整理多种解法
- SRM 556 Div1 500
- java源码Integer类toBinaryString()方法探究
- 二分图的匹配
- 一篇SSM框架整合友好的文章(三)
- LeetCode075 Sort Colors
- POJ-1017 Packets
- 数组
- There is no getter for property named 'itmesCustom' in 'class com.ssm.po.Ite