用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;
}
}

longest-repeating-character-replacement(难)的更多相关文章

  1. Leetcode: Longest Repeating Character Replacement && G 面经

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  2. [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  3. G 面经 && Leetcode: Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  4. LeetCode——Longest Repeating Character Replacement

    1. Question Given a string that consists of only uppercase English letters, you can replace any lett ...

  5. leetcode424 Longest Repeating Character Replacement

    """ Given a string s that consists of only uppercase English letters, you can perform ...

  6. [LeetCode] Longest Repeating Character Replacement 最长重复字符置换

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  7. LeetCode 424. Longest Repeating Character Replacement

    原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...

  8. 【leetcode】424. Longest Repeating Character Replacement

    题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...

  9. 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...

  10. 424. Longest Repeating Character Replacement

    以最左边为开始,往右遍历,不一样的个数大于K的时候停止,回到第一个不一样的地方,以它为开始,继续.. 用QUEUE记录每次不一样的INDEX,以便下一个遍历开始, 从左往右,从右往左各来一次..加上各 ...

随机推荐

  1. 取消myeclipse自动进入workspace

    进入到C:\Program Files\MyEclipse 6.5\eclipse\configuration\.settings目录, 修改SHOW_WORKSPACE_SELECTION_DIAL ...

  2. python_基于反射模拟Web框架路由系统

    根据用户输入的内容,导入模块 #根据用户输入的内容,导入模块 inp = input("请输入模块名: ") print(inp,type(inp)) dd = __import_ ...

  3. Windows服务器学习篇:服务器连接与退出

    此文是我早期在公司内部发布的一篇给予新入职程序员基础技术培训的文章,非常基础简单,现拿出来给大家分享.当然,已工作人士可直接忽略... 一.Windows服务器连接 1. 在桌面菜单中的“运行”里,输 ...

  4. (6) go 流程控制

    一. if else (1)如果只有一条语句,大括号不能省略 (2)右括号 和 else 在一行 (3)支持 if 时可以定义变量 (4)if 的风格尽量不要加括号,用空格代替 (5)多分支 二.sw ...

  5. Java网络编程一

    1.InetAddress的应用 import java.util.List; import java.math.BigDecimal; import java.net.InetAddress; im ...

  6. CF1025B Weakened Common Divisor【数论/GCD/思维】

    #include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...

  7. Ajax使用进阶

    关于Ajax的概念不再做解释了,我想通过三个小例子来让大家对Ajax有个清晰的认识.要学习它,必须从最基础最原始的方式开始认识,然后通过使用框架来提升效率,逐步认识它. 一.原生js版(注册的用户名是 ...

  8. Luogu P2016 战略游戏(树形DP)

    题解 设\(f[u][0/1/2]\)表示当前节点\(u\),放或不放(\(0/1\))时其子树满足题目要求的最小代价,\(2\)表示\(0/1\)中的最小值. 则有: \[ f[u][0]=\sum ...

  9. 洛谷——P2095 营养膳食

    题目描述 Mr.L正在完成自己的增肥计划. 为了增肥,Mr.L希望吃到更多的脂肪.然而也不能只吃高脂肪食品,那样的话就会导致缺少其他营养.Mr.L通过研究发现:真正的营养膳食规定某类食品不宜一次性吃超 ...

  10. Linux命令之useradd

    useradd [选项] LOGIN(登录名) useradd –D useradd –D [选项] 创建一个新用户或更新默认新用户信息.useradd和adduser命令相同,adduser是use ...