目录
引言
Map的使用方法
Set说明
用map统计数组中每个数字出现的次数
将数据去重
找出第一个重复出现的数字
宝石与石头
复制带随机指针的链表
只出现一次的数字
Map和Set是适合动态查找的集合容器,Map中存储的就是key-value的键值对,Set中只存储了Key
K getKey() 返回 entry 中的 key V getValue() 返回 entry 中的 value V setValue(V value) 将键值对中的value替换为指定value V get(Object key) 返回 key 对应的 value V getOrDefault(Object key, V defaultValue) 返回 key 对应的 value,key 不存在,返回默认值 V put(K key, V value) 设置 key 对应的 value V remove(Object key) 删除 key 对应的映射关系 SetkeySet() 返回所有 key 的不重复集合 Collection values() 返回所有 value 的可重复集合 Set > entrySet() 返回所有的 key-value 映射关系 boolean containsKey(Object key) 判断是否包含 ke
Set与Map主要的不同有两点:Set是继承自Collection的接口类,Set中只存储了Key,Set是集合,存储的元素是不重复的。
boolean add(E e) 添加元素,但重复元素不会被添加成功 void clear() 清空集合 boolean contains(Object o) 判断 o 是否在集合中 Iteratoriterator() 返回迭代器 boolean remove(Object o) 删除集合中的 o int size() 返回set中元素的个数 boolean isEmpty() 检测set是否为空,空返回true,否则返回false Object[] toArray() 将set中的元素转换为数组返回 boolean containsAll(Collection> c) 集合c中的元素是否在set中全部存在,是返回true,否则返回 false boolean addAll(Collection extends E> c) 将集合c中的元素添加到set中,可以达到去重的效果
public static Map func1(int[] array){Map map=new HashMap<>();//判断array中的元素是否在map中,如果不在就是1//在就在原来的基础上加1;for(int x:array){if(map.get(x)==null){map.put(x,1);}else{int val=map.get(x);map.put(x,val+1);}}return map;}
就直接把数据放在集合中
public static Set
func2(int[] array){HashSet set=new HashSet<>();for(int x:array){set.add(x);}return set;}
//从10万个数据中找到第一个重复的数据//每次把元素放在set里,放之前都检查一下,set中是不是已经有了public static int func3(int[] array){HashSet set=new HashSet<>();for(int x:array){if(set.contains(x)){return x;}set.add(x);}return -1;}
给你一个字符串 jewels 代表石头中宝石的类型,另有一个字符串 stones 代表你拥有的石头。 stones 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
字母区分大小写,因此 "a" 和 "A" 是不同类型的石头。
示例 1:
输入:jewels = "aA", stones = "aAAbbbb"
输出:3class Solution {public int numJewelsInStones(String jewels, String stones) {//只要把宝石放在集合中就可以了;HashSet
set=new HashSet<>();for(Character ch:jewels.toCharArray()){set.add(ch);}int count=0;for(Character ch:stones.toCharArray()){if(set.contains(ch)){count++;}}return count;} }
给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。
构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。
例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。
返回复制链表的头节点。
用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:
val:一个表示 Node.val 的整数。
random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为 null 。
你的代码 只 接受原链表的头节点 head 作为传入参数。来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/copy-list-with-random-pointer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
//map通过key可以找到value;
//key中存储旧节点
//value中存储新节点
//步骤:
//第一次遍历链表,存储老节点与新结点之间的映射关系;
//第二次修改链表,修改新的next和random
//return map.get(head);
/* // Definition for a Node. class Node {int val;Node next;Node random;public Node(int val) {this.val = val;this.next = null;this.random = null;} } *///map.get(cur).next=map.get(cur.next); //map.get(cur).random=map.get(cur.random); class Solution {public Node copyRandomList(Node head) {HashMap
map=new HashMap<>();Node cur=head;while(cur!=null){Node node=new Node(cur.val);map.put(cur,node);cur=cur.next;}cur=head;while(cur!=null){map.get(cur).next=map.get(cur.next);map.get(cur).random=map.get(cur.random);cur=cur.next;}return map.get(head); }}
//如果集合中没有这个元素,就把这个元素放入集合中,如果有这个元素,就把这个元素从集合中删除,最终剩下的元素就是要找的元素
class Solution {public int singleNumber(int[] nums) {HashSet set=new HashSet<>();for(int x:nums){if(!set.contains(x)){set.add(x);}else{set.remove(x);}}//return set.;// for(int i=0;i