台州公司建站模板,同城分类网站建设,珠海网站建设建站模板,织梦网站后台管理教程刷题顺序及思路来源于代码随想录#xff0c;网站地址#xff1a;https://programmercarl.com
目录
300. 最长递增子序列 - 力扣#xff08;LeetCode#xff09;
674. 最长连续递增序列 - 力扣#xff08;LeetCode#xff09;
718. 最长重复子数组 - 力扣#xff08…刷题顺序及思路来源于代码随想录网站地址https://programmercarl.com
目录
300. 最长递增子序列 - 力扣LeetCode
674. 最长连续递增序列 - 力扣LeetCode
718. 最长重复子数组 - 力扣LeetCode 300. 最长递增子序列 - 力扣LeetCode
给你一个整数数组 nums 找到其中最长严格递增子序列的长度。
子序列 是由数组派生而来的序列删除或不删除数组中的元素而不改变其余元素的顺序。例如[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。
输入nums [10,9,2,5,3,7,101,18]
输出4
解释最长递增子序列是 [2,3,7,101]因此长度为 4 import java.util.Arrays;/*** author light* Description 最长递增子序列*** 思路数组中只要有递增的就行无需连续* 动态规划--弄明白dp数组所表示的含义* dp[i]:nums[i]之前包括nums[i])的字序列最大递增子序列长度为dp[i]* create 2023-10-15 9:50*/
public class LengthOfLISTest {public static void main(String[] args) {int[] nums{0,1,0,3,2};System.out.println(lengthOfLIS(nums));}public static int lengthOfLIS(int[] nums) {int[] dpnew int[nums.length];Arrays.fill(dp, 1);//初始化int result1;for (int i 1; i nums.length; i) {for (int j 0; j i; j) {if (nums[i] nums[j]) dp[i] Math.max(dp[i], dp[j] 1);}if (dp[i] result) result dp[i]; // 取长的子序列}return result;}
}674. 最长连续递增序列 - 力扣LeetCode
给定一个未经排序的整数数组找到最长且 连续递增的子序列并返回该序列的长度。
连续递增的子序列 可以由两个下标 l 和 rl r确定如果对于每个 l i r都有 nums[i] nums[i 1] 那么子序列 [nums[l], nums[l 1], ..., nums[r - 1], nums[r]] 就是连续递增子序列。 import java.util.Arrays;/*** author light* Description 最长连续递增的子序列* 思路只需考虑nums[i]和nums[i-1]* create 2023-10-15 10:49*/
public class FindLengthOfLCISTest {public int findLengthOfLCIS(int[] nums) {int[] dpnew int[nums.length];Arrays.fill(dp,1);int res1;for (int i 1; i nums.length; i) {if(nums[i]nums[i-1]){dp[i]dp[i-1]1;}resMath.max(dp[i],res);}return res;}
}718. 最长重复子数组 - 力扣LeetCode
给两个整数数组 nums1 和 nums2 返回 两个数组中 公共的 、长度最长的子数组的长度 。
输入nums1 [1,2,3,2,1], nums2 [3,2,1,4,7]
输出3
解释长度最长的公共子数组是 [3,2,1] 。 /*** author light* Description 最长重复子数组** 给两个整数数组 nums1 和 nums2 返回 两个数组中 公共的 、长度最长的子数组的长度 。** 思路搞清dp数组含义* dp[i][j]:以i-1为结尾的数组nums1和以j-1为结尾的数组nums2的最长重复子数组长度为dp[i][j]* 比以i,j为结尾的好处简化了dp数组的初始化使得dp[i][0]和dp[0][j]没有意义* create 2023-10-15 13:38*/
public class FindLengthTest {public int findLength(int[] nums1, int[] nums2) {//dp[i][j]:以i-1为结尾的数组nums1和以j-1为结尾的数组nums2的最长重复子数组长度为dp[i][j]int[][] dpnew int[nums1.length1][nums2.length1];int res0;for (int i 1; i nums1.length; i) {for (int j 1; j nums2.length; j) {if(nums1[i-1]nums2[j-1]){dp[i][j]dp[i-1][j-1]1;}res Math.max(dp[i][j],res);}}return res;}
} 文章转载自: http://www.morning.kcfnp.cn.gov.cn.kcfnp.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.ryxdf.cn.gov.cn.ryxdf.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.wmdqc.com.gov.cn.wmdqc.com http://www.morning.nxstj.cn.gov.cn.nxstj.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.tlnbg.cn.gov.cn.tlnbg.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.wfwqr.cn.gov.cn.wfwqr.cn http://www.morning.tkjh.cn.gov.cn.tkjh.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.ldspj.cn.gov.cn.ldspj.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.kjmcq.cn.gov.cn.kjmcq.cn http://www.morning.dkqyg.cn.gov.cn.dkqyg.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.hnpkr.cn.gov.cn.hnpkr.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.hknk.cn.gov.cn.hknk.cn http://www.morning.nlywq.cn.gov.cn.nlywq.cn http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.jtcq.cn.gov.cn.jtcq.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.kstlm.cn.gov.cn.kstlm.cn http://www.morning.czcbl.cn.gov.cn.czcbl.cn http://www.morning.ybgyz.cn.gov.cn.ybgyz.cn http://www.morning.xmtzk.cn.gov.cn.xmtzk.cn http://www.morning.tgtrk.cn.gov.cn.tgtrk.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.gqwbl.cn.gov.cn.gqwbl.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.rrgm.cn.gov.cn.rrgm.cn http://www.morning.bpmfl.cn.gov.cn.bpmfl.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.kfjnx.cn.gov.cn.kfjnx.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn http://www.morning.gqwpl.cn.gov.cn.gqwpl.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.pghgq.cn.gov.cn.pghgq.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn http://www.morning.fwnyz.cn.gov.cn.fwnyz.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.prplf.cn.gov.cn.prplf.cn http://www.morning.nwbnt.cn.gov.cn.nwbnt.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.youyouling.cn.gov.cn.youyouling.cn http://www.morning.pbzgj.cn.gov.cn.pbzgj.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.hxycm.cn.gov.cn.hxycm.cn http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn