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,以便下一个遍历开始, 从左往右,从右往左各来一次..加上各 ...
随机推荐
- HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的 ...
- DataTable.DefaultView.Sort 排序方法
今天在整合一个东西,需要用到DataTable的一个排序方法, 前我是将DataTable存到DataView里面的,所以刚开始就使用了DataView.Sort="ColumnName A ...
- Grid++Report 注册
regsvr32 "D:\Grid++Report 5.0\gregn50.dll" regsvr32 "D:\Grid++Report 5.0\grdes50.dl ...
- DataSet、DataTable、DataRow的数据复制方法
DataSet 对象是支持 ADO.NET的断开式.分布式数据方案的核心对象 ,用途非常广泛.我们很多时候需要使用其中的数据,比如取得一个DataTable的数据或者复制另一个DataTabe中的数据 ...
- AC日记——红色的幻想乡 洛谷 P3801
红色的幻想乡 思路: 线段树+容斥原理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #de ...
- lr手工添加关联函数的步骤:
点击“确定”后: 如何修改已经创建好的关联规则:
- spring boot 使用thymeleaf模版 报错:org.thymeleaf.exceptions.TemplateInputException
错误: org.thymeleaf.exceptions.TemplateInputException: Error resolving template "Hello", tem ...
- php获取不到url问号之后的参数
url规则已定义: RewriteRule ^/member/editprivilege/([0-9]+) /access.php?_u=mobile/editprivilege&aid=$1 ...
- 二分查找(BinarySearch)
http://blog.csdn.net/magicharvey/article/details/10282801 简单描述 二分查找,又名折半查找,是一种在有序序列中查找特定元素的搜索算法.搜素过程 ...
- Js文件中调用其它Js函数的方法
在项目开发过程中,也许你会遇这样的情况.在某一Js文件中需要完成某一功能,但这一功能的大部分代码在另外一个Js文件中已经完成了,自己只需要调用这个方法再加上几句代码就可以实现所需的功能.我们知道,在h ...