当前位置:网站首页>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);
边栏推荐
- PostgreSQL Elementary / intermediate / advanced certification examination (7.16) passed the candidates' publicity
- [question 39] special question for Niuke in-depth learning
- Unity code imports packages through package manager
- Calendar common methods
- Zip compression and decompression
- Zooinspector Download
- 多线程与并发编程常见问题(未完待续)
- 【无标题】
- [laser principle and application -6]:q switching element and Q drive circuit board
- Nacos简介和控制台服务安装
猜你喜欢

C Programming classic tutorial
![[JVM learning 03] class loading and bytecode Technology](/img/b4/9482b02b58580171235fd4c36129e9.png)
[JVM learning 03] class loading and bytecode Technology
![[question 39] special question for Niuke in-depth learning](/img/18/0e182f2c003ff5dd8ed3751c718d73.png)
[question 39] special question for Niuke in-depth learning

Sequences, time series and prediction in tessorflow quizs on coursera (II)

matplotlib

OPENGL学习(二)OPENGL渲染管线

多线程与并发编程常见问题(未完待续)
![[laser principle and application -6]:q switching element and Q drive circuit board](/img/30/e199b73fb9b0ad335f26f2378cfc45.png)
[laser principle and application -6]:q switching element and Q drive circuit board

Clion configuring WSL tool chain

Summer Niuke multi school 1:i chiitoitsu (expectation DP, inverse yuan)
随机推荐
JVM method call
Pam4 popular science
Principle and application of database
On dynamic application of binary array
轻松学Pytorch-迁移学习实现表面缺陷检查
What are the benefits of knowledge management in enterprises?
Implement a proxy pool from 0
Add column by column selection for JTable
Tcl/tk file operation
Zooinspector Download
mysql排序.按字段值排序
[Huawei lyevk-3861a intelligent IOT development board evaluation] unpacking experience and Hisilicon hi3861v100 chip learning experience
OpenGL learning (IV) glut 3D image rendering
PostgreSQL weekly news - July 13, 2022
JS part
[resolved] CVC datatype valid. 1.2.1: '' is not a valid value for 'ncname'.
[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
Convolutional Neural Networks in TensorFlow quizs on Coursera
Biopharmaceutical safety, power supply and production guarantee
Nezha monitoring - server status monitoring, SSL certificate change expiration, Ping monitoring and scheduled task reminder