2018-06-29 22:56:24

问题描述:

问题求解:

本题是一条字符串问题,且是求Optimal,自然最初想到的是使用DP来进行求解,但是问题就是如果采用DP的话,前一个状态也太多了,和替换了多少了k值相关,因此从这个角度来说,使用DP来处理本题是不太合适的。

那么,另一个处理的手段滑动窗口就呼之欲出了。

在本题中窗口[start, end]来维护,其中只要窗口长度 - 窗口中出现字符最多的个数 <= k则当前窗口的长度符合题意,只要找出最长的符合条件的窗口即可。

问题是如果出现了上述条件不满足的情况,那么就需要减小窗口大小使之满足条件。

这里有一个trick,就是在减小窗口大小的时候是不需要对maxcnt的大小进行修改的,因为如果要得到一个比当前的res更好的解,那么势必窗口大小要增大,同时maxcnt也要增大。因此这里不需要重新遍历一遍来获得新的窗口中的maxcnt。

    public int characterReplacement(String s, int k) {
int res = 0;
int start = 0;
int[] cnt = new int[26];
int maxcnt = 0;
for (int end = 0; end < s.length(); end++) {
maxcnt = Math.max(maxcnt, ++cnt[s.charAt(end) - 'A']);
if (end - start + 1 - maxcnt > k) {
cnt[s.charAt(start++) - 'A']--;
}
res = Math.max(res, end - start + 1);
}
return res;
}

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. 远程桌面时出现身份验证错误,要求的函数不正确,这可能是由于CredSSP加密Oracle修正

    问题如下: 那么解决办法如下:

  2. 问题排查之'org.apache.rocketmq.spring.starter.core.RocketMQTemplate' that could not be found.- Bean method 'rocketMQTemplate' in 'RocketMQAutoConfiguration' not loaded.

    背景 今天将一个SpringBoot项目的配置参数从原有的.yml文件迁移到Apollo后,启动报错“Bean method 'rocketMQTemplate' in 'RocketMQAutoCo ...

  3. 关于QStandardItemModel

    类QabstractItemModel,QabstractListModel,QAbstractTableModel不保存数据,用户需要从这些类派生出子类,并在子类中定义某种数据结构来保存数据.与此不 ...

  4. C/C++之extern "C"的用法解析

    extern "C"的用法解析 http://blog.sina.com.cn/u/494a1ebc010004g5 C++中extern “C”含义深层探索 1.引言 C++语言 ...

  5. [转载]ViewState使用小结

    ViewState是.Net中提出的状态保存的一种新途径,web程序保存状态的方式有这样几种:1.Application:保存在Application中的数据是全局有效的:Application里面存 ...

  6. Hadoop MapReduce执行过程实例分析

    1.MapReduce是如何执行任务的?2.Mapper任务是怎样的一个过程?3.Reduce是如何执行任务的?4.键值对是如何编号的?5.实例,如何计算没见最高气温? 分析MapReduce执行过程 ...

  7. CSS 基础知识点 样式 选择器 伪类

    CSS 基础知识点汇集 版权声明:这篇博客是别人写的,大神博客地址 : https://www.cnblogs.com/Mtime/p/5184685.html 1.CSS 简介 CSS 指层叠样式表 ...

  8. Linux 环境 HTTP 服务器

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/in ...

  9. Python 技术点

    1.文件操作 1-1 遍历文件夹和文件 import os rootDir = "/path/to/root" for parent, dirnames, filenames in ...

  10. 探索Java8:(三)Predicate接口的使用

    上一篇学习了下Function接口的使用,本篇我们学习下另一个实用的函数式接口Predicate. Predicate的源码跟Function的很像,我们可以对比这两个来分析下.直接上Predicat ...