力扣125. 验证回文串;力扣443. 压缩字符串

力扣125. 验证回文串;力扣443. 压缩字符串

class Solution {
public:
    bool isPalindrome(string s) {
		if (s.size() == 0)
			return true;
		size_t size = s.size();
		int i = 0;
		int j = size - 1;
		int flag = 0;
		while (i <= j)
		{
			while (i <= j)
			{
				if ('a' <= s[i] && s[i] <= 'z' || 'A' <= s[i] && s[i] <= 'Z' || '0' <= s[i] && s[i] <= '9')
				{
					flag = 1;
					break;
				}
				else
					++i;
			}
			while (i <= j)
			{
				if ('a' <= s[j] && s[j] <= 'z' || 'A' <= s[j] && s[j] <= 'Z' || '0' <= s[j] && s[j] <= '9')
				{
					flag = 1;
					break;
				}
				else
					--j;
			}
		
			if (flag == 1)
			{
				flag = 0;
				if (s[i] == s[j] || s[i] == (s[j] + 32) || s[i] == (s[j] - 32))
				{
					if (('0' <= s[i] && s[i] <= '9') && ('0' > s[j] || s[j] > '9') || ('0' > s[i] || s[i] > '9') && ('0' <= s[j] && s[j] <= '9'))
						return false;
					++i;
					--j;
				}
				else
					return false;
			}
		}
		return true;
	}
};

力扣125. 验证回文串;力扣443. 压缩字符串

class Solution {
public:
    int compress(vector<char>& chars) {
	    size_t size = chars.size();
	    for (auto i = chars.begin(); i != chars.end(); ++i)
	    {
		    int count = 1;
		    auto j = i;
		    while (j + 1 != chars.end() && *j == *(j + 1))
		    {
			    count++;
			    j++;
		    }
		    if (count == 1)
		    {
		    }
		    else
		    {
			    if (count == 2)
				    *(++i) = '2';
			    else
			    {
				    string s = to_string(count);
				    for (int x = 0; x < s.size(); ++x)
				    {
					    *(++i) = s[x];
				    }
				    chars.erase(i + 1, j + 1);
			    }
		    }
	    }
	    return chars.size();
    }
};
力扣125. 验证回文串;力扣443. 压缩字符串力扣125. 验证回文串;力扣443. 压缩字符串 回不了头的卒 发布了245 篇原创文章 · 获赞 28 · 访问量 1万+ 私信 关注
上一篇:【LeetCode】 1160. Find Words That Can Be Formed by Characters 拼写单词(Easy)(JAVA)


下一篇:蓝桥杯基础练习 完美的代价