java 给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
输入:nums = [1,3,5,4,7]
输出:3
解释:最长连续递增序列是 [1,3,5], 长度为3。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class 最长连续递增序列 { public static void main(String[] args) { int [] a = { 4 , 3 , 1 , 5 , 7 , 9 }; System.out.println(findLengthOfLCIS(a)); } public static int findLengthOfLCIS( int [] nums) { if (nums.length == 0 ) return 0 ; int max = 0 , num = 1 ; for ( int i = 0 ; i < nums.length - 1 ; i++){ if (nums[i + 1 ] > nums[i]){ num++; } else { if (num > max) //比较谁是最长序列 max = num; num = 1 ; //重新计数 } } return Math.max(max, num); } } |
给定一个字符串,求最大连续递增数字串(如ads3s456789DF3456d345AA中的456789)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class 最长连续递增序列 2 { public static void main(String[] args) { System.out.println(func( "ads3s456789DF3456d345AA" )); } public static int func(String s){ int num = 1 , max = 0 ; for ( int i = 0 ; i < s.length() - 1 ; i++){ if (Character.isDigit(s.charAt(i)) && Character.isDigit(s.charAt(i + 1 ))){ if (s.charAt(i + 1 ) > s.charAt(i)){ num++; } else { if (num > max) max = num; num = 1 ; } } else { if (num > max) max = num; num = 1 ; } } return max > num ? max : num; } } |
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过