7020. 统计对称整数的数目 - 力扣(LeetCode)
思路:转化为字符串后枚举
class Solution { public: int countSymmetricIntegers(int low, int high) { int res = 0; for(int i = low; i <= high; i ++ ) { string s = to_string(i); if(s.size() % 2) continue; int pre = 0, suf = 0; for(int i = 0; i < s.size() / 2; i ++ ) pre += s[i] - 'a'; for(int i = s.size() / 2; i < s.size(); i ++ ) suf += s[i] - 'a'; res += (pre == suf); } return res; } };
8040. 生成特殊数字的最少操作 - 力扣(LeetCode)
思路:枚举,rfind()
class Solution { public: int minimumOperations(string num) { int n = num.size(); auto f = [&](string tail) { int i = num.rfind(tail[1]); if(i == string::npos || i == 0) return n; i = num.rfind(tail[0], i - 1); if(i == string::npos) return n; return n - i - 2; }; return min({n - (num.find('0') != string::npos), f("00"), f("25"), f("50"), f("75")}); } };