EasyExcel多线程批量导出数据,动态表头,静态资源访问

1.导入依赖

        <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.1.1</version></dependency>

2.建立实体

@Data
public class ActResultLogVO implements Serializable {private static final long serialVersionUID = 1L;@ExcelProperty(value = "onlineseqid",index = 0)private String onlineseqid;@ExcelProperty(value = "businessid",index = 1)private String businessid;@ExcelProperty(value = "becifno",index = 2)private String becifno;@ExcelProperty(value = "ivisresult",index = 3)private String ivisresult;@ExcelProperty(value = "createdby",index = 4)private String createdby;@ExcelProperty(value = "createddate",index = 5)private LocalDate createddate;@ExcelProperty(value = "updateby",index = 6)private String updateby;@ExcelProperty(value = "updateddate",index = 7)private LocalDate updateddate;@ExcelProperty(value = "risklevel",index = 8)private String risklevel;/*** 分页开始数量*/private Integer start;/***  分页 size*/private Integer size;
}

3.多线程异步导出

	@Async@Transactionalpublic  void exportExcel(String filePath) {//filePath 文件路径ExcelWriter writer = EasyExcel.write(filePath, ActResultLogVO.class).build();// 根据数据读写速度来调整,一般来说读的逻辑复杂,比较慢,如果读比写快,这里设为1int N = 2;// 大小设置为2就可以,作为缓冲BlockingQueue<List<ActResultLogVO>> queue = new ArrayBlockingQueue<>(2);AtomicInteger start = new AtomicInteger(0);AtomicInteger num = new AtomicInteger(0);AtomicInteger sheet = new AtomicInteger(1);// 分页大小可以适当调整int pageSize = 100000;//开启多个线程分页查数据for (int i = 0; i < N; i++) {executorService.submit(() -> {while (true) {//自增int startNum= start.getAndAdd(pageSize);try {long l = System.currentTimeMillis();logger.info("[多线程,分页查询] 线程:{},开始执行查询:startNum :{},时间:{}",Thread.currentThread().getName(),startNum,l);List<ActResultLogVO> list = selectActResultLogVOPage(startNum, pageSize);logger.info("[多线程,分页查询] 线程:{},执行查询完成 startNum :{} 用时:{}",Thread.currentThread().getName(),startNum,System.currentTimeMillis()-l);if (CollectionUtils.isEmpty(list)) {//读到没数据也要放入空集合queue.put(Collections.EMPTY_LIST);break;}queue.put(list);} catch (Exception e) {//异常情况也要放入空集合,防止写线程无法退出循环try {queue.put(Collections.EMPTY_LIST);} catch (InterruptedException ex) {throw new RuntimeException(ex);}throw new RuntimeException(e);}}});}Future<?> submit = executorService.submit(() -> {int count = 0;while (true) {List<ActResultLogVO> list = null;try {list = queue.take();} catch (InterruptedException e) {Thread.interrupted();}if (CollectionUtils.isEmpty(list)) {count++;// 当获取到两次空集合时,说明已经读完if (count == N) {break;}continue;}if(num.get()>=500000){sheet.getAndAdd(1);num.getAndSet(0);}num.getAndAdd(list.size());long l = System.currentTimeMillis();logger.info("[多线程,写入Excel] 线程:{},开始执行写入:sheet :{},num:{}时间:{}",Thread.currentThread().getName(),sheet.get(),num.get(),l);writer.write(list, EasyExcel.writerSheet("Sheet"+sheet).build());logger.info("[多线程,写入Excel] 线程:{},开始执行写入结束:sheet :{},num:{},时间:{}",Thread.currentThread().getName(),sheet.get(),num.get(),System.currentTimeMillis()-l);}writer.finish();});try {// 阻塞等待完成,异步处理也可以去掉这段代码submit.get();} catch (Exception e) {}}

4.动态表头

 	 @Autowired@Qualifier("excelThreadPool")private ExecutorService executorService;@Async@Transactionalpublic void exportExcel(String filePath,LinkedHashMap<String, DynamicExcelData> nameMap) {// 指定写入的文件ExcelWriter writer = EasyExcel.write(filePath).head(ExcelUtils.getHead(nameMap)).build();// 根据数据读写速度来调整,一般来说读的逻辑复杂,比较慢,如果读比写快,这里设为1int N = 2;// 大小设置为2就可以,作为缓冲BlockingQueue<List<Map<String,Object>>> queue = new ArrayBlockingQueue<>(2);AtomicInteger num = new AtomicInteger(0);AtomicInteger sheet = new AtomicInteger(1);// 分页大小可以适当调整int pageSize = ExcelConstants.SELECT_TO_DB_ROWS_MYBATIS;//开启多个线程分页查数据getData(lvnengAllCardBillVo,queue,pageSize);Future<?> submit = executorService.submit(() -> {int count = 0;while (true) {List<Map<String,Object>> list = null;try {list = queue.take();} catch (InterruptedException e) {Thread.interrupted();}if (CollectionUtils.isEmpty(list)) {count++;// 当获取到两次空集合时,说明已经读完if (count == N) {break;}continue;}if(num.get()>=600000){sheet.getAndAdd(1);num.getAndSet(0);}num.getAndAdd(list.size());List<List<String>> dataList = ExcelUtils.getDataList(nameMap, list);writer.write(dataList, EasyExcel.writerSheet("Sheet"+sheet).build());}writer.finish();});try {// 阻塞等待完成,异步处理也可以去掉这段代码submit.get();} catch (Exception e) {}}
public void getData(ActResultLogVO actResultLogVO,BlockingQueue<List<Map<String,Object>>> queue,int pageSize) {// 根据数据读写速度来调整,一般来说读的逻辑复杂,比较慢,如果读比写快,这里设为1int N = 2;AtomicInteger start = new AtomicInteger(0);for (int i = 0; i < N; i++) {executorService.submit(() -> {while (true) {//自增int startNum = start.getAndAdd(pageSize);List<Map<String, Object>> list = new ArrayList<>();try {ActResultLogVO actResultLogVO1 = DeepCopyUtil.deepCopy(actResultLogVO);actResultLogVO1.setStart(startNum);List<Map<String, Object>> list = selectactResultLogVOPage(actResultLogVO1);if (CollectionUtils.isEmpty(list)) {//读到没数据也要放入空集合queue.put(Collections.EMPTY_LIST);break;}queue.put(list);} catch (Exception e) {//异常情况也要放入空集合,防止写线程无法退出循环try {queue.put(Collections.EMPTY_LIST);} catch (InterruptedException ex) {throw new RuntimeException(ex);}throw new RuntimeException(e);}}});}logger.info("查询数据完成");}
@Configuration
public class ThreadPoolConfig {@Bean("excelThreadPool")public  ExecutorService buildExcelThreadPool() {int cpuNum = Runtime.getRuntime().availableProcessors();BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(1000);ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("excel-pool-%d").build();return new ThreadPoolExecutor(10 * cpuNum, 30 * cpuNum,1, TimeUnit.MINUTES, workQueue, threadFactory);}
}
public class ExcelConstants {public static final Integer PER_SHEET_ROW_COUNT = 100*10000;public static final Integer PER_WRITE_ROW_COUNT = 20*10000;public static final Integer SHEET_WRITE_ROW_COUNT = 50*10000;public static final Integer SELECT_TO_DB_ROWS = 10*10000;public static final Integer SELECT_TO_DB_ROWS_MYBATIS = 5*10000;
}
@Data
public class DynamicExcelData {//列名private String name;//默认值private String defaultValue;public DynamicExcelData(String name, String defaultValue) {this.name = name;this.defaultValue = defaultValue;}
}

5,用到的util

5.1 EasyUtils

public class ExcelUtils {public static void dynamicExportByURL(String filePath,LinkedHashMap<String, DynamicExcelData> nameMap,List<Map<String, Object>> list,String sheetName) throws IOException {//首先判断是否有数据,没有就返回if(CollUtil.isEmpty(list)){return;}//这里的map使用LinkedHashMap,实现字段的顺序功能if(nameMap==null){throw new RuntimeException("请填写好映射表数据");}File file = getFile(filePath);dynamicExportByURL(file,nameMap,list,sheetName);}public static void dynamicExport(HttpServletResponse response,LinkedHashMap<String, DynamicExcelData> nameMap,List<Map<String, Object>> list,String sheetName) throws IOException {//首先判断是否有数据,没有就返回if(CollUtil.isEmpty(list)){return;}//这里的map使用LinkedHashMap,实现字段的顺序功能if(nameMap==null){throw new RuntimeException("请填写好映射表数据");}response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");//使用EasyExcel write进行写出EasyExcel.write(response.getOutputStream()).head(getHead(nameMap)).sheet(sheetName).doWrite(getDataList(nameMap,list));}public static ArrayList<List<String>> getHead(LinkedHashMap<String, DynamicExcelData> nameMap) {//获取表头ArrayList<List<String>> head = new ArrayList<>();for (Map.Entry<String, DynamicExcelData> titleMap : nameMap.entrySet()) {DynamicExcelData data = titleMap.getValue();head.add(Collections.singletonList(data.getName()));}return head;}public static List<List<String>> getDataList(LinkedHashMap<String, DynamicExcelData> nameMap, List<Map<String, Object>> list) {//先初始化一下传入int size = list.size();List<List<String>> dataList = new ArrayList<>();for (int i = 0; i < size; i++) {dataList.add(new ArrayList<>());}//数据重组for (int i = 0; i < list.size(); i++) {Map<String, Object> map = list.get(i);List<String> columns = dataList.get(i);for (Map.Entry<String, DynamicExcelData> sortNameEntry : nameMap.entrySet()) {String key = sortNameEntry.getKey();Object value = map.get(key);columns.add(value != null ? String.valueOf(value) : sortNameEntry.getValue().getDefaultValue());}}return dataList;}public static File getFile(String filePath) throws IOException {File file = new File(filePath);if (!file.exists()) {file.createNewFile();}return file;}public static void dynamicExportByURL(File file,LinkedHashMap<String, DynamicExcelData> nameMap,  List date,String sheetName) {long startTime = 0;ExcelWriter excelWriter = null;try {startTime = System.currentTimeMillis();// 获取 sheet 的个数int sheetNum = getSheetNum(date.size());// 获取每个sheet 写入的次数int writeNumPerSheet = getWriteNumPerSheet();// 最后一个 sheet 写入的数量int writeNumLastSheet = date.size() - (sheetNum - 1) * ExcelConstants.PER_SHEET_ROW_COUNT;// 最后一个 sheet 写入的次数int writeNumPerLastSheet = writeNumLastSheet % ExcelConstants.PER_WRITE_ROW_COUNT == 0 ? writeNumLastSheet / ExcelConstants.PER_WRITE_ROW_COUNT : writeNumLastSheet / ExcelConstants.PER_WRITE_ROW_COUNT + 1;// 指定写入的文件excelWriter = EasyExcel.write(file).head(getHead(nameMap)).build();for (int i = 0; i < sheetNum; i++) {String sheet = sheetName + i;WriteSheet writeSheet = EasyExcel.writerSheet(i, sheet).build();int writeNum = i == sheetNum - 1 ? writeNumPerLastSheet : writeNumPerSheet; // 每个sheet 写入的次数int endEndNum = i == sheetNum - 1 ? date.size() : (i + 1) * ExcelConstants.PER_SHEET_ROW_COUNT; // 每个sheet 最后一次写入的最后行数for (int j = 0; j < writeNum; j++) {long l = System.currentTimeMillis();int startNum = i * ExcelConstants.PER_SHEET_ROW_COUNT + j * ExcelConstants.PER_WRITE_ROW_COUNT;int endNum = j == writeNum - 1 ? endEndNum : i * ExcelConstants.PER_SHEET_ROW_COUNT + (j + 1) * ExcelConstants.PER_WRITE_ROW_COUNT;excelWriter.write(getDataList(nameMap,date.subList(startNum, endNum)), writeSheet);}}} catch (Exception e) {} finally {// 需要放入 finally 中if (excelWriter != null) {excelWriter.finish();}}}public static void writeExcel(String filePath, Class clazz, List date,String sheetName) throws Exception{File file = getFile(filePath);long startTime = System.currentTimeMillis();// 获取 sheet 的个数int sheetNum = getSheetNum(date.size());// 获取每个sheet 写入的次数int writeNumPerSheet = getWriteNumPerSheet();// 最后一个 sheet 写入的数量int writeNumLastSheet = date.size() - (sheetNum - 1) * ExcelConstants.PER_SHEET_ROW_COUNT;// 最后一个 sheet 写入的次数int writeNumPerLastSheet = writeNumLastSheet % ExcelConstants.PER_WRITE_ROW_COUNT == 0 ? writeNumLastSheet / ExcelConstants.PER_WRITE_ROW_COUNT : writeNumLastSheet / ExcelConstants.PER_WRITE_ROW_COUNT + 1;// 指定写入的文件ExcelWriter excelWriter = EasyExcel.write(file, clazz).build();for (int i = 0; i < sheetNum; i++) {String sheet = sheetName + i;WriteSheet writeSheet = EasyExcel.writerSheet(i, sheet).build();int writeNum = i == sheetNum - 1 ? writeNumPerLastSheet : writeNumPerSheet; // 每个sheet 写入的次数int endEndNum = i == sheetNum - 1 ? date.size() : (i + 1) * ExcelConstants.PER_SHEET_ROW_COUNT; // 每个sheet 最后一次写入的最后行数for (int j = 0; j < writeNum; j++) {long l = System.currentTimeMillis();int startNum = i * ExcelConstants.PER_SHEET_ROW_COUNT + j * ExcelConstants.PER_WRITE_ROW_COUNT;int endNum = j == writeNum - 1 ? endEndNum : i * ExcelConstants.PER_SHEET_ROW_COUNT + (j + 1) * ExcelConstants.PER_WRITE_ROW_COUNT;excelWriter.write(date.subList(startNum, endNum), writeSheet);}}// 需要放入 finally 中if (excelWriter != null) {excelWriter.finish();}}public static int getSheetNum(int dateSize){return dateSize % ExcelConstants.PER_SHEET_ROW_COUNT == 0 ? (dateSize / ExcelConstants.PER_SHEET_ROW_COUNT) : (dateSize / ExcelConstants.PER_SHEET_ROW_COUNT + 1);}public static int getWriteNumPerSheet(){return ExcelConstants.PER_SHEET_ROW_COUNT % ExcelConstants.PER_WRITE_ROW_COUNT == 0 ? (ExcelConstants.PER_SHEET_ROW_COUNT / ExcelConstants.PER_WRITE_ROW_COUNT) : (ExcelConstants.PER_SHEET_ROW_COUNT / ExcelConstants.PER_WRITE_ROW_COUNT + 1);}public static  List<String> objectToList(Object obj){ArrayList<String> list = new ArrayList<>();//获取obj类中的所有字段Field[] fields = obj.getClass().getDeclaredFields();//遍历所有属性for (Field field : fields) {try {field.setAccessible(true);list.add(field.get(obj).toString());} catch (IllegalAccessException e) {throw new RuntimeException(e);}}return list;}public static Map<String,Object> objectToMap(Object obj){HashMap<String, Object> map = new HashMap<>();//获取obj类中的所有字段Field[] fields = obj.getClass().getDeclaredFields();//遍历所有属性for (Field field : fields) {try {field.setAccessible(true);map.put(field.getName(),field.get(obj));} catch (IllegalAccessException e) {throw new RuntimeException(e);}}return map;}/*** 通过反射方式将头部作为公共部分进行设置*/public static List<List<String>> setTitles(Class clazz) {List<List<String>> titles = new ArrayList<List<String>>();for (Field declaredField : clazz.getDeclaredFields()) {ExcelProperty annotation = declaredField.getAnnotation(ExcelProperty.class);if (null != annotation) {titles.add(Arrays.asList(annotation.value()));}}return titles;}
}

5.2 深拷贝

public class DeepCopyUtil {public static <T extends Serializable> T deepCopy(T object) {try {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);objectOutputStream.writeObject(object);ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);return (T) objectInputStream.readObject();} catch (IOException | ClassNotFoundException e) {throw new RuntimeException(e);}}
}

配置静态资源访问

@Configuration
public class MyStaticConfig extends WebMvcConfigurationSupport {@Value("${download.url}")private String fileDir;@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/uploads/**").addResourceLocations("classpath:/META-INF/resources/", "classpath:/resources/","classpath:/static/", "classpath:/public/","file:"+fileDir);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/582256.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于MySql字段类型的实践总结

当字段为数值类型时应使用无符号UNSIGNED修饰 ALTER TABLE infoMODIFY COLUMN user_id int UNSIGNED NOT NULL; 当字段为varchar类型时应注意是否选择合适的字符集 例如存储一些范围值&#xff0c;数字英文字符时&#xff08;IP、生日、客户端标识等或以“,”分隔的数据&…

Google Chrome 现在会在后台扫描泄露的密码

谷歌表示&#xff0c;Chrome 安全检查功能将在后台运行&#xff0c;检查网络浏览器中保存的密码是否已被泄露。 如果桌面用户正在使用标记为危险的扩展程序&#xff08;从 Chrome Web Store 中删除&#xff09;、最新的 Chrome 版本&#xff0c;或者如果启用安全浏览来阻止 Go…

Dolphinscheduler-3.2.0 离线部署 伪集群模式

Dolphinscheduler-3.2.0(离线)伪集群模式 一、依赖(前置准备工作) 1.JDK&#xff1a;版本要求 JDK(1.8),安装并配置 JAVA_HOME 环境变量,并将其下的 bin 目录追加到PATH 环境变量中; 2.数据库&#xff1a;PostgreSQL(8.2.15) 或者MySQL(5.7),两者任选其一即可,如 MySQL 则需要…

我的NPI项目之行业黑话 -- 电子/机构/软件/认证

因为最近的NPI项目&#xff0c;参加了很多项目的会议&#xff0c;有电子/机构/软件/认证相关的各方面的专业词汇就出现了。这里我将之称为黑话&#xff0c;就是对我&#xff08;纯软件) 来说是黑盒的话。这里简单记录并用于理解。 EE有关&#xff1a; Layout&#xff0c;一直…

基于Qt之QChart 图表(优美的曲线图案例)

## 项目演示 平台:ubuntu18.04 Qt版本:QT5.14.2 源码位置GitCode:https://gitcode.com/m0_45463480/QCharts/tree/main ## QChart 图表 自从 Qt 发布以来,给跨平台的用户带来很多便利。在 Qt5.7 之前,Qt 在开源社区版本里没有 Qt Charts(自带的绘图组件库)。这使得像…

安卓开发学习笔记

Android发展历史 开发工具 Android Studio 安装工具 下载页面&#xff1a;下载 Android Studio 和应用工具 - Android 开发者 | Android Developers 安装步骤 要求代理&#xff0c;关闭就可以 下载SDK 完成后创建新项目 努力学习中~

12-28

#include <iostream>using namespace std; class Person {int *age;string &name; public:void ss(){cout<<"年龄:"<<*age<<endl<<"名字:"<<name<<endl;}//拷贝构造函数(深拷贝&#xff09;Person(const Pe…

docker学习(二十一、network使用示例container、自定义)

文章目录 一、container应用示例1.需要共用同一个端口的服务&#xff0c;不适用container方式2.可用示例3.停掉共享源的容器&#xff0c;其他容器只有本地回环lo地址 总结 二、自定义网络应用示例默认bridge&#xff0c;容器间ip通信默认bridge&#xff0c;容器间服务名不通 自…

C++ DAY1 作业

1.定义自己的命名空间myspace&#xff0c;并在myspace中定义一个字符串&#xff0c;并实现求字符串长度 #include <iostream>using namespace std; namespace myspace {string str;int length_fun(){getline(cin,str);int i 0;while(str[i] ! \0){i;}return i;}} using…

数字反转(升级版)#洛谷

题目背景 以下为原题面&#xff0c;仅供参考: 给定一个数&#xff0c;请将该数各个位上数字反转得到一个新数。 这次与 NOIp2011 普及组第一题不同的是&#xff1a;这个数可以是小数&#xff0c;分数&#xff0c;百分数&#xff0c;整数。整数反转是将所有数位对调&#xff…

案例214:基于微信小程序的水果销售系统的设计与实现

文末获取源码 开发语言&#xff1a;Java 框架&#xff1a;springboot JDK版本&#xff1a;JDK1.8 数据库&#xff1a;mysql 5.7 开发软件&#xff1a;eclipse/myeclipse/idea Maven包&#xff1a;Maven3.5.4 小程序框架&#xff1a;uniapp 小程序开发软件&#xff1a;HBuilder …

【网络安全 | 指纹识别工具】WhatWeb使用详析

前言 WhatWeb 是一款用于识别 Web 应用程序和 Web 服务器的开源工具。它可以识别网站使用的编程语言、Web 框架、Web 服务器软件、Web 应用程序等信息&#xff0c;从而帮助安全测试人员快速了解目标网站的技术特征&#xff0c;发现可能存在的漏洞。 本文将对 WhatWeb 的使用方法…

git 如何将某个分支的某个提交复制到另外一个分支

请直接去看原文: 原文链接:git 如何将某个分支的某个提交复制到另外一个分支_gitlab里面的markdown文件可以复用其他分支的吗-CSDN博客 --------------------------------------------------------------------------------------------------------------------------------…

184.【2023年华为OD机试真题(C卷)】IPv4地址转换成整数(位运算实现JavaPythonC++JS)

请到本专栏顶置查阅最新的华为OD机试宝典 点击跳转到本专栏-算法之翼:华为OD机试 🚀你的旅程将在这里启航!本专栏所有题目均包含优质解题思路,高质量解题代码,详细代码讲解,助你深入学习,深度掌握! 文章目录 184.【2023年华为OD机试真题(C卷)】IPv4地址转换成整…

GIT如何重新生成ssh密钥过程

GIT如何重新生成ssh密钥过程 一、生成密钥前需要把之前的密钥删除吆 第一步&#xff1a;重新配置用户名和邮箱&#xff08; Git Bash 或命令窗口&#xff09; 1、配置用户命令&#xff1a;git config --global user.name “xxxxx” 2、配置邮箱命令&#xff1a;git config …

【我与Java的成长记】之this引用和构造方法的使用详解

系列文章目录 能看懂文字就能明白系列 C语言笔记传送门 &#x1f31f; 个人主页&#xff1a;古德猫宁- &#x1f308; 信念如阳光&#xff0c;照亮前行的每一步 文章目录 系列文章目录&#x1f308; *信念如阳光&#xff0c;照亮前行的每一步* 前言一、this的使用this引用的特…

Hive-DDL详解(超详细)

文章目录 前言HiveQL的数据定义语言&#xff08;DDL&#xff09;1. 创建数据库2. 切换到指定数据库3. 创建表格(1) 基本形式:(2) 示例: 4. 查看表格结构5. 删除数据库和表格(1) 删除数据库:(2) 删除数据表: 总结 前言 本教程将介绍HiveQL的数据定义语言&#xff08;DDL&#x…

Elasticsearch 查询命令执行时,如何通过词项索引、词项字典、倒排表定位文档逻辑介绍

这里不涉及到源码&#xff0c;只是根据网上的一些文章总结一下&#xff0c;目前不需要细究&#xff0c;只需要知道大概就好&#xff0c;除非你的工作是二次开发ES 一、​Term Index(词项索引)1、FSM&#xff08;Finite State Machine&#xff09;有限状态机2、FSA&#xff08;F…

SDKMAN的安装与卸载

[官方网站](Installation - SDKMAN! the Software Development Kit Manager) Installing SDKMAN! on UNIX-like platforms is as easy as ever. SDKMAN! installs smoothly on Mac OSX, Linux, WLS, Cygwin, Solaris and FreeBSD. We also support Bash and ZSH shells. Simpl…

力扣刷题记录(20)LeetCode:198、213、337

198. 打家劫舍 我们从第一个开始分析&#xff1a; dp[i]:i表示索引&#xff0c;dp表示当前索引可以拿到的最高金额 索引为0时&#xff0c;可以拿到的最高金额为1&#xff1b; 索引为1时&#xff0c;可以拿到的最高金额就是在索引[0,1]之间取&#xff0c;为2 索引为2时&…