当前位置:网站首页>Click "Niuke | daily question" to eliminate it
Click "Niuke | daily question" to eliminate it
2022-07-26 06:40:00 【Starry Luli】
Author's brief introduction : One likes to write , Sophomore rookie of planning major
Personal home page :starry Lu Li
First date :2022 year 7 month 25 Monday, Sunday
Last article :『 Cattle guest | A daily topic 』 Pressure into the stack 、 Pop-up sequence _
Subscription column :『 Collection of Niuke brush questions 』
If the article helps you, remember to praise + Collect and support

『 Cattle guest | A daily topic 』 Click to eliminate
1. A daily topic
Original link : Portal -》 Poke me

2. Their thinking
2.1 Thought analysis
Because the adjacent and same characters are eliminated every time , So we consider using stack : Traversal string , Every time you encounter the same character as the top of the stack, the top of the stack element is out of the stack , The stack is empty or the current character is different from the top element , It into the stack
- step 1: Introducing auxiliary stack stk, Traversal string , Every time Encountered the same character as the top of the stack Stack the top element
- step 2: The stack is empty or the current character is different from the top element , Stack the current element
- step 3: Output results , Stack empty description is finally empty string , Direct output 0
- step 4: If It's not empty string Just introduce a stack to reverse the character output
2.2 Core code
import java.util.Scanner;
import java.util.Stack;
public class Main5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get input
String s=scanner.next();
// Auxiliary stack
Stack<Character>stk=new Stack<Character>();
// Traversal string , If the current character is not equal to the top of the stack, it will be put on the stack , If it is equal, the element at the top of the stack is out of the stack
for(int i=0;i<s.length();++i) {
char a=s.charAt(i);
if(stk.isEmpty()||a!=stk.peek()) {
stk.push(a);
}else if(a==stk.peek()) {
stk.pop();
}
}
// Output processing
// Stack empty description is finally empty string , Output 0
if(stk.isEmpty()) {
System.out.println("0");
}else {
// If it is not an empty string, then introduce a stack to reverse the character output
Stack<Character>stk2=new Stack<Character>();
while(!stk.isEmpty()) {
stk2.push(stk.pop());
}
while(!stk2.isEmpty()) {
System.out.print(stk2.pop());
}
}
scanner.close();
}
}
2.3 Train of thought expansion
This question uses java The stack in the collection class actually brings inconvenience to our output , When outputting elements, you also need to introduce an auxiliary stack to reverse the elements . A more concise approach is to be direct use StringBuffer String concatenation , Delete the string to simulate stacking , Out of the stack . The idea is the same .
/** * @author starry */
import java.util.Scanner;
public class Main51 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s=scanner.next();
int len=s.length();
// use StringBuffer String concatenation , Delete the string to simulate stacking , Out of the stack
StringBuffer sbf=new StringBuffer(len);
//index Express “ To the top of the stack ” Element subscript . Initial stack empty , Subscript to be -1
int index=-1;
// Traversal string
for(int i=0;i<len;++i) {
// The current character
char ch=s.charAt(i);
//“ Stack ” Empty or the current element and “ To the top of the stack ” Elements are not equal , The stack , also “ To the top of the stack ” Subscript ++
if(index==-1||ch!=sbf.charAt(index)) {
sbf.append(ch);
index++;
}else {
// Current element and “ To the top of the stack ” The elements are equal ,“ To the top of the stack ” Element out of stack , also “ To the top of the stack ” Subscript minus 1
sbf.deleteCharAt(index);
index--;
}
}
if(index==-1) {
System.out.println("0");
}else {
System.out.println(sbf);
}
scanner.close();
//ahoy-starry
}
}

Subscription column :『 Collection of Niuke brush questions 』
If the article helps you, remember to praise + Collect and support
边栏推荐
- Downloadutilse tool class without error
- Liberg avenue to Jane series
- 【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码
- Facing the rebound market, how do we operate? 2020-03-21
- Force deduction 5: Longest palindrome substring
- Can C use reflection to assign values to read-only attributes?
- 少儿编程 电子学会图形化编程等级考试Scratch一级真题解析(选择题)2022年6月
- [fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code
- English sentence pattern reference exclusive Edition - adverbial clause
- The real epidemic situation in the United States, do not easily "bottom" 2020-03-23
猜你喜欢

信号处理系统综合设计-求解器函数的设计(连续和离散时间系统)

『牛客|每日一题』有效括号序列

How to solve the crash when the easygbs platform edits the device management group?

力扣——4. 寻找两个正序数组的中位数

7. Reverse Integer整数反转

Force buckle - 3. Longest substring without repeated characters

『期末复习』16/32位微处理器(8086)基本寄存器

力扣5: 最长回文子串

少儿编程 电子学会图形化编程等级考试Scratch一级真题解析(选择题)2022年6月

BPG笔记(四)
随机推荐
Servlet cannot directly obtain JSON format data in request request
Yolov6: the fast and accurate target detection framework is open source
Markdown add Emoji expression
openssl: error while loading shared libraries: libssl.so.1.1
Display Chinese characters in uitoolkit
[C language] address book dynamic version and document version
FastDFS-支持双IP、IPV6
Esxi 7.0 installation supports mellanox technologies mt26448 [connectx en 10gige, PCIe 2.0 5gt/s] driver, and supports the cheapest 10GB dual fiber network card
原生高性能抓包工具Proxyman,送给爱学习的你
【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码
TCP protocol -- message format, connection establishment, reliable transmission, congestion control
Summary of common usage of dev treelist
Alibaba cloud OSS binding custom domain name
Go 的通道channel
【保姆级】包体积优化教程
[Hangzhou][15k-20k] medical diagnosis company recruits golang development engineers without overtime! No overtime! No overtime!
The "darkest hour" has not come yet. Cherish every bullet 2020-03-22
Integrated design of signal processing systems - Design of solver functions (continuous and discrete time systems)
Nuxt configuration topic switching
Overview of image classification of vision transformer must read series
