给定十万个数据,统计每个数据出现的次数
public class TestDemo2 {
//k是关键字,value时出现的次数
public static Map<Integer,Integer> func1(int[] array){
Map<Integer,Integer> 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 void main(String[] args) {
int[] array = new int[1_0000];
Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(1000);
}
Map<Integer,Integer> map = func1(array);
System.out.println(map);
}
将十万个数据中的数据去重
public static Set<Integer> func2(int[] array){
HashSet<Integer> set = new HashSet<>();
for (int x:array) {
set.add(x);
}
return set;
}
从十万个数据中,找出第一个重复的数据
public static int func3(int[] array) {
HashSet<Integer> set = new HashSet<>();
for (int x : array) {
if (set.contains(x)) {
return x;
}
set.add(x);
}
return -1;
}
只出现一次的数字
//只出现一次的数字
public int singleNumber(int[] nums){
HashSet<Integer> set = new HashSet<>();
for (int x:nums) {
if(set.contains(x)){
set.remove(x);
}else{
set.add(x);
}
}
for (int i = 0; i < nums.length; i++) {
if (set.contains(nums[i])) {
return nums[i];
}
}
return -1;
}
复制带随机指针的链表
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cKA4tDb9-1661132103682)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1646100793838.png)]
public Node copyRandomList(Node head) {
Map<Node,Node> 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);
}
public int numJewelsInStones(String jewels,String stones){
HashSet<Character> set = new HashSet<>();
for (Character ch:stones.toCharArray()) {
set.add(ch);
}
int count = 0;
for (Character ch: stones.toCharArray()) {
if(set.contains(ch)){
count++;
}
}
return count;
}
旧键盘 https://www.nowcoder.com/questionTerminal/f88dafac00c8431fa363cd85a37c2d5e
//坏掉的键盘
public static void func(String strExce,String strActual) {
HashSet<Character> set = new HashSet<>();
for (char ch: strActual.toUpperCase().toCharArray()) {
set.add(ch);
}
HashSet<Character> broken = new HashSet<>();
for (char ch:strExce.toUpperCase().toCharArray()) {
if(!set.contains(ch) && !broken.contains(ch)){
System.out.println(ch);
broken.add(ch);
}
}
}
public static void main(String[] args) {
func("7_This_is_a_test","_hs_s_a_es");
}
前K个高频单词
/**
* 求的是出现次数最多的单词
* @param words
* @param k
* @return
*/
public static List<String> topKFrequent(String[] words, int k) {
HashMap<String,Integer> map = new HashMap<>();
//1、统计每个单词出现的次数 map
for (String s : words) {
if(map.get(s) == null) {
map.put(s,1);
}else {
int val = map.get(s);
map.put(s,val+1);
}
}
//2、建立一个大小为K的小根堆
PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(k, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
if(o1.getValue().compareTo(o2.getValue()) == 0) {
return o2.getKey().compareTo(o1.getKey());
}
return o1.getValue()-o2.getValue();
}
});
//3、遍历Map
for (Map.Entry<String,Integer> entry : map.entrySet()) {
if(minHeap.size() < k) {
minHeap.offer(entry);
}else {
//说明堆中 已经放满了K个元素,需要看堆顶元素的数据 和当前的数据的大小关系
Map.Entry<String,Integer> top = minHeap.peek();
//判断频率是否相同,如果相同,比较单词的大小,单词小 的入堆
if(top.getValue().compareTo(entry.getValue()) == 0) {
if(top.getKey().compareTo(entry.getKey()) > 0) {
minHeap.poll();
minHeap.offer(entry);
}
}else {
if(top.getValue().compareTo(entry.getValue()) < 0) {
minHeap.poll();
minHeap.offer(entry);
}
}
}
}
//System.out.println(minHeap);
List<String> ret = new ArrayList<>();
for (int i = 0;i < k;i++) {
Map.Entry<String,Integer> top = minHeap.poll();
ret.add(top.getKey());
}
Collections.reverse(ret);
return ret;
}
public static void main(String[] args) {
//String[] worlds = {"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"};
String[] worlds = {"i", "love", "leetcode", "i", "love", "coding"};
List<String> ret = topKFrequent(worlds,3);
System.out.println(ret);
}
import org.omg.CORBA.PUBLIC_MEMBER;
public class HashBuck {
static class Node{
public int key;
public int val;
public Node next;
public Node(int key,int val){
this.key = key;
this.val = val;
}
}
public Node[] array;
public int usedSize;
private static final double DEFAULT_LOAD_FACTOR = 0.75;
public HashBuck(){
this.array = new Node[10];
}
public void put(int key,int val){
//1.找到key所在的位置
int index = key % this.array.length;
//2.遍历这个下标的列表,看看是不是有相同的key,有,更新val的值
Node cur = array[index];
while (cur != null) {
if (cur.key == key) {
cur.val = val;//更新val的值
return;
}
cur = cur.next;
}
//3.没有key这个元素,头插法
Node node = new Node(key,val);
node.next = array[index];
array[index] = node;
this.usedSize++;
//4.插入元素成功后,检查当前散列表的负载因子
if(loadFactor() >= DEFAULT_LOAD_FACTOR){
reszie();//
}
}
public double loadFactor(){
return 1.0*usedSize/array.length;
}
public void reszie(){
Node[] newArray = new Node[array.length*2];
for (int i = 0; i < array.length; i++) {
Node cur = array[i];
while (cur != null) {
int index = cur.key %newArray.length;//获取新的下表11
//就是吧cur 这个节点,头插/尾插的形式 插入到新的数组对应的下标的列表当中
Node curNext = cur.next;
cur.next = newArray[index];//先绑定后面的
newArray[index] = cur;//绑定前面
cur = curNext;
}
}
array = newArray;
}
public int get(int key){
//1.找到key所在的位置
int index = key % this.array.length;
//2.遍历这个下标的列表,看看是不是有相同的key,有,更新val的值
Node cur = array[index];
while (cur != null) {
if (cur.key == key) {
return cur.val;
}
cur = cur.next;
}
return -1;
}
public static void main(String[] args) {
HashBuck hashBuck = new HashBuck();
hashBuck.put(1,1);
hashBuck.put(2,2);
hashBuck.put(12,12);
hashBuck.put(3,3);
hashBuck.put(6,6);
hashBuck.put(7,7);
hashBuck.put(11,11);
hashBuck.put(8,8);
System.out.println(hashBuck.get(11));
}
}