- 5253. 找到指定长度的回文数(周赛)">5253. 找到指定长度的回文数(周赛)
- 5236. 美化数组的最少删除数(周赛)">5236. 美化数组的最少删除数(周赛)
- 954. 二倍数对数组">954. 二倍数对数组
- 区间问题
- 699. 掉落的方块(hard)">699. 掉落的方块(hard)
- 54. 螺旋矩阵(重点题目)">54. 螺旋矩阵(重点题目)
5253. 找到指定长度的回文数(周赛)
- 只需要考虑前面一半。后面使用字符串拼接即可
怎么计算多少个最大的回文数呢?最大位加1,总体减1即可。因为前导不能是0,因此最大位+1,而idx是从1开始的,因此减1
class Solution {
public:
vector<long long> kthPalindrome(vector<int>& queries, int intLength) {
vector<long long> res;
int n = queries.size();
int intLength1 = intLength%2==0? intLength/2:intLength/2+1;//最大位+1,总体减1
for(int i = 0; i < n; i++) {
long long idx = queries[i];
long long val = idx+pow(10,intLength1-1)-1;
if(val > pow(10,intLength1)-1) {//判断是否超范围
res.push_back(-1);
continue;
}
string str = to_string(val);
if(intLength%2==0) {
string str1 = str;
reverse(str1.begin(), str1.end());
str = str+str1;
} else {
string str1 = str.substr(0, str.size()-1);
reverse(str1.begin(), str1.end());
str = str+str1;
}//处理
val = stol(str);
res.push_back(val);
}
return res;
}
};
5236. 美化数组的最少删除数(周赛)
这题使用栈来写的
- 使用栈来模拟更新之后的数组
奇数要摘除栈顶
class Solution {
public:
int minDeletion(vector<int>& nums) {
int n = nums.size();
stack<int> stk;
stk.push(nums[0]);
for(int i = 1; i < n; i++){
int num = stk.top();
if(stk.size()%2==0||num!=nums[i]) {
stk.push(nums[i]);
}
}
return stk.size()%2==0? n-stk.size():n-stk.size()+1;
}
};
954. 二倍数对数组
是用哈希表来查找是否有为两倍的数,问题在于要一一对应上。
不能一开始就设定好,必须要边找边查。
class Solution {
public:
bool canReorderDoubled(vector<int> &arr) {
unordered_map<int, int> cnt;
for (int x : arr) {
++cnt[x];
}
if (cnt[0] % 2) {
return false;
}
vector<int> vals;
vals.reserve(cnt.size());//vals重置为哈希表的长度
for (auto &[x, _] : cnt) {
vals.push_back(x);
}
sort(vals.begin(), vals.end(), [](int a, int b) { return abs(a) < abs(b); });//根据绝对值进行排序
for (int x : vals) {
if (cnt[2 * x] < cnt[x]) { // 无法找到足够的 2x 与 x 配对
return false;
}
cnt[2 * x] -= cnt[x];
}
return true;
}
};
区间问题
699. 掉落的方块(hard)
判断两个区间[l1, r1],[l2, r2]是否相交
r1 >= l2&& r2>=l1
class Solution {
public:
vector<int> fallingSquares(vector<vector<int>>& positions) {
int n = positions.size();
vector<int> heights(n);//heights[i]实际上就是当前范围(i)内的最大高度
for (int i = 0; i < n; i++) {
int left1 = positions[i][0], right1 = positions[i][0] + positions[i][1] - 1;
heights[i] = positions[i][1];//边长
for (int j = 0; j < i; j++) {//暴力枚举之前的所有方块
int left2 = positions[j][0], right2 = positions[j][0] + positions[j][1] - 1;//左边起点到右边起点
if (right1 >= left2 && right2 >= left1) {//情况重叠,注意这里的写法非常重要,这种写法包含了所有的情况。
heights[i] = max(heights[i], heights[j] + positions[i][1]);
}
}
}
for (int i = 1; i < n; i++) {
heights[i] = max(heights[i], heights[i - 1]);//前面可能出现比当前还高的情况。
}
return heights;
}
};
54. 螺旋矩阵(重点题目)
怎么旋转的必须要记住!!!!
int left=0;
int right=(matrix[0].size()-1);
int top=0;
int bottom=(matrix.size()-1);
while(right>=left&&bottom>=top){
for(int i=left;i<=right;i++){
matrix[left][i] = head->val;
head = head->next;
if(!head) return matrix;
}
for(int i=(top+1);i<=bottom;i++){
matrix[i][right] = head->val;
head = head->next;
if(!head) return matrix;
}
if(right>left&&bottom>top){
for(int i=(right-1);i>=left;i--){
matrix[bottom][i] = head->val;
head = head->next;
if(!head) return matrix;
}
for(int i=(bottom-1);i>top;i--){
matrix[i][left] = head->val;
head = head->next;
if(!head) return matrix;
}
}
left++;
top++;
right--;
bottom--;
}