1207. Unique Number of Occurrences
两次计数,比较出现次数是否唯一
class Solution {
public:
bool uniqueOccurrences(vector<int>& arr) {
map<int, int> m;
for (int i = 0; i < arr.size(); i++) {
m[arr[i]]++;
}
vector<bool> marked(arr.size() + 1);
for (auto p : m) {
if (marked[p.second] == true)
return false;
marked[p.second] = true;
}
return true;
}
};
1208. Get Equal Substrings Within Budget
双指针
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int nsize = s.size();
int h = 0;
int cnt = 0, re = 0;
for (int i = 0; i < nsize; i++) {
if (i > 0) {
cnt -= abs(s[i - 1] - t[i - 1]);
}
if (i > h) {
h = i;
cnt = 0;
}
while (h < nsize && cnt + abs(s[h] - t[h]) <= maxCost) {
cnt += abs(s[h] - t[h]);
re = max(re, h - i + 1);
h++;
}
}
return re;
}
};
1209. Remove All Adjacent Duplicates in String II
栈模拟
class Solution {
public:
string removeDuplicates(string s, int k) {
stack<pair<char, int>> st;
for (int i = 0; i < s.size(); i++) {
if (st.empty() || st.top().first != s[i]) {
st.push({s[i], 1});
} else {
st.push({s[i], st.top().second + 1});
}
while (!st.empty() && st.top().second == k) {
for (int i = 0; i < k; i++)
st.pop();
}
}
string re;
while (!st.empty()) {
re = st.top().first + re;
st.pop();
}
return re;
}
};
1210. Minimum Moves to Reach Target with Rotations
BFS最短路径
int paths = 0, size = 0, r = grid.size(), c = grid[0].size();
queue<array<int, 3>> q;
q.push({0,0,1});
while (!q.empty()) {
size = q.size();
while (size--) {
auto& v = q.front();
if (v[0] == r - 1 && v[1] == c - 2 && v[2] == 1 )
return paths;
if (v[2] == 1) {
marked[v[0]][v[1]] = true;
marked[v[0]][v[1] + 1] = true;
if (v[0] + 1 < r && grid[v[0] + 1][v[1]] == 0 && grid[v[0] + 1][v[1] + 1] == 0){
if(!marked[v[0] + 1][v[1]] && !marked[v[0] + 1][v[1] + 1])
q.push({v[0] + 1, v[1], 1});
if (!marked1[v[0] + 1][v[1]] && !marked1[v[0] + 1][v[1] + 1])
q.push({v[0], v[1], 0});
}
if (v[1] + 2 < c && grid[v[0]][v[1] + 2] == 0 && !marked[v[0]][v[1] + 2])
q.push({v[0], v[1] + 1, 1});
} else {
int r1 = v[0] + 1, c1 = v[1];
marked1[v[0] + 1][v[1]] = true;
marked1[v[0]][v[1]] = true;
if (v[1] + 1 < c && grid[v[0]][v[1] + 1] == 0 && grid[v[0] + 1][v[1] + 1] == 0){
if(!marked1[v[0]][v[1] + 1] && !marked1[v[0] + 1][v[1] + 1])
q.push({v[0], v[1] + 1, 0});
if (!marked[v[0]][v[1] + 1] && !marked[v[0] + 1][v[1] + 1])
q.push({v[0], v[1], 1});
}
if (v[0] + 2 < r && grid[v[0] + 2][v[1]] == 0 && !marked1[v[0] + 2][v[1]])
q.push({v[0] + 1, v[1], 0});
}
q.pop();
}
paths++;
}
return -1;
}
```
这个超时了。。。