longest-repeating-character-replacement(难)
生活随笔
收集整理的這篇文章主要介紹了
longest-repeating-character-replacement(难)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
用sliding window的方法,之前還有個(gè)k不同元素好像也是類(lèi)似的思路。有時(shí)間可以去復(fù)習(xí)下。
https://leetcode.com/problems/longest-repeating-character-replacement/// sliding windowpublic class Solution {public int characterReplacement(String s, int k) {int[] count = new int[26];int maxch = -1; // the char with max count in current substrint max = 0; // the max count for single char in current substrint len = 0; // current substr lengthint ret = 0; // final resultfor (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;} }?
總結(jié)
以上是生活随笔為你收集整理的longest-repeating-character-replacement(难)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 今日头条APP如何开通加油包
- 下一篇: 面向物联网的 23 个开源软件项目