当前位置:网站首页>Use of push() and pop()
Use of push() and pop()
2022-04-23 01:51:00 【Tan.] der】
1、push What is it? ?( advance )
push Just push , Extend to push . This is an instruction of assembly ,( It may also be found in other languages ). The meaning is almost the same , Is to put an element on the stack . You can imagine that the stack is a box for CDs , There is a bottom but no cover , First push When you go in, you always have to wait push You can't come out until you come out .
Into the stack push()
Push : At the top of the stack ( The tail of the array ) Add specified elements , And return the length of the new array .
2、pop What is it? ?( eject )
pop and push contrary , It means pop up , Is to pop up an element from the stack , Every time it pops up, it's the top of the stack ( The top of the CD case , That is, the last one put in ) That element of .
Out of the stack pop()
Out of the stack : Delete the top of the stack ( The tail of the array ) An element of , And return the deleted element .
3. Example
requirement : Given one only includes ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ String s , Determines whether the string is valid .
Valid string needs to meet :
- Opening parentheses must be closed with closing parentheses of the same type .
- The left parenthesis must be closed in the correct order
Example 1:
Input :s = "()"
Output :true
Example 2:
Input :s = "()[]{}"
Output :true
Example 3:
Input :s = "([)]"
Output :false
public static boolean isValid(String s) {
int n = s.length();
if (n % 2 == 1) {
return false;
}
Map<Character, Character> pairs = new HashMap<Character, Character>() {
{
put(')', '(');
put(']', '[');
put('}', '{');
}};
Deque<Character> stack = new LinkedList<Character>();
for (int i = 0; i < n; i++) {
char ch = s.charAt(i);
if (pairs.containsKey(ch)) {
if (stack.isEmpty() || stack.peek() != pairs.get(ch)) {
return false;
}
stack.pop();
} else {
stack.push(ch);
}
}
return stack.isEmpty();
}
4. A simple example 2
public static void main(String[] args) {
LinkedList linked=new LinkedList<>();
linked.add(“ tearful ”);
linked.add(“ Ash ”);
linked.add(“ Hey ”);
System.out.println(linked.pop());// Remove the element at the top of the stack and return its value
// return : tearful
linked.push(“ Pig ”);
System.out.println(linked);// Push the object to the top of the stack
//[ Pig , Ash , Hey ]
}
版权声明
本文为[Tan.] der]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220909240547.html
边栏推荐
- Server 2019 the available memory of the server is half of the actual memory
- EBS:PO_EMPLOYEE_HIERARCHIES_ALL
- PID精讲
- What are the test steps of dynamic proxy IP?
- 什么是bgp服务器,有哪些优势?
- 角色个人属性英文缩写
- PHP & laravel & master several ways of generating token by API and some precautions (PIT)
- Solve the problem when installing MySQL
- Dimension C China helping farmers in rural areas warms people's hearts the third stop is jiabaoguo farm
- 拨号vps会遇到什么问题?
猜你喜欢
最长公共子序列(记录路径版)
教程】如何用GCC“零汇编”白嫖MDK
Analyze the three functions of static proxy IP.
Is it better to use a physical machine or a virtual machine to build a website?
Performance introduction of the first new version of cdr2022
W801/W800/W806唯一ID/CPUID/FLASHID
W801 / w800 / w806 unique ID / CPUID / flashid
Ziguang micro financial report is outstanding. What does the triple digit growth of net profit in 2021 depend on
When should I write unit tests? What is TDD?
42. Use k3det in mmrotate for rotating target detection, MNN deployment and ncnn deployment
随机推荐
DO447管理用户和团队的访问
J-link v9 使用技巧之虚拟串口功能
PID refinement
代理IP可用率是不是等同于代理IP的效率?
有哪些业务会用到物理服务器?
Technology cloud report: cloud computing has entered the "second half". Where is the way out for domestic cloud?
2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
What business scenarios will the BGP server be used in?
Shardingsphere introduction and sub table usage
[experience tutorial] Alipay balance automatically transferred to the balance of treasure how to set off, cancel Alipay balance automatically transferred to balance treasure?
使用单元测试框架编写单元测试的好处?
什么是布尔类型?
How to classify proxy IP?
K zeros after leetcode factorial function
关于C4D动画如何导入Lumion
How to "gracefully" measure system performance
PHP & laravel & master several ways of generating token by API and some precautions (PIT)
Leetcode 112 Total path (2022.04.22)
Use of j-link RTT
J-Link RTT使用