longest-repeating-character-replacement(难)
用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(难)的更多相关文章
- Leetcode: Longest Repeating Character Replacement && G 面经
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- G 面经 && Leetcode: Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- LeetCode——Longest Repeating Character Replacement
1. Question Given a string that consists of only uppercase English letters, you can replace any lett ...
- leetcode424 Longest Repeating Character Replacement
""" Given a string s that consists of only uppercase English letters, you can perform ...
- [LeetCode] Longest Repeating Character Replacement 最长重复字符置换
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- LeetCode 424. Longest Repeating Character Replacement
原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...
- 【leetcode】424. Longest Repeating Character Replacement
题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...
- 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...
- 424. Longest Repeating Character Replacement
以最左边为开始,往右遍历,不一样的个数大于K的时候停止,回到第一个不一样的地方,以它为开始,继续.. 用QUEUE记录每次不一样的INDEX,以便下一个遍历开始, 从左往右,从右往左各来一次..加上各 ...
随机推荐
- Myeclipse实用快捷键总结
alt+shift+J 为选中的类/方法添加注释 ctrl+T 显示选中类的继承树 ctrl+shift+X/Y 将选中的字符转换为大写/小写 ctrl+shift+R 打开资源 ctrl+shift ...
- Maximum Subarray——经典
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 175. Combine Two Tables
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- qtp录制时间控件不允许用户手动输入的解决办法
qtp录制时间控件不允许用户手动输入的解决办法 [前面的话] 一边学习qtp,一边用自己的项目试着写代码,而遇到一个问题就会让自己卡壳很久,这次也是这样的,在写好了登录代码以后,自己就试着写第一个预订 ...
- OpenStack 认证服务 KeyStone 服务注册(六)
一)检查keystone是否安装配置成功 1.1删除环境变量的配置 unset OS_AUTH_URL redhat 1.2 请求令牌认证 admin用户,请求认证令牌 openstack --os- ...
- ubuntu安装过程记录
[DNS修改] 新下载的ubuntu 17.04 安装后DNS是指向谷歌DNS的,谷歌被屏蔽啦,所以无法解析域名.解决办法: ctrl+alt+t 启动终端 : sudo su 输入管理員密碼,或去 ...
- POJ 2031 Building a Space Station【最小生成树+简单计算几何】
You are a member of the space station engineering team, and are assigned a task in the construction ...
- Noip2015提高组解题报告
Day1 T1神奇的幻方 一道简单异常的小模拟,我们只需要确定数字1的位置,然后根据题意枚举即可,简简单单就A了,什么也不卡. 然而这题,我刚开始学OI的时候,因为当时比较蠢,被这题花式吊打啊.... ...
- 面向对象编程课程(OOP)第二单元总结
一.设计策略 第一次作业(傻瓜式电梯): 由于是第一次写多线程作业,许多的知识还处在理论阶段,所以第一次作业写得非常的朴实无华.整个程序总共有四个类,Main类负责通过电梯类实例化一个电梯,然后通过w ...
- Flask实战第57天:UEditor编辑器集成以及配置上传文件到七牛
相关链接 UEditor:http://ueditor.baidu.com/website/ 下载地址:http://ueditor.baidu.com/website/download.html# ...