当前位置:网站首页>Leetcode - Tips
Leetcode - Tips
2022-07-29 06:26:00 【lalajh】
1.java The method by which the program obtains console input
import java.util.Scanner;// Pilot Kit
Scanner sc = new Scanner(System.in);
String str = sc.next();// The input obtained is String type
int a1 = sc.nextInt();// The input obtained is int type
double a2= sc.nextDouble();// The input obtained is double type
2.Java Build a stack
Stack.peek() and Stack.pop() The difference between
Stack<TreeNode> stack = new Stack<>(); //
1. Stack.peek()
peek() Function returns the element at the top of the stack , But do not pop up the top element of the stack .
2. Stack.pop()
pop() Function returns the element at the top of the stack , And push the top element of the stack out of the stack .3.Java Create a queue
Use :Deque Interface
Deque<Integer> test = new LinkedList<>();
int currentLevelSize = queue.size(); // The queue length public class Main {
public static void main(String[] args) {
//add() and remove() Method throws an exception when it fails ( Not recommended )
Queue<String> queue = new LinkedList<String>();
// Additive elements
queue.offer("a");
queue.offer("b");
queue.offer("c");
queue.offer("d");
queue.offer("e");
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("poll="+queue.poll()); // Returns the first element , And delete... In the queue
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("element="+queue.element()); // Returns the first element
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("peek="+queue.peek()); // Returns the first element
for(String q : queue){
System.out.println(q);
}
}
}a
b
c
d
e
===
poll=a
b
c
d
e
===
element=b
b
c
d
e
===
peek=b
b
c
d
eDeque It's a two terminal queue interface , Inherited from Queue Interface ,Deque The implementation class is LinkedList、ArrayDeque、LinkedBlockingDeque, among LinkedList Is the most commonly used .
4.LinkedList usage
Reference resources :
LinkedList Usage details _ Han family boss's blog -CSDN Blog _linkedlist
5.ArrayList usage
3-1.ArrayList How to add
Method 1: Go to... In order ArrayList Add data to .
usage : take a Add to list in ,list.add("a");

3-2 ArrayList Delete method of
Method 1: Delete individual data by location
usage : take list pass the civil examinations 2 Data deletion
list.remove(2);
Be careful : Location slave 0 Start calculating (0、1、2、3...)

Method 2: Delete single data according to content
usage :
take list Data in "d" Delete
list.remove("d");

Be careful : about int,String,char Such original type data can be deleted , But for complex objects , For example, I wrote it myself User class 、Person Class object , Need to rewrite equals Method , be responsible for remove Method cannot match delete .
Reference resources :
Java Of ArrayList usage _Fighting_ Chuxin's blog -CSDN Blog _arraylist usage
Middle order and post order sequence , Build trees

Map<String, Integer> map = new HashMap<String, Integer>();
map.put(" Zhang San ", 18);
map.put(" Li Si ", 28);
map.put(" Wang Wu ", 38);
map.put(" Zhao Liu ", 48);
//1. obtain map A collection of entry aggregate
Set<Map.Entry<String, Integer>> entries = map.entrySet();
//2. Use iterators Traverse this entries This Set aggregate
Iterator<Map.Entry<String, Integer>> it = entries.iterator();
//3. Traverse
while(it.hasNext()){
Map.Entry<String, Integer> entry = it.next();
// One entry in There are two properties
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"="+value);
} public class MapDemo {
public static void main(String[] args) {
// Define a Map aggregate Store keys for students ( full name , Age ) It's worth the student's own home address .
Map<Student, String> map = new HashMap<Student, String>();
// Add data to the collection
map.put(new Student(" Baoqiang Wang ", 40), " Outside the Fifth Ring Road in Beijing ");
map.put(new Student(" Nicholas Tse ", 50), " Beijing 180 Outside the ring ");
map.put(new Student(" Ma Rong ", 45), " Shanghai Jiaotong road ");
map.put(new Student(" Guo Degang ", 55), " Guangzhou Deyun society ");
map.put(new Student(" Ma Rong ", 45), " My house ");
//map Judge whether the key is repeated or not , It's through hashCode and equals Method
// If I want a student's name and age to be the same I think it's the same student
// Ergodic set keySet entrySet
//1. obtain entry Set
Set<Map.Entry<Student, String>> entries = map.entrySet();
//2. Iterator traversal foreach
for (Map.Entry<Student, String> entry : entries) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"="+value);
}
}
}边栏推荐
猜你喜欢

Redshift 2.6.41 for maya2018 水印去除

Abstract encapsulation inheritance polymorphism

八大排序-----------快速排序

Unity初学3——敌人的移动控制和掉血区域的设置(2d)

Leetcode 19. delete the penultimate node of the linked list

2022暑初二信息竞赛学习成果分享2

【Leetcode刷题】数组1——双指针

LeetCode #283.移动零

Multithreading and concurrency

Official tutorial redshift 03 parameters and general instructions of various GI
随机推荐
Number theory: proof that the maximum number that px+py cannot represent is pq-p-q
Official tutorial redshift 04 rendering parameters
JUC并发知识点
c语言面试准备一(谈谈理解系类)
官方教程 Redshift 06 Opt参数
JUC集合类不安全
Rowkey设计
Operating system interview questions
LeetCode #7.整数反转
[beauty of software engineering - column notes] 14 | project management tools: all management problems should be considered whether they can be solved by tools
UE5 光影基础 阴影全解析 锯齿阴影解决方案 Lumen
官方教程 Redshift 01 基础理论知识和基础特性学习
Leetcode 167. sum of two numbers II - input ordered array
Leetcode notes 452. minimum number of arrows to burst balloons (medium) 452. detonate balloons with the minimum number of arrows (medium)
Redshift还原SP效果 - SP贴图导出设置及贴图导入配置
【Leetcode刷题】数组1——双指针
UE5 landscape 换算 Nanite 转换方式及不支持 配合 Lumen及Lumen开启 Dynamic Mesh 使用方法
官方教程 Redshift 07 Instances and Proxy
Joiner.on和stream().map联合使用技巧
Abstract encapsulation inheritance polymorphism