注意审题,是相同的字符,而不是相同的字符串。所以对于 abcccab 来说就是输出7
class Solution { public: int minimumLength(string s) { int left = 0,right = s.size()-1; while(left < right){ if(s[left] != s[right]) return right-left+1; while(s[left] == s[left+1] && left + 1 != right) ++left; while(s[right] == s[right-1] && left + 1 != right) --right; ++left;--right; } if(left + 1 == right && s[left] == s[right]) return 0; return right-left+1; } };
题解:进一步优化
class Solution { public: int minimumLength(string s) { int left = 0,right = s.size()-1; while(left < right && s[left] == s[right]){ char temp = s[left]; while(left <= right && s[left] == temp) ++left; while(left <= right && s[right] == temp) --right; } return right-left+1; } };
标签:right,int,while,&&,字符串,1750,leetcode,left From: https://www.cnblogs.com/uacs2024/p/18603430