[Selenium] 通过Java+Selenium查询某个博主的Top100文章质量分

这篇具有很好参考价值的文章主要介绍了[Selenium] 通过Java+Selenium查询某个博主的Top100文章质量分。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

系列文章目录

通过Java+Selenium查询文章质量分
通过Java+Selenium查询某个博主的Top40文章质量分
通过Java+Selenium查询某个博主的Top100文章质量分


[Selenium] 通过Java+Selenium查询某个博主的Top100文章质量分


前言

大家好,我是青花,本篇给大家分享一下《通过Java+Selenium查询某个博主的Top100文章质量分》,针对上一章Top40文章,做了简单的优化,在查询博客质量分的时候,控制了频繁的开关Chrome浏览器,避免了重复的加载Chrome驱动以及打开Chrome浏览器。
备注: 在上章节里,加载100文章,在50-60文章数时,就会被限制访问。


一、环境准备

浏览器:本篇使用的是Chrome
Chrome浏览器版本:113
Chrome驱动版本:113(Java爬虫第一篇)
Java版本:Jdk1.8
selenium版本: 4.9.1


二、查询某个博主的Top100文章

2.1、修改pom.xml配置

	 <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
     <dependency>
         <groupId>org.jsoup</groupId>
         <artifactId>jsoup</artifactId>
         <version>1.11.3</version>
     </dependency>

     <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.9.1</version>
     </dependency>
     
     <dependency>
         <groupId>com.google.code.gson</groupId>
         <artifactId>gson</artifactId>
         <version>2.10.1</version>
     </dependency>

2.2、配置Chrome驱动(SeleniumUtil类,包含驱动位置,图片保存路径)

/***
 * @title SeleniumUtil
 * @desctption Selenium辅助类
 * @author Kelvin
 * @create 2023/6/21 22:47
 **/
@Slf4j
public class SeleniumUtil {

    public final static String CHROMEDRIVERPATH = "/Users/apple/Downloads/chromedriver_mac64/chromedriver";

    public final static String LOCATION_IMG_BASE_PATH = "~/java/code/spiderX/img/";

    public static void sleep(int m) {
        try {
            Thread.sleep(m);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
	System.setProperty("webdriver.chrome.driver", SeleniumUtil.CHROMEDRIVERPATH );//    	chromedriver localPath

2.3、引入浏览器配置

	 WebDriver driver;
	 ChromeOptions chromeOptions = new ChromeOptions();

2.4、设置无头模式

	chromeOptions.addArguments('--headless')
	chromeOptions.addArguments("--remote-allow-origins=*");

2.5、启动浏览器实例,添加配置信息

	driver = new ChromeDriver(chromeOptions);

2.6、窗口设置

	chromeOptions.addArguments("–no-sandbox");  //--start-maximized

2.7、禁止加载图片设置

	// 增加禁止加载图片的设置
   	HashMap<String, Object> prefs = new HashMap<>();
    prefs.put("profile.default_content_settings", 2);
    chromeOptions.setExperimentalOption("prefs", prefs);
    chromeOptions.addArguments("blink-settings=imagesEnabled=false");//禁用图片

[Selenium] 通过Java+Selenium查询某个博主的Top100文章质量分
[Selenium] 通过Java+Selenium查询某个博主的Top100文章质量分

2.8、加载博主地址

 	String baseUrl = "https://blog.csdn.net/s445320?type=blog";

[Selenium] 通过Java+Selenium查询某个博主的Top100文章质量分

2.9、加载文章列表

   //定位到文章列表
   WebElement mainSelectE = driver.findElement(By.cssSelector("div.mainContent"));

2.10、加载下一页

模拟浏览器滚动条下拉,加载下一页数据

   //加载下一页
   JavascriptExecutor jsDriver = (JavascriptExecutor) driver;//将java中的driver强制转型为JS类型
   jsDriver.executeScript("window.scrollTo(0, 50)");
   jsDriver.executeScript("window.scrollTo(0, document.body.scrollHeight-20)");
   SeleniumUtil.sleep(500);
   jsDriver.executeScript("window.scrollTo(0, document.body.scrollHeight +1)");
   SeleniumUtil.sleep(2000);

2.11、设置加载100条数据

 	// 获取Top100的数量
    int topNum = 100;
        
	//如果加载的数据超过或等于 要求的最大长度,返回现在已加载的数据
    if( webElements.size() >= topNum ) {
        for(WebElement element : webElements ){
            System.out.println(  element.getAttribute("href")  );
            blogUrlList.add(element.getAttribute("href"));
        }
        log.info("文章已读取 {} 条,最大限制 {} 条!" , webElements.size() , topNum);
        break;
    }

[Selenium] 通过Java+Selenium查询某个博主的Top100文章质量分

2.12、对频繁的开关Chrome浏览器做了优化

优化1

针对上一章Top40文章,做了简单的优化,在查询博客质量分的时候,控制了频繁的开关Chrome浏览器,避免了重复的加载Chrome驱动以及打开Chrome浏览器。

优化2

初次的时候加载查询博客质量分页面,后续只需要更换博客文章链接地址,去获取数据即可。
[Selenium] 通过Java+Selenium查询某个博主的Top100文章质量分

	/**
     *  获取质量数据
     * @throws IOException
     */
    CsdnBlogInfo csdnQcBySelenium(String blogUrl , WebDriver driver , boolean isFirst) {
        log.info("csdnQcBySelenium start!");
        CsdnBlogInfo csdnBlogInfo = new CsdnBlogInfo();

        //第一次时加载查询质量分页面
        if( isFirst ) {
            driver.get("https://www.csdn.net/qc");
            SeleniumUtil.sleep(500);
        }

[Selenium] 通过Java+Selenium查询某个博主的Top100文章质量分

2.13、成果


00:44:50.693 [main] INFO com.kelvin.spiderx.service.CsdnQcService - csdnQcBySelenium start!
00:45:03.151 [main] INFO com.kelvin.spiderx.service.CsdnQcService - 文章已读取 100 条,最大限制 100 条!
00:45:03.151 [main] INFO com.kelvin.spiderx.service.CsdnQcService - blogUrlList size:100
00:45:03.294 [main] INFO com.kelvin.spiderx.service.CsdnQcService - blogUrlList:
[{"title":"[刷题] 删除有序数组中的重复项","posttime":"- 青花锁 · 2023-06-25 12:49:42 -","score":"82","remark":"文章质量良好"},{"title":"[Selenium] 通过Java+Selenium查询某个博主的Top40文章质量分","posttime":"- 青花锁 · 2023-06-25 01:22:55 -","score":"87","remark":"文章质量良好"},{"title":"[Selenium] 通过Java+Selenium查询文章质量分","posttime":"- 青花锁 · 2023-06-23 08:42:36 -","score":"86","remark":"文章质量良好"},{"title":"【并发知识点】CAS的实现原理及应用","posttime":"- 青花锁 · 2023-06-22 11:55:48 -","score":"90","remark":"文章质量良好"},{"title":"【并发知识点】AQS的实现原理及应用","posttime":"- 青花锁 · 2023-06-22 11:35:53 -","score":"90","remark":"文章质量良好"},{"title":"简单介绍html/javascript、ajax应用","posttime":"- 青花锁 · 2023-06-22 10:18:01 -","score":"92","remark":"文章质量良好"},{"title":"[设计模式] OOP六大原则","posttime":"- 青花锁 · 2023-06-22 01:24:19 -","score":"89","remark":"文章质量良好"},{"title":"[Web前端] Servlet及应用","posttime":"- 青花锁 · 2023-06-22 01:07:56 -","score":"91","remark":"文章质量良好"},{"title":"【在线商城系统】数据来源-爬虫篇","posttime":"- 青花锁 · 2023-06-22 00:48:46 -","score":"87","remark":"文章质量良好"},{"title":"《项目实战》构建SpringCloud alibaba项目(三、构建服务方子工程store-user-service)","posttime":"- 青花锁 · 2023-06-21 18:20:46 -","score":"86","remark":"文章质量良好"},{"title":"《项目实战》构建SpringCloud alibaba项目(二、构建微服务鉴权子工程store-authority-service)","posttime":"- 青花锁 · 2023-06-19 17:24:53 -","score":"86","remark":"文章质量良好"},{"title":"《项目实战》使用JDBC手写分库","posttime":"- 青花锁 · 2023-06-16 17:56:03 -","score":"88","remark":"文章质量良好"},{"title":"《项目实战》构建SpringCloud alibaba项目(一、构建父工程、公共库、网关))","posttime":"- 青花锁 · 2023-06-15 20:41:46 -","score":"92","remark":"文章质量良好"},{"title":"《项目实战》 Jenkins 与 CICD、发布脚本","posttime":"- 青花锁 · 2023-06-13 15:53:46 -","score":"90","remark":"文章质量良好"},{"title":"《微服务实战》 第三十二章 微服务链路跟踪-sleuth zipkin","posttime":"- 青花锁 · 2023-06-11 18:41:09 -","score":"90","remark":"文章质量良好"},{"title":"《微服务实战》 第三十一章 ShardingSphere - ShardingSphere-JDBC","posttime":"- 青花锁 · 2023-06-11 18:25:34 -","score":"80","remark":"文章质量良好"},{"title":"《微服务实战》 第三十章 分布式事务框架seata TCC模式","posttime":"- 青花锁 · 2023-06-11 10:38:21 -","score":"89","remark":"文章质量良好"},{"title":"《微服务实战》 第二十九章 分布式事务框架seata AT模式","posttime":"- 青花锁 · 2023-06-11 10:23:44 -","score":"92","remark":"文章质量良好"},{"title":"《微服务实战》 第二十八章 分布式锁框架-Redisson","posttime":"- 青花锁 · 2023-06-09 17:55:59 -","score":"91","remark":"文章质量良好"},{"title":"【项目实战】一、Spring boot整合JWT、Vue案例展示用户鉴权","posttime":"- 青花锁 · 2023-06-09 09:06:43 -","score":"92","remark":"文章质量良好"},{"title":"《微服务实战》 第二十七章 CAS","posttime":"- 青花锁 · 2023-05-30 16:26:30 -","score":"87","remark":"文章质量良好"},{"title":"《微服务实战》 第二十六章 Java锁的分类","posttime":"- 青花锁 · 2023-05-29 17:28:47 -","score":"91","remark":"文章质量良好"},{"title":"《微服务实战》 第二十五章 Java多线程安全与锁","posttime":"- 青花锁 · 2023-05-29 16:07:53 -","score":"91","remark":"文章质量良好"}.....]
00:45:03.296 [main] INFO com.kelvin.spiderx.service.CsdnQcService - 此博主有文章,开始解析文章质量分!
00:45:03.296 [main] INFO com.kelvin.spiderx.service.CsdnQcService - csdnQcBySelenium start!
---------- 省略解析过程
此博主质量分如下:
00:50:40.699 [main] INFO com.kelvin.spiderx.service.CsdnQcService - {"title":"[刷题] 删除有序数组中的重复项","posttime":"- 青花锁 · 2023-06-25 12:49:42 -","score":"82","remark":"文章质量良好"},{"title":"[Selenium] 通过Java+Selenium查询某个博主的Top40文章质量分","posttime":"- 青花锁 · 2023-06-25 01:22:55 -","score":"87","remark":"文章质量良好"},{"title":"[Selenium] 通过Java+Selenium查询文章质量分","posttime":"- 青花锁 · 2023-06-23 08:42:36 -","score":"86","remark":"文章质量良好"},{"title":"【并发知识点】CAS的实现原理及应用","posttime":"- 青花锁 · 2023-06-22 11:55:48 -","score":"90","remark":"文章质量良好"},{"title":"【并发知识点】AQS的实现原理及应用","posttime":"- 青花锁 · 2023-06-22 11:35:53 -","score":"90","remark":"文章质量良好"},{"title":"简单介绍html/javascript、ajax应用","posttime":"- 青花锁 · 2023-06-22 10:18:01 -","score":"92","remark":"文章质量良好"},{"title":"[设计模式] OOP六大原则","posttime":"- 青花锁 · 2023-06-22 01:24:19 -","score":"89","remark":"文章质量良好"},{"title":"[Web前端] Servlet及应用","posttime":"- 青花锁 · 2023-06-22 01:07:56 -","score":"91","remark":"文章质量良好"}....]


00:50:40.693 [main] INFO com.kelvin.spiderx.service.CsdnQcService - csdnQcBySelenium end!

三、循环查询文章质量分

通过Java+Selenium查询文章质量分
查询文章质量分可见上述文章,在上一章中对返回值,禁止图片加载做了优化。在本章中,对driver做了优化,多个查询页面共用一个driver,且在进入查询质量分阶段,只在初次加载页面。

备注: 后期针对查询质量分,在并发情况下,可考虑driver池化技术,100篇文章分为N段,分别去作业,优化性能。


四、代码

package com.kelvin.spiderx.service;

import com.google.gson.Gson;
import com.kelvin.spiderx.util.SeleniumUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/***
 * @title CsdnQcService
 * @desctption CSDN查询质量分
 * @author LTF
 * @create 2023/6/21 23:02
 **/
@Slf4j
public class CsdnQcService {

    @Data
    class CsdnBlogInfo {
        private String title;

        private String posttime;

        private String score;

        private String remark;

    }

    /**
     *  获取质量数据
     * @throws IOException
     */
    CsdnBlogInfo csdnQcBySelenium(String blogUrl , WebDriver driver , boolean isFirst) {
        log.info("csdnQcBySelenium start!");
        CsdnBlogInfo csdnBlogInfo = new CsdnBlogInfo();

        if( isFirst ) {
            driver.get("https://www.csdn.net/qc");
            SeleniumUtil.sleep(500);
        }

        //定位到输入框
        WebElement inputSelectE = driver.findElement(By.cssSelector("input.el-input__inner"));
        //输入文字地址
        inputSelectE.sendKeys(blogUrl);

        SeleniumUtil.sleep(100);

        //定位查询按钮
        WebElement qcSelectE = driver.findElement(By.cssSelector("div.trends-input-box-btn"));
        //点击查询按钮
        qcSelectE.click();

        SeleniumUtil.sleep(1000);

        //获取右边区域 -- 文章质量分结果区域
        WebElement mainSelectE = driver.findElement(By.cssSelector("div.csdn-body-right"));

        //转化为Jsoup文档处理
        Document doc = Jsoup.parse( mainSelectE.getAttribute("outerHTML") );

        //获取文章标题
        String title = doc.select("span.title").text();
        if(!StringUtils.isEmpty(title)) {
            csdnBlogInfo.setTitle(title);
        }

        //获取作者和发布时间
        String posttime = doc.select("span.name").text();
        if(!StringUtils.isEmpty(posttime)) {
            csdnBlogInfo.setPosttime(posttime);
        }

        //获取质量分
        String score = doc.select("p.img").text();
        if(!StringUtils.isEmpty(score)) {
            csdnBlogInfo.setScore(score);
        }

        //获取博文质量分建议
        String remark = doc.select("p.desc").text();
        if(!StringUtils.isEmpty(remark)) {
            csdnBlogInfo.setRemark(remark);
        }

        //打印结果
        log.info("文章标题:{} , 作者和发布时间:{} , 质量分:{} , 博文建议:{}" , title , posttime , score , remark );

//        driver.quit();
        log.info("csdnQcBySelenium end!");
        return csdnBlogInfo;
    }

    /**
     *  查询指定博主的文章质量分
     */
    void allBlogQcDataBySelenium() {
        String baseUrl = "https://blog.csdn.net/s445320?type=blog";

        System.setProperty("webdriver.chrome.driver", SeleniumUtil.CHROMEDRIVERPATH );// chromedriver localPath
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        chromeOptions.addArguments("–no-sandbox");  //--start-maximized

        // 增加禁止加载图片的设置
        HashMap<String, Object> prefs = new HashMap<>();
        prefs.put("profile.default_content_settings", 2);
        chromeOptions.setExperimentalOption("prefs", prefs);
        chromeOptions.addArguments("blink-settings=imagesEnabled=false");//禁用图片

        WebDriver driver = new ChromeDriver(chromeOptions);

        driver.get(baseUrl);

        SeleniumUtil.sleep(200);

        //定位到文章列表
        WebElement mainSelectE = driver.findElement(By.cssSelector("div.mainContent"));

        boolean isEnd = false;
        // 获取Top40的数量
        int topNum = 100;
        // 上一次读取的文章数
        int prePoint = 0;
        List<String> blogUrlList = new ArrayList<>();
        List<WebElement>  webElements = null;
        while ( isEnd == false  ) {
            JavascriptExecutor jsDriver = (JavascriptExecutor) driver;//将java中的driver强制转型为JS类型
            jsDriver.executeScript("window.scrollTo(0, 50)");
            jsDriver.executeScript("window.scrollTo(0, document.body.scrollHeight-20)");
            SeleniumUtil.sleep(500);
            jsDriver.executeScript("window.scrollTo(0, document.body.scrollHeight +1)");
            SeleniumUtil.sleep(2000);

            webElements = mainSelectE.findElements(By.cssSelector("article.blog-list-box>a"));

            // 如果上一次的文章数
            //    等于 当前页面的文章数:文章已全部读取完
            //    否则,继续加载下一页
            if( webElements.size() == prePoint){
                for(WebElement element : webElements ){
                    System.out.println(  element.getAttribute("href")  );
                    blogUrlList.add(element.getAttribute("href"));
                }
                log.info("文章已全部读取完");
                break;
            } else {
                prePoint = webElements.size();
            }

            //如果加载的数据超过或等于 要求的最大长度,返回现在已加载的数据
            if( webElements.size() >= topNum ) {
                for(WebElement element : webElements ){
                    System.out.println(  element.getAttribute("href")  );
                    blogUrlList.add(element.getAttribute("href"));
                }
                log.info("文章已读取 {} 条,最大限制 {} 条!" , webElements.size() , topNum);
                break;
            }
        }
        log.info("blogUrlList size:{}" , blogUrlList.size());
        log.info("blogUrlList:{}" , new Gson().toJson(blogUrlList) );

        List<CsdnBlogInfo> csdnBlogInfoList = null;
        if(CollectionUtils.isEmpty(blogUrlList)) {
            log.info("此博主没有发表文章!");
        } else {
            log.info("此博主有文章,开始解析文章质量分!");
            csdnBlogInfoList = new ArrayList<>();
            int num = 0;
            for (String blogUrl : blogUrlList) {
                try{
                    CsdnBlogInfo csdnBlogInfo = this.csdnQcBySelenium(blogUrl , driver , num <= 0 );
                    if( null != csdnBlogInfo ) {
                        csdnBlogInfoList.add(csdnBlogInfo);
                    }
                    num ++;
                } catch (Exception e) {
                    log.info("解析文章质量分失败,文章:{}" , blogUrl);
                }

            }

            if(CollectionUtils.isEmpty(csdnBlogInfoList)) {
                log.info("解析文章质量分失败!");
            } else {
                log.info("此博主质量分如下:");
                log.info(new Gson().toJson(csdnBlogInfoList));
            }

        }
        driver.quit();
        log.info("读取数据完毕!the end!");
    }

    public static void main(String[] args) {
        CsdnQcService csdnQcService = new CsdnQcService();
        csdnQcService.allBlogQcDataBySelenium();
    }


}


总结

通过Java+Selenium查询某个博主的Top100文章质量分至此结束,优化空间还有很大,以实现效果为主。文章来源地址https://www.toymoban.com/news/detail-508001.html

到了这里,关于[Selenium] 通过Java+Selenium查询某个博主的Top100文章质量分的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ES 通过查询更新某个字段,Error 500 (Internal Server Error)

    问题描述: 项目中通过查询ES中某个字段,并更新某个值的字段,当量比较大的时候报错: upsert associated failed: elastic: Error 500 (Internal Server Error): Failed to compile inline script [ctx._source.pcap_filename = ] using lang [painless] [type=general_script_exception] 问题解析: 1. 通过查询某个字段并更新这

    2024年02月03日
    浏览(33)
  • 通过Python+Selenium查询文章质量分

    通过Python+Selenium查询文章质量分 质量分查询地址 大家好,我是空空star,本篇给大家分享一下 《通过Python+Selenium查询文章质量分》 。 浏览器:本篇使用的是Chrome Chrome驱动版本:110.0.5481.77 Python版本:Python3.8 selenium版本: 4.8.2 Selenium基础篇之环境准备 import pprint import time fro

    2024年02月11日
    浏览(74)
  • java通过stream流的形式把列表中某个字段的值取出并生成列表

    可以使用Java 8中引入的Stream API来实现这一功能。例如,假设你有一个类名为Person的列表,并且你想要从这个列表中提取所有人的姓名并生成一个新的列表。你可以这样做: 这段代码首先使用 stream() 方法将列表转换为流。然后,使用 map() 方法对流中的每个元素执行一个转换函

    2024年02月12日
    浏览(33)
  • top100-贪心算法

    题目链接 121. 买卖股票的最佳时机 - 力扣(LeetCode) 解题思路 1、暴力破解 我们需要找出给定数组中两个数字之间的最大差值,即max(prices[j] - prices[i]) 2、一次遍历 遍历价格数组一遍,记录历史最低点,然后在每一天考虑这么一个问题:如果我是在历史最低点买进的,那么

    2024年03月25日
    浏览(38)
  • 【Leetcode】top 100 图论

    基础知识补充 1.图分为有向图和无向图,有权图和无权图; 2.图的表示方法:邻接矩阵适合表示稠密图,邻接表适合表示稀疏图;    邻接矩阵:    邻接表: 基础操作补充 1.邻接矩阵: 2.邻接表: 3.图的遍历:  题目 200 岛屿数量 给你一个由  \\\'1\\\' (陆地)和  \\\'0\\\' (水)组

    2024年04月10日
    浏览(33)
  • TOP100 图论

    给你一个由  \\\'1\\\' (陆地)和  \\\'0\\\' (水)组成的的二维网格,请你计算网格中岛屿的数量。 岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。 此外,你可以假设该网格的四条边均被水包围。 示例 1: 示例 2: 提示: m == grid.length n == g

    2024年02月19日
    浏览(25)
  • 【Leetcode】top 100 多维动态规划

    62 不同路径 一个机器人位于一个  m x n   网格的左上角,机器人每次只能向下或者向右移动一步,机器人试图达到网格的右下角,问总共有多少条不同的路径? 分析:dp[i][j] 代表走到 (i, j) 的路径总和数 递推规律:dp[i][j] = dp[i-1][j] + dp[i][j-1] 初始条件:dp[0][:] = 1 dp[:][0] = 1

    2024年03月26日
    浏览(37)
  • LeetCode - #85 最大矩形(Top 100)

    本题为 LeetCode 前 100 高频题 本题由于没有合适答案为以往遗留问题,最近有时间将以往遗留问题一一完善。 我们社区陆续会将顾毅( Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。 )的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 LeetCode 算法到目前我

    2024年02月10日
    浏览(29)
  • Leetcode Top 100 Liked Questions(序号53~74)

    题意:一个数组,找到和最大的子串 我记得好像On的动态规划来做的?但是想不起来了,先死做,用的 前缀和——TLE超时 那就只能想想dp怎么做了 假设dp[i]表示的是以 i 为右端点的最大的子串,dp[0]是自己; i=1时,如果dp[0]小于0,dp[1]=nums[1],否则dp[1]=dp[0]+nums[1] i=2时,如果

    2024年02月12日
    浏览(33)
  • Leetcode Top 100 Liked Questions(序号105~139)

    题意:根据前序遍历和中序遍历来构造二叉树 要用递归造树,要同时递归左子树和右子树,造树需要左边边界和右边边界 build函数有 树的跟指针, 前序的有左边边界和右边边界,中序的左边边界和右边边界 如果lr return; 因为是先序遍历,所以左子树是先序的第一个,先构

    2024年02月10日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包