当前位置:网站首页>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
e
Deque是一个双端队列接口,继承自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);
}
}
}
边栏推荐
猜你喜欢
Jingwei Qili: development of heart rate and blood oxygen module based on hmep060 (1: FPGA sends multi bit instructions)
基于51单片机的直流电机调速系统(L298的使用)
一些工具,插件,软件链接分享给大家~
Add time series index to two-dimensional table
Open source based on STM32: MHD Bluetooth speaker (including source code +pcb)
Ml9 self study notes
【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
LeetCode #9.回文数
LeetCode #876.链表的中间结点
markdown与Typora
随机推荐
STM8S003国产替代 DP32G003 32 位微控制器芯片
传统模型预测控制轨迹跟踪——圆形轨迹(功能包已经更新)
寒假集训总结 (1.23~1.28) [第一梯队]
多线程和并发
Based on STM32: couple interactive doll (design scheme + source code +3d drawing +ad circuit)
【软件工程之美 - 专栏笔记】24 | 技术债务:是继续修修补补凑合着用,还是推翻重来?
单链表面试题
LeetCode #19.删除链表的倒数第N个结点
CS4344国产替代DP4344 192K 双通道 24 位 DA 转换器
FPGA based: moving target detection (supplementary simulation results, available)
STM32 printf问题总结 semihosting microLIB理解
八大排序----------------冒泡排序
IDEA安装scala
Ml8 self study notes
【软件工程之美 - 专栏笔记】29 | 自动化测试:如何把Bug杀死在摇篮里?
CV520国产替代Ci521 13.56MHz 非接触式读写器芯片
【RoboMaster】A板接收JY-ME01角度传感器数据--modebus协议&CRC软件校验
Hal library learning notes-14 ADC and DAC
Huawei cloud 14 days Hongmeng device development -day1 environment construction
低功耗蓝牙5.0芯片nrf52832-QFAA