leetcode 395 Longest Substring with At Least K Repeating Characters
时间:2019-06-12
本文章向大家介绍leetcode 395 Longest Substring with At Least K Repeating Characters,主要包括leetcode 395 Longest Substring with At Least K Repeating Characters使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
lc395 Longest Substring with At Least K Repeating Characters
思路是按子串中不同字母的个数来计算,1~26
每次计算n个不同字母所能构成的最长子串
记录其中最长子串
单次循环中,用双指针end向后遍历s,begin用来计算子串长度
当见过的字母个数 == n且这些字母出现次数都大于等于k
说明begin~end这个子串符合条件,更新maxLen
若 >,则需要向后移动begin,使其变为==
若<,不需要操作
1 class Solution { 2 public int longestSubstring(String s, int k) { 3 if(s.length() == 0 ||s.length() < k) 4 return 0; 5 int max = 0; 6 for(int i=1; i<=26; i++){ 7 max = Math.max(max, helper(s, k, i)); 8 } 9 10 return max; 11 } 12 13 private int helper(String s, int k, int abcNum){ 14 int[] count = new int[128]; 15 int begin = 0, end = 0; 16 int seenChar = 0, noLessThanK = 0; 17 int res = 0; 18 19 while(end < s.length()){ 20 if(count[s.charAt(end)]++ == 0) 21 seenChar++; 22 if(count[s.charAt(end++)] == k) 23 noLessThanK++; 24 25 /*while(seenChar > abcNum){ 26 if(count[s.charAt(begin)]-- == k) 27 noLessThanK--; 28 if(count[s.charAt(begin++)] == 0) 29 seenChar--; 30 }*/ 31 while(seenChar > abcNum){ 32 if(count[s.charAt(begin)]-- == 1) 33 seenChar--; 34 if(count[s.charAt(begin++)] == k-1) 35 noLessThanK--; 36 } 37 38 if(seenChar == noLessThanK && seenChar == abcNum) 39 res = Math.max(res, end-begin); 40 } 41 42 return res; 43 } 44 }
原文地址:https://www.cnblogs.com/hwd9654/p/11009640.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 10-3 信号
- 11-1 环境中存储的是什么?
- 【Rust日报】2020-08-07 无船同志关于Rust未来Generator语法的新尝试
- 11-2 环境是如何建立的
- 11-3 激活修改
- 12-1 定制提示符
- MySQL INSERT的4种形态
- PowerBI DAX 性能优化 高级视图算法 超越经典 性能提升成千上万倍
- Java开发必备 Git 分支开发:规范指南及完全学会Git的24堂课笔记
- 聊聊dubbo-go的failbackCluster
- 视频综合管理平台EasyNVS通道列表如何获得RTMP地址和RTSP地址?
- 【每周一库】 rust-ftp - an FTP client written in Rust
- Mall 电商实战项目发布重大更新,全面支持SpringBoot 2.3.0 !
- Python与Tableau相结合,万字长文搞定传统线下连锁店数据分析
- 社群答疑精选03:拆分数据到新工作表