classSolution { public: boolisValid(string word){ if (word.length() < 3) { returnfalse; } bool f[2]{}; for (char c : word) { if (isalpha(c)) { c = tolower(c); f[c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'] = true; } elseif (!isdigit(c)) { returnfalse; } } return f[0] && f[1]; } };
B
非常合理的哈希做法,就是这样做的啦
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public:
intminimumOperationsToMakeKPeriodic(string word, int k){ unordered_map<string,int> m; int n=word.length(); //每k个字符分成一段,统计各段字符串出现的次数 for(int i=0;i<n;i+=k){ string tmp=word.substr(i,k); m[tmp]++; } //让其中出现次数最多的字符串不变,其余的都变成该字符串 auto x=max_element(m.begin(),m.end(),[](const pair<string,int>& a,const pair<string,int>& b){ return a.second<b.second; });
classSolution { public: intminCostToEqualizeArray(vector<int>& nums, int cost1, int cost2){ int n = nums.size(); int lim = 0; for (int x : nums) lim = max(lim, x);
longlong sm = 0; int mx = 0; for (int x : nums) { int det = lim - x; sm += det; mx = max(mx, det); }
constint MOD = 1e9 + 7; // 排除特殊情况 if (nums.size() <= 2 || cost1 * 2 <= cost2) return sm * cost1 % MOD;
longlong ans = 1e18; for (int i = lim; i <= lim * 2; i++) { longlong tmp; if (mx > sm - mx) { tmp = (sm - mx) * cost2; longlong rem = mx - (sm - mx); tmp += rem * cost1; } else { tmp = sm / 2 * cost2; if (sm & 1) tmp += cost1; } ans = min(ans, tmp); sm += n; mx++; } return ans % MOD; } };