当前位置:网站首页>【Map与Set】之LeetCode&牛客练习
【Map与Set】之LeetCode&牛客练习
2022-07-31 01:31:00 【快到锅里来呀】
目录
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) {
//注意不能使用treemap不然不能比较
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<>();
//把宝石放进集合中
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);
}
//遍历希望输入的,转为大写,再转数组,和实际输入进行比较
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();//希望输入的
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) {
//单词出现次数相同时,调整以key为准的大根堆
if(o1.getValue().compareTo(o2.getValue()) == 0) {
return o2.getKey().compareTo(o1.getKey());
}
return o1.getValue().compareTo(o2.getValue());
//根据单词出现的次数创建小根堆
}
});
//3.遍历map,先给小根堆放满k个,然后从第k+1开始,和堆顶元素比较
for(Map.Entry<String,Integer> entry : map.entrySet()) {
if(minQ.size() < k) {
//如果小根堆没有放满,就继续放
minQ.offer(entry);
}else {
//如果已经放满了小根堆,那么此时就要让当前元素和堆顶元素进行比较
Map.Entry<String,Integer> top = minQ.peek();
if(top != null) {
if(entry.getValue() > top.getValue()) {
minQ.poll();
minQ.offer(entry);
}else {
//如果此时<不用管,但如果=,就要根据题意按字典顺序排
if(top.getValue().compareTo(entry.getValue()) == 0) {
//value一样,看key
if(top.getKey().compareTo(entry.getKey()) > 0) {
minQ.poll();
minQ.offer(entry);
}
}
}
}
}
}
//此时前k个次数最多的单词,已经全部放入堆中了,将堆中元素出堆
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());
}
}
//前面是小根堆出堆的,根据次数高到低的要求,这就要逆置
Collections.reverse(ret);
return ret;
}
}
边栏推荐
- 网站频繁出现mysql等数据库连接失败等信息解决办法
- Centos 7.9 install PostgreSQL14.4 steps
- Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
- The sword refers to offer17---print the n digits from 1 to the largest
- 深度学习可以求解特定函数的参数么?
- 孩子的编程启蒙好伙伴,自己动手打造小世界,长毛象教育AI百变编程积木套件上手
- Yolov7实战,实现网页端的实时目标检测
- Jetpack Compose学习(8)——State及remeber
- Word 表格跨页,仍然显示标题
- "Actual Combat" based on part-of-speech extraction in the field of e-commerce and its decision tree model modeling
猜你喜欢
Shell变量与赋值、变量运算、特殊变量
RTL8720DN开发笔记一 环境搭建与mqtt实例
1782. Count the number of point pairs Double pointer
297. 二叉树的序列化与反序列化
斩获BAT、TMD技术专家Offer,我都经历了什么?
ShardingSphere's vertical sub-database sub-table actual combat (5)
MySQL (6)
Chi-square distribution of digital image steganography
【网络安全】文件上传靶场通关(1-11关)
Ticmp - 更快的让应用从 MySQL 迁移到 TiDB
随机推荐
使用PageHelper实现分页查询(详细)
Xiaohei's leetcode journey: 104. The maximum depth of a binary tree
TiDB 操作实践 -- 备份与恢复
Artificial Intelligence and Cloud Security
计算S=a+aa+…+aa…a
Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
爬虫文本数据清洗
《实战》基于情感词典的文本情感分析与LDA主题分析
一万了解 Gateway 知识点
MySQL的存储过程
"Actual Combat" based on part-of-speech extraction in the field of e-commerce and its decision tree model modeling
Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
MySql data recovery method personal summary
想要写出好的测试用例,先要学会测试设计
JPEG Steganalysis of Digital Image Steganography
打印任务排序 js od华为
Yolov7实战,实现网页端的实时目标检测
tkinter模块高级操作(二)—— 界面切换效果、立体阴影字效果及gif动图的实现
link与@import的区别
Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch