用sliding window的方法,之前还有个k不同元素好像也是类似的思路。有时间可以去复习下。

https://leetcode.com/problems/longest-repeating-character-replacement/

// sliding window

public class Solution {
public int characterReplacement(String s, int k) { int[] count = new int[26]; int maxch = -1; // the char with max count in current substr
int max = 0; // the max count for single char in current substr
int len = 0; // current substr length
int ret = 0; // final result for (int i=0; i<s.length(); i++) {
int tmp = s.charAt(i) - 'A';
count[tmp]++;
len++; if (maxch == tmp) {
max++;
}
else {
if (count[tmp] > max) {
max = count[tmp];
maxch = tmp;
}
} if (len - max <= k) {
if (len > ret) {
ret = len;
}
} while (len - max > k) {
int newTmp = s.charAt(i-len+1) - 'A';
count[newTmp]--;
len--;
if (maxch == newTmp) {
max--;
for (int j=0; j<26; j++) {
if (count[j] > max) {
max = count[j];
maxch = j;
}
}
}
}
}
return ret;
}
}

最新文章

  1. TFS 升级错误一则 TF400654
  2. 5Hibernate配置及使用方法----青软S2SH(笔记)
  3. cordova添加plugin
  4. BZOJ3928 [Cerc2014] Outer space invaders
  5. jQuery之Nestable
  6. ContactsContract中涉及数据库中的一些列属性值【Written By KillerLegend】
  7. asp.net全局记住值
  8. (转)在Eclipse中使用JUnit4进行单元测试
  9. Springboot基础篇
  10. nested exception is java.sql.SQLException: Cannot convert value &#39;0000-00-00 00:00:00&#39; from column 14 to TIMESTAMP.
  11. Git文件状态描述
  12. redux中间件的原理——从懵逼到恍然大悟
  13. Colossus: Successor to the Google File System (GFS)
  14. 【php】php5.0以上,instanceof 用法
  15. Spark MLlib 之 Vector向量深入浅出
  16. window如何安装redis服务、卸载redis服务和启动redis服务
  17. 树莓派系统(Debain)中设置SSH服务开机自启动
  18. mysql: SELECT ... FOR UPDATE 对SELECT语句的阻塞实验
  19. POJ 2663 Tri Tiling 矩阵快速幂 难度:3
  20. 一个自带简易数据集的模拟线性分类器matlab代码——实验训练

热门文章

  1. linux命令(42):wc命令
  2. git 修改 本地分支名称
  3. List转换为DataTable List&lt;Entity&gt;
  4. 更换163的yum源
  5. Linux之父Linus的8个趣闻轶事
  6. DotNetCore 微服务上传附件
  7. [水煮 ASP.NET Web API2 方法论](12-1)创建 OData
  8. Sql Server递归查询(转)
  9. 五十七 POP3收取邮件
  10. Interllij IDEA 使用Git工具