当前位置:网站首页>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);
边栏推荐
- Biopharmaceutical safety, power supply and production guarantee
- Summary of articles in 2020
- 2022杭电多校第二场1009 ShuanQ(数学)
- Ensure the health and safety of front-line construction personnel, and implement wrong time construction at Shenzhen construction site
- [JVM learning 03] class loading and bytecode Technology
- [understanding of opportunity-48]: Guiguzi - Chapter 7 - collect information in advance, make predictions and implementation plans in advance
- [Huawei lyevk-3861a intelligent IOT development board evaluation] unpacking experience and Hisilicon hi3861v100 chip learning experience
- [in depth study of 4g/5g/6g topic -39]: urllc-10 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -3- how to distinguish urllc services? Detailed expl
- Colon sorting code implementation
- Profile environment switching
猜你喜欢

FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 1)

PostgreSQL Elementary / intermediate / advanced certification examination (7.16) passed the candidates' publicity

暑期牛客多校1: I Chiitoitsu(期望dp,求逆元)

卷积神经网络感受野计算指南

matplotlib

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

Tencent Browser service TBS usage

Emergency lighting design of large stadiums and gymnasiums

Nacos introduction and console service installation

Clion configuring WSL tool chain
随机推荐
知乎上的那些神回复
C Programming classic tutorial
mysql排序.按字段值排序
Principle and application of database
Those gods on Zhihu reply
FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 2)
High speed ASIC packaging trends: integration, SKU and 25g+
[Huawei lyevk-3861a intelligent IOT development board evaluation] unpacking experience and Hisilicon hi3861v100 chip learning experience
OpenGL learning (IV) glut 3D image rendering
杭电多校第一场第三题 Backpack(异或dp+bitset)
Implement a proxy pool from 0
Converter
Arrays
MySQL hidden version number
BUUCTF-pwn[1]
Traversal and splicing of strings
About core files
MySQL1
Literature reading: gopose 3D human pose estimation using WiFi
PWN learning