首页 > 其他 > 详细

查找问题 set 与 map

时间:2020-05-10 12:01:39      阅读:62      评论:0      收藏:0      [点我收藏+]

主要问题

1.查找有无 set 

set (实现collection接口)

实现类:

hashset(hashcode()和equals()保证唯一性)

treeset(对象排序)(红黑树)

linkedhashset(双向链表和哈希表)

底层实现:hashmap 对应value是一个静态object对象。

2.对应频次 map

map(独立接口)

实现类:

hashmap(数组+链表/红黑树)(无synchronized 线程不安全、效率较高)(允许null)

解决hash冲突:

  • 再散列
  • 链表

hashtable

synchronized修饰,效率低;no null(equals())需要对象。

linkedhashmap\

arraymap(android.util中的类)

对hash数组二分查找。

相比hashmap,内存消耗减少30%。

(hashmap初始容量16,达到0.75时扩大一倍,每次扩容要重新计算成员位置;arraymap,size大于8,申请size*1.5,4-8申请8,小于4申请4,但是扩容频次高。所以数据量较大时hashmap更合适。)

treemap(红黑树)

父类:sortmap

 

相关算法题

202happy number

class Solution {
    public boolean isHappy(int n) {
        HashSet<Integer> h=new HashSet();
        while(n!=1&&!h.contains(n)){//若某数字已经出现,则陷入循环,不符合。
            h.add(n);
            n=sol(n);
        }
        return n==1;  
    }
    public int sol(int n){
        int sum=0;
        while(n>0){
            int a=n%10;
            n=n/10;
            sum+=a*a;
        }
        return sum;
    }
}

205. Isomorphic Strings
242. Valid Anagram

290. Word Pattern

451. Sort Characters By Frequency

 

查找表经典问题

1.数据集大小?

2.n^2是否可接受?

1. Two Sum

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map <Integer,Integer> map=new HashMap<>();
        int[] a=new int[2];
        for(int i=0;i<nums.length;i++){
            int b=target-nums[i];
            if(map.containsKey(b)){
                a[0]=map.get(b);
                a[1]=i;
            }
            map.put(nums[i],i);
        }
        return a;
    }
}

15. 3Sum

16. 3Sum Closest

18. 4Sum

454. 4Sum II

49. Group Anagrams

447. Number of Boomerangs

149. Max Points on a Line

 

查找问题 set 与 map

原文:https://www.cnblogs.com/NeverGiveUp0/p/12862433.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!