用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. Django REST Framework JWT提供的登录签发的视图

    Django REST Framework JWT提供了一个视图.在我们登录的时候,会校验用户名.密码是否正确.如果信息无误,可以返回一个JWT token.就可以简单地实现了记录用户登录状态. 用法 ...

  2. hdu 5023

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

  3. DuplicateHandle

    功能:将一个进程内的伪句柄,转化为可以用来进程间通信的实句柄 BOOL DuplicateHandle(  HANDLE hSourceProcessHandle,  HANDLE hSourceHa ...

  4. The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack

    题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...

  5. (6) go 流程控制

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

  6. 欧拉图 欧拉回路 欧拉通路 Euler

    欧拉图 本文链接:http://www.cnblogs.com/Ash-ly/p/5397702.html 定义: 欧拉回路:图G的一个回路,如果恰通过图G的每一条边,则该回路称为欧拉回路,具有欧拉回 ...

  7. 读书笔记(高性能javascript)(二)

    5. 字符串和正则表达式: (1) 在大多数浏览器中,数组项合并(Array.prototype.join)比其他字符串连接方法更慢,但它却在IE7及更早版本浏览器中合并大量字符串唯一高效的途径: ( ...

  8. 查看所有shell类型

    [xf@xuexi ~]$ cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/no ...

  9. python 日历(Calendar)模块

    另附一篇文章:http://www.jb51.net/article/77971.htm 序号 函数及描述 1. calendar.calendar(year,w=2,l=1,c=6) 返回一个多行字 ...

  10. 安卓 应用app启动过程

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 从用户点击 Launcher 上的 App 图标,到显示出 App 界面时主要发生的事情.知晓以 ...