当前位置:网站首页>leetcode---技巧
leetcode---技巧
2022-07-29 05:24:00 【lalajh】
1.java程序获取控制台输入的方法
import java.util.Scanner;//先导包
Scanner sc = new Scanner(System.in);
String str = sc.next();//获取的输入为String类型
int a1 = sc.nextInt();//获取的输入为int类型
double a2= sc.nextDouble();//获取的输入为double类型
2.Java建立一个栈
Stack.peek()和Stack.pop()的区别
Stack<TreeNode> stack = new Stack<>(); //
1. Stack.peek()
peek()函数返回栈顶的元素,但不弹出该栈顶元素。
2. Stack.pop()
pop()函数返回栈顶的元素,并且将该栈顶元素出栈。3.Java建立一个队列
使用:Deque接口
Deque<Integer> test = new LinkedList<>();
int currentLevelSize = queue.size(); // 队列长度public class Main {
public static void main(String[] args) {
//add()和remove()方法在失败的时候会抛出异常(不推荐)
Queue<String> queue = new LinkedList<String>();
//添加元素
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()); //返回第一个元素,并在队列中删除
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("element="+queue.element()); //返回第一个元素
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("peek="+queue.peek()); //返回第一个元素
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是一个双端队列接口,继承自Queue接口,Deque的实现类是LinkedList、ArrayDeque、LinkedBlockingDeque,其中LinkedList是最常用的。
4.LinkedList用法
参考:
LinkedList用法详解_韩家老大的博客-CSDN博客_linkedlist
5.ArrayList用法
3-1.ArrayList的添加方法
方法1:依次按照顺序向ArrayList中添加数据。
用法:将a添加到list中,list.add("a");

3-2 ArrayList的删除方法
方法1:按照位置删除单个数据
用法:将list中第2个数据删除
list.remove(2);
注意:位置从0开始计算(0、1、2、3...)

方法2:按照内容删除单个数据
用法:
将list中的数据"d"删除
list.remove("d");

注意:对于int,String,char这样的原始类型数据是可以删除的,但是对于复杂对象,例如自己编写的User类、Person类对象,需要重写equals方法,负责remove方法无法匹配删除。
参考:
Java的ArrayList用法_Fighting_初心的博客-CSDN博客_arraylist用法
中序和后序序列,建立树

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("张三", 18);
map.put("李四", 28);
map.put("王五", 38);
map.put("赵六", 48);
//1.获取map集合的entry 集合
Set<Map.Entry<String, Integer>> entries = map.entrySet();
//2.用迭代器 遍历这个entries这个Set集合
Iterator<Map.Entry<String, Integer>> it = entries.iterator();
//3.遍历
while(it.hasNext()){
Map.Entry<String, Integer> entry = it.next();
//一个entry中 有两个属性
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"="+value);
} public class MapDemo {
public static void main(String[] args) {
//定义一个Map集合 存储键为学生(姓名,年龄)值为学生自己的家庭住址。
Map<Student, String> map = new HashMap<Student, String>();
//向集合添加数据
map.put(new Student("王宝强", 40), "北京五环外");
map.put(new Student("谢霆锋", 50), "北京180环外");
map.put(new Student("马蓉", 45), "上海交通路");
map.put(new Student("郭德纲", 55), "广州德云社");
map.put(new Student("马蓉", 45), "我家");
//map判断键重复不重复,是通过hashCode和equals方法
//如果 我要求一个学生的姓名和年龄一样 就认为是同一个学生
//遍历集合 keySet entrySet
//1.获取entry的集合
Set<Map.Entry<Student, String>> entries = map.entrySet();
//2.迭代器遍历 foreach
for (Map.Entry<Student, String> entry : entries) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"="+value);
}
}
}边栏推荐
- SimpleFOC调参3-PID参数整定攻略
- Design and implementation of QT learning notes data management system
- 低成本2.4GHz 无线收发芯片--Ci24R1
- JUC collection class is unsafe
- 数学建模心得
- 基于F407ZGT6的WS2812B彩灯驱动
- Install MySQL from scratch (MySQL installation document - unzipped version)
- 【软件工程之美 - 专栏笔记】28 | 软件工程师的核心竞争力是什么?(下)
- 多线程和并发
- SimpleFOC+PlatformIO踩坑之路
猜你喜欢

【软件工程之美 - 专栏笔记】26 | 持续交付:如何做到随时发布新版本到生产环境?

【软件工程之美 - 专栏笔记】22 | 如何为项目做好技术选型?

【软件工程之美 - 专栏笔记】14 | 项目管理工具:一切管理问题,都应思考能否通过工具解决

LeetCode #189.轮转数组

CV520国产替代Ci521 13.56MHz 非接触式读写器芯片

STM32 printf问题总结 semihosting microLIB理解

scanBasePackages扫包范围配置

Based on stc51: schematic diagram and source code of four axis flight control open source project (entry-level DIY)

Huawei cloud 14 day Hongmeng device development -day7wifi function development

Power electronics: single inverter design (matlab program +ad schematic diagram)
随机推荐
循环链表和双向链表
scanBasePackages扫包范围配置
EPS32+Platform+Arduino 跑马灯
太原市公交路线爬取
进程与进程的概念
Jingwei Qili: development of heart rate and blood oxygen module based on hmep060 (1: FPGA sends multi bit instructions)
网络爬虫
低成本2.4GHz 无线收发芯片--Ci24R1
FPGA based: moving target detection (schematic + source code + hardware selection, available)
#6898 变幻的矩阵 题解
【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
CS4344国产替代DP4344 192K 双通道 24 位 DA 转换器
唯美girls
2022暑初二信息竞赛学习成果分享1
【软件工程之美 - 专栏笔记】26 | 持续交付:如何做到随时发布新版本到生产环境?
【软件工程之美 - 专栏笔记】24 | 技术债务:是继续修修补补凑合着用,还是推翻重来?
【软件工程之美 - 专栏笔记】“一问一答”第2期 | 30个软件开发常见问题解决策略
Ml self study notes 5
LeetCode #7.整数反转
基于51单片机ADC0808的proteus仿真