当前位置:网站首页>Leetcode402 remove K digits
Leetcode402 remove K digits
2022-07-24 19:15:00 【Coding~Man】
Title Description : Give you a nonnegative integer expressed as a string num And an integer k , Remove the k Digit number , Keep the remaining numbers to a minimum . Please return the smallest number as a string .
Example :
Input :num = “1432219”, k = 3
Output :“1219”
explain : Remove three numbers 4, 3, and 2 Form a new minimum number 1219 .
analysis :
When the... Of the string i The number corresponding to bit is greater than i+1 Number corresponding to bit , We delete No i The number of digits is reserved for i+1 The number of . Use the heap to constantly store the obtained numbers , This ensures that the heap maintains an increasing order from the bottom of the heap to the top of the heap , Then we will get the newly acquired number i Compare the number corresponding to bit with the number on the top of the heap , Make sure the top of the heap is a small number . Only to delete k A digital , Then loop all the remaining numbers into the queue . Then create a dynamic string , Get each string from the bottom of the heap and transfer it to the dynamic string .
Knowledge point :
1: Create a heap with a value of characters
Deque<Character> deque = new LinkedList<Character>();
2: Get the character corresponding to the string
char digit = num.charAt(i);
3: Determine whether the heap is empty
deque.isEmpty()
4: Add or remove elements to the heap
deque.offerLast(digit);
deque.pollLast();
deque.pollFirst();
deque.peekLast()
Get the top element , But don't delete
5: Create a dynamic string
StringBuilder ret = new StringBuilder();
Add an element to a string
ret.append(digit);
边栏推荐
- Principle and application of database
- 【JVM学习03】类加载与字节码技术
- [question 39] special question for Niuke in-depth learning
- Cyberpanel free open source panel - high speed lscache free SSL Certificate - self built DNS and enterprise post office
- Ebpf verifier
- OPENGL学习(三)GLUT二维图像绘制
- OPENGL学习(二)OPENGL渲染管线
- 2022杭电多校第二场1009 ShuanQ(数学)
- Clion configuring WSL tool chain
- TCL programming style guide
猜你喜欢

Crazy God redis notes 11

Techempower web framework performance test 21st round results release --asp Net core continue to move forward

2022杭电多校第二场1009 ShuanQ(数学)

matplotlib

Today's sleep quality record 79 points

2022 Hangdian multi school second session 1009 shuangq (Mathematics)
思源笔记 v2.1.2 同步问题

This visual analysis library makes it easy for you to play with data science!

Emergency lighting design of large stadiums and gymnasiums

OPENGL学习(四)GLUT三维图像绘制
随机推荐
Modelarts, Pangu big model, ModelBox... Detailed explanation of Huawei cloud AI development production line
Crazy God redis notes 11
Equals() method of object class
Mysql database, de duplication, connection
Add column by column selection for JTable
32-bit stack overflow advanced
New stage of investment
2022 Summer Games of Hangzhou electric power multiple schools 1012aalice and Bob (game theory)
[laser principle and application -6]:q switching element and Q drive circuit board
Data model subclassing reference
Easily learn pytoch transfer learning to realize surface defect inspection
MySQL sort. Sort by field value
[Huawei lyevk-3861a intelligent IOT development board evaluation] unpacking experience and Hisilicon hi3861v100 chip learning experience
为什么梯度是函数变化最快的方向
FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 2)
Reading notes of XXL job source code
First knowledge database
Oneinstack installation and configuration PHP 8.1 and MySQL 8.0-oneinstack site building novice tutorial
Arrays
Math