当前位置:网站首页>[Map and Set] LeetCode & Niu Ke exercise
[Map and Set] LeetCode & Niu Ke exercise
2022-07-31 01:39:00 【Come to the pot】
目录
1. 只出现一次的数字
题目链接:136. 只出现一次的数字
题目要求:

分析一下:

上代码:
//2.使用集合
//时间复杂度:O(n)
public int singleNumber(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int i = 0; i < nums.length; i++) {
int key = nums[i];
if(!set.contains(key)) {
set.add(nums[i]);
}else {
set.remove(key);
}
}
//此时集合,就剩一个元素了 找到这个元素
for(int i = 0; i < nums.length; i++) {
int key = nums[i];
if(set.contains(key)) {
return nums[i];
}
}
return -1;
}
2. 复制带随机指针的链表
题目链接:138. 复制带随机指针的链表
题目要求:

分析一下:

上代码:
class Solution {
public Node copyRandomList(Node head) {
//注意不能使用treemapOtherwise it can't be compared
Map<Node,Node> map = new HashMap<>();
//1.第一次遍历链表
Node cur = head;
while(cur != null) {
Node node = new Node(cur.val);
map.put(cur,node);
cur = cur.next;
}
//2.第二次遍历链表
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);
}
}
3. 宝石与石头
题目链接:771. 宝石与石头
题目要求:

分析一下:
上代码:
class Solution {
public int numJewelsInStones(String jewels, String stones) {
Set<Character> set = new HashSet<>();
//Put the gems into the collection
for(char ch : jewels.toCharArray()) {
set.add(ch);
}
int count = 0;
//遍历石头,在石头中找宝石,计数++
for(char ch : stones.toCharArray()) {
if(set.contains(ch)) {
count++;
}
}
return count;
}
}
4. 旧键盘
题目链接:旧键盘 (20)__牛客网 (nowcoder.com)
题目要求:

分析一下:

上代码:
import java.util.*;
public class Main {
public static void func(String str1,String str2) {
Set<Character> set = new HashSet<>();
//str2先转大写,再转数组,然后放入集合
for(char ch : str2.toUpperCase().toCharArray()) {
set.add(ch);
}
//Iterate over the desired input,转为大写,再转数组,Compare with actual input
Set<Character> set2 = new HashSet<>();
for(char ch : str1.toUpperCase().toCharArray()) {
if(!set.contains(ch) && !set2.contains(ch)) {
System.out.print(ch);
set2.add(ch);
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()) {
String str1 = scan.nextLine();//wish to enter
String str2 = scan.nextLine();//实际输入的
func(str1,str2);
}
}
}
5. 前k个高频单词
题目链接:692. 前K个高频单词
题目要求:

分析一下:

上代码:
class Solution {
public List<String> topKFrequent(String[] words, int k) {
//1.统计单词出现的次数
Map<String,Integer> map = new HashMap<>();
for(String key : words) {
if(map.get(key) == null) {
map.put(key,1);
}else {
int val = map.get(key);
map.put(key,val+1);
}
}
//2.建立大小为k的小根堆
PriorityQueue<Map.Entry<String,Integer>> minQ = new PriorityQueue<>(k,new Comparator<Map.Entry<String,Integer>>(){
@Override
public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) {
//When the word appears the same number of times,调整以key为准的大根堆
if(o1.getValue().compareTo(o2.getValue()) == 0) {
return o2.getKey().compareTo(o1.getKey());
}
return o1.getValue().compareTo(o2.getValue());
//Create a small root heap based on the number of occurrences of a word
}
});
//3.遍历map,Fill the small roots firstk个,然后从第k+1开始,和堆顶元素比较
for(Map.Entry<String,Integer> entry : map.entrySet()) {
if(minQ.size() < k) {
//If the small root pile is not full,just keep putting it
minQ.offer(entry);
}else {
//如果已经放满了小根堆,Then we need to compare the current element and the top element of the heap at this time
Map.Entry<String,Integer> top = minQ.peek();
if(top != null) {
if(entry.getValue() > top.getValue()) {
minQ.poll();
minQ.offer(entry);
}else {
//如果此时<不用管,但如果=,Arrange them in lexicographical order according to the meaning of the questions
if(top.getValue().compareTo(entry.getValue()) == 0) {
//value一样,看key
if(top.getKey().compareTo(entry.getKey()) > 0) {
minQ.poll();
minQ.offer(entry);
}
}
}
}
}
}
//此时前k个次数最多的单词,All have been put into the heap,Remove the elements from the heap from the heap
List<String> ret = new ArrayList<>();
for(int i = 0; i < k; i++) {
Map.Entry<String,Integer> top = minQ.poll();
if(top != null) {
ret.add(top.getKey());
}
}
//The front is a pile of small roots,According to the number of high to low requirements,This is reversed
Collections.reverse(ret);
return ret;
}
}边栏推荐
- 1782. 统计点对的数目 双指针
- Sping.事务的传播特性
- 221. 最大正方形
- 聚簇索引和非聚簇索引到底有什么区别
- Jetpack Compose学习(8)——State及remeber
- C语言_结构体指针数组函数选票系统
- 手把手教你配置Jenkins自动化邮件通知
- RTL8720DN开发笔记一 环境搭建与mqtt实例
- The difference between 4G communication module CAT1 and CAT4
- MySql installation and configuration super detailed tutorial and simple method of building database and table
猜你喜欢

C language _ structure pointer array function voting system

Distributed. Idempotency

软件测试要达到一个什么水平才能找到一份9K的工作?

221. 最大正方形

数字图像隐写术之JPEG 隐写分析

Mysql:Invalid default value for TIMESTAMP

射频器件的基本参数1

Chi-square distribution of digital image steganography

Parameter introduction and selection points of wireless module

MySQL (6)
随机推荐
分布式.分布式锁
tensorflow与GPU版本对应安装问题
Word 表格跨页,仍然显示标题
验证 XML 文档
【952. Calculate the maximum component size according to the common factor】
最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?
Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
4G通信模块CAT1和CAT4的区别
MySQL (6)
倍增、DFS序
PDF split/merge
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
MySQL stored procedure
第一学年课程期末考试
力扣每日一题-第46天-704. 二分查找
Likou Daily Question - Day 46 - 704. Binary Search
Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
程序员转正述职报告/总结
prometheus 监控概述