Bit Manipulation
190, 338, 191
1356. Sort Integers by The Number of 1 Bits
class Solution {
public:
static int cnt1(int num) {
int cnt = 0;
while (num) {
cnt += num % 2;
num = num / 2;
}
return cnt;
}
static bool wayToSort(int i, int j) {
int cntA = cnt1(i);
int cntB = cnt1(j);
if (cntA == cntB) {
return i < j;
}
return cntA < cntB;
}
vector<int> sortByBits(vector<int>& arr) {
sort(arr.begin(), arr.end(), wayToSort);
return arr;
}
};
Math & Logic Puzzles
Rotation Matrix
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
reverse(matrix.begin(), matrix.end());
for (int i = 0; i < matrix.size(); i++) {
for (int j = i + 1; j < matrix.size(); j++) {
swap(matrix[i][j], matrix[j][i]);
}
}
}
};
13. Roman to Integer
390. Elimination Game
class Solution {
public:
int lastRemaining(int n) {
if (n == 1) {
return 1;
}
return 2*(1 + n / 2 - lastRemaining(n / 2));
}
};
2166. Design Bitset
这个根本不是位操作,但是很 tricky
class Bitset {
public:
int num;
string bitset;
int cnt1;
bool flag;
Bitset(int size) {
num = size;
cnt1 = 0;
bitset = string(num, '0');
flag = false;
}
void fix(int idx) {
if (flag == false) {
if (bitset[idx] == '0') {
bitset[idx] = '1';
cnt1++;
}
}
else {
if (bitset[idx] == '1') {
bitset[idx] = '0';
cnt1++;
}
}
}
void unfix(int idx) {
if (flag == false) {
if (bitset[idx] == '1') {
bitset[idx] = '0';
cnt1--;
}
}
else {
if (bitset[idx] == '0') {
bitset[idx] = '1';
cnt1--;
}
}
}
void flip() {
if (flag == false) {
flag = true;
} else {
flag = false;
}
cnt1 = num - cnt1;
}
bool all() {
return (cnt1 == num);
}
bool one() {
return (cnt1 != 0);
}
int count() {
return cnt1;
}
string toString() {
string result;
if (flag == false)
result = bitset;
else {
for (int i = 0; i < num; i++) {
char c = (bitset[i] == '0') ? '1' : '0';
result.push_back(c);
}
}
return result;
}
};
/**
* Your Bitset object will be instantiated and called as such:
* Bitset* obj = new Bitset(size);
* obj->fix(idx);
* obj->unfix(idx);
* obj->flip();
* bool param_4 = obj->all();
* bool param_5 = obj->one();
* int param_6 = obj->count();
* string param_7 = obj->toString();
*/
567. Permutation in String
这道题非常有趣,key idea 是 s1 每个字符的 count 在 一个 sliding window 中数量一致。注意字符种类的限制。
先统计 pattern 中各个字符的 frequency,然后一边 slide 一边 judge。
class Solution {
public:
bool checkInclusion(string s1, string s2) {
vector<int> goal(26), cur(26);
for (auto c: s1) {
goal[c - 'a']++;
}
for (int i = 0; i < s2.size(); i++) {
cur[s2[i] - 'a']++;
if (i >= s1.size()) {
cur[s2[i - s1.size()] - 'a']--;
}
if (cur == goal) {
return true;
}
}
return false;
}
};
438. Find All Anagrams in a String
Almost the same problem.
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
vector<int> pattern(26), cur(26);
vector<int> result;
for (auto c: p) {
pattern[c - 'a']++;
}
for (int i = 0; i < s.size(); i++) {
cur[s[i] - 'a']++;
if (i >= p.size()) {
cur[s[i - p.size()] - 'a']--;
}
if (pattern == cur) {
result.push_back(i - p.size() + 1);
}
}
return result;
}
};
1220. Count Vowels Permutation
Graph!
class Solution {
public:
int countVowelPermutation(int n) {
vector<vector<long>> dp(n, vector<long>(5, 0));
for (int i = 0; i < 5; i++) {
dp[0][i] = 1;
}
int MOD = 1e9 + 7;
for (int i = 1; i < n; i++) {
dp[i][0] = (dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][4]) % MOD;
dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % MOD;
dp[i][2] = (dp[i - 1][1] + dp[i - 1][3]) % MOD;
dp[i][3] = (dp[i - 1][2]) % MOD;
dp[i][4] = (dp[i - 1][2] + dp[i - 1][3]) % MOD;
}
long res = 0;
for (int i = 0; i < 5; i++) {
res = (res + dp[n - 1][i]) % MOD;
}
return res;
}
};