赞
踩
考察:滑动窗口
#include<bits/stdc++.h> using namespace std; int maxLength(string str) { int left = 0, right = 0, charCount = 0, numCount = 0, maxCount = -1; while (right < str.size()){ if(isdigit(str[right])){ numCount++; }else{ charCount++; } if(charCount == 1 && numCount >= 1){ maxCount = std::max(maxCount, right - left + 1); } // 当窗口不满足时,使得窗口满足 while (charCount > 1){ if(isdigit(str[left])){ numCount--; }else{ charCount--; } left++; } right++; } return maxCount; } int main(int argc, char **argv) { std::cout << maxLength("abcdef"); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。