力扣爆刷第119天之CodeTop100五连刷81-85

ops/2024/10/21 22:59:17/

力扣爆刷第119天之CodeTop100五连刷81-85

文章目录

      • 力扣爆刷第119天之CodeTop100五连刷81-85
      • 一、14. 最长公共前缀
      • 二、718. 最长重复子数组
      • 三、169. 多数元素
      • 四、662. 二叉树最大宽度
      • 五、128. 最长连续序列

一、14. 最长公共前缀

题目链接:https://leetcode.cn/problems/longest-common-prefix/description/
思路:求最长公共前缀,直接取第一个字符串,然后每一个字符就遍历剩余的字符串,时间复杂度O(N)2.

class Solution {public String longestCommonPrefix(String[] strs) {if(strs.length == 0 || strs[0].equals("")) return strs[0];StringBuilder builder = new StringBuilder();String s0 = strs[0];int k = 0;for(int i = 0; i < s0.length(); i++) {char c = s0.charAt(i);for(int j = 1; j < strs.length; j++) {if(i >= strs[j].length() || c != strs[j].charAt(i)) {return builder.toString();}}builder.append(c);}return builder.toString();}
}

二、718. 最长重复子数组

题目链接:https://leetcode.cn/problems/maximum-length-of-repeated-subarray/description/
思路:最长重复子数组是连续的子数组,定义dp[i][j]表示,以nums1[i]和nums2[j]为结尾的区间的最长重复子数组,根据这个定义,若nums1[i] != nums2[j],则以这两为结尾的字符子串长度为0,相等为dp[i+1][j+1] = dp[i][j] + 1;
定义好dp然后根据定义去推导递推关系。

class Solution {public int findLength(int[] nums1, int[] nums2) {int m = nums1.length, n = nums2.length, max = Integer.MIN_VALUE;int[][] dp = new int[m+1][n+1];for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if(nums1[i] == nums2[j]) {dp[i+1][j+1] = dp[i][j] + 1;}max = Math.max(max, dp[i+1][j+1]);}}return max;}
}

三、169. 多数元素

题目链接:https://leetcode.cn/problems/majority-element/description/
思路:利用一个元素用来计数,相同的数加1,不同的数减1,最后剩下的数就是多数元素。

class Solution {public int majorityElement(int[] nums) {int count = 0, res = 0;for(int n : nums) {if(count == 0) {res = n;}count = res == n ? count + 1 : count - 1;}return res;}
}

四、662. 二叉树最大宽度

题目链接:https://leetcode.cn/problems/maximum-width-of-binary-tree/description/
思路:本题求最大宽度,是需要利用满二叉树的特性的,采用前序遍历,给每一个节点编号,root=id,左孩子=id2,右孩子=id2+1,只需要记录下每一行最左边的节点的id,后面每次递归都计算当前节点与这一层最左边的距离,id相减就是距离,那么如何收集最左边的节点呢,采用先序遍历,list元素个数和深度相等就收集。
在这里插入图片描述

class Solution {ArrayList<Integer> list = new ArrayList<>();int max = 1;public int widthOfBinaryTree(TreeNode root) {traverse(root, 1, 1);return max;}void traverse(TreeNode root, int id, int deep) {if(root == null) return;if(list.size() == deep-1) {list.add(id);}else{max = Math.max(max, id - list.get(deep-1) + 1);}traverse(root.left, id * 2, deep + 1);traverse(root.right, id * 2 + 1, deep + 1);}
}

五、128. 最长连续序列

题目链接:https://leetcode.cn/problems/longest-consecutive-sequence/description/
思路:列表无序,也不要求顺序,求最长连续序列,还要求O(N),那么就不能排序了,可以利用set集合,如果连续一定是一段一段的,我们只需要找到每一个连续段的开头,从这个地方开始技术,全部统计完就可以,注意,只从连续段的头开始计算。

class Solution {public int longestConsecutive(int[] nums) {HashSet<Integer> set = new HashSet<>();int max = 0;for(int i : nums) {set.add(i);}for(int i : set) {if(!set.contains(i-1)) {int t = 0;while(set.contains(i)) {t++;i++;}max = Math.max(max, t);}}return max;}
}

http://www.ppmy.cn/ops/7467.html

相关文章

wps导出pdf文献引用不能跳转解决办法

问题描述 本科论文参考文献使用wps设置交叉引用&#xff0c;但导出pdf后无法跳转引用 尝试 用office word打开文件word版跳转没有问题&#xff0c; 另存为pdf或导出pdf。 但是pdf版跳转完全错误。 16跳到14.但是总体而言都是跳到包含该序号的页 要求不高的话也可以&#x…

修改element-ui中el-calendar(日历)的样式

效果图如下&#xff1a; <template><div class"dashboard-container"><el-card style"width: 350px; height: auto; border-radius: 8px"><div class"custom-style"><p class"new-data">{{ newDate }}&…

四川古力未来科技抖音小店安全:保障您的购物体验,让每一笔交易都安心

在数字化浪潮席卷全球的今天&#xff0c;电子商务已经成为人们生活中不可或缺的一部分。四川古力未来科技抖音小店&#xff0c;作为新兴的电商力量&#xff0c;始终将顾客的安全放在首位&#xff0c;倾力打造安全、便捷、高效的购物平台&#xff0c;让每一位顾客在享受购物乐趣…

软考132-上午题-【软件工程】-沟通路径

一、定义 1-1、沟通路径1 沟通路径 1-2、沟通路径2 沟通路径 n-1 二、真题 真题1&#xff1a; 真题2&#xff1a; 真题3&#xff1a;

日常小bug

1.mybatis-config.xml中记载sql的映射文件的方式 <mappers><!-- 方法一&#xff1a;使用xml文件进行注册,注意&#xff1a;这里是斜线--><mapper resource"com/dao/UserMapper.xml"/><!-- 方法二&#xff1a;使用class进行注册&#xff0c;注…

SpringCloud整合ElasticSearch搜索使用

环境说明 ORM&#xff1a;easy-es 2.0.0(opens new window) ElasticSearch&#xff1a;7.14.0 pigx&#xff1a;5.2 请保持环境如上&#xff0c;ElasticSearch 兼容性较差无法保证其他版本正常整合执行。快速开始 ① 安装 ElasticSearch docker run --name es714 -p 9200:920…

Expiring 3623 record(s) for 2:xxx ms has passed since batch

Expiring 3623 record(s) for 2:xxx ms has passed since batch 报错大意为&#xff1a;生产发送批次已经创建&#xff0c;但是已经过去120000ms&#xff0c;仍然没有发送&#xff0c;消息过期 (当kafka服务器磁盘空间不足时&#xff0c;也会报此错误。清空磁盘空间&#xff0…

微信小程序之console.log()使用

console.log() 是 JavaScript 中的标准内置函数&#xff0c;主要用于在浏览器的控制台&#xff08;Console&#xff09;中输出信息&#xff0c;帮助开发者进行调试和跟踪代码运行状态。以下是一些基本和进阶的使用方法&#xff1a; 1、基础使用方法&#xff1a; console.log(…