基础的 api 还是不够熟悉啊
5112. 十六进制魔术数字
class Solution {
public:
char *lltoa(long long num, char *str, int radix) {
const char index[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
unsigned long long unum;
int i = 0, j, k;
if(radix == 10 && num < 0) {
unum = (unsigned) - num;
str[i++] = '-';
} else
unum = (unsigned long long)num;
do {
str[i++] = index[unum % (unsigned)radix];
unum /= radix;
} while(unum);
str[i] = '\0';
if(str[0] == '-')
k = 1;
else
k = 0;
char temp;
for(j = k; j <= (i - k - 1) / 2.0; j++) {
temp = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = temp;
}
return str;
}
string toHexspeak(string num) {
long long val = atoll(num.c_str());
char str[1024];
lltoa(val, str, 16);
num = string(str);
//cout <<val <<"\n"<< str << "\n" <<num << "\n";
for(int i = 0, sz = num.length(); i < sz; ++i) {
if(num[i] == '0')
num[i] = 'O';
else if(num[i] == '1')
num[i] = 'I';
}
string ans = num;
for(int i = 0, sz = num.length(); i < sz; ++i) {
if(num[i] >= 'A' && num[i] <= 'Z')
continue;
ans = "ERROR";
}
return ans;
}
};
一时没有想起 atoll
、 sprintf
等函数,写得很暴力。 long long
16 进制输出可用 %llX
。
class Solution {
public:
string toHexspeak(string num) {
long long val = atoll(num.c_str());
char str[100];
sprintf(str, "%llX", val);
for(int i = 0, sz = strlen(str); i < sz; ++i)
if(str[i] == '1')
str[i] = 'I';
else if(str[i] == '0')
str[i] = 'O';
else if(str[i] > '0' && str[i] <= '9') {
string ans("ERROR");
return ans;
}
string ans = string(str);
return ans;
}
};
5113. 删除区间
留意区间交集是一个点的情况。
#define PB push_back
class Solution {
public:
vector<vector<int>> removeInterval(vector<vector<int>>& intervals, vector<int>& toBeRemoved) {
vector<vector<int>> ans;
for(vector<int> interval : intervals) {
if(interval[0] >= toBeRemoved[1] || (interval[1] <= toBeRemoved[0])) {
ans.PB(interval);
} else if(toBeRemoved[0] < interval[0]) {
if(toBeRemoved[1] >= interval[1])
continue;
else if(toBeRemoved[1] < interval[1])
ans.PB(vector<int> {toBeRemoved[1], interval[1]});
} else if(toBeRemoved[0] < interval[1]) {
if(toBeRemoved[1] < interval[1]) {
if(interval[0] != toBeRemoved[0])
ans.PB(vector<int> {interval[0], toBeRemoved[0]});
if(toBeRemoved[1] != interval[1])
ans.PB(vector<int> {toBeRemoved[1], interval[1]});
} else if(toBeRemoved[1] >= interval[1]) {
if(interval[0] != toBeRemoved[0])
ans.PB(vector<int> {interval[0], toBeRemoved[0]});
}
}
}
return ans;
}
};
5114. 删除树节点
#define PB push_back
typedef pair<int, int> PII;
class Solution {
public:
static const int maxn = 1e4 + 7;
vector<int> g[maxn];
int deleteTreeNodes(int nodes, vector<int>& parent, vector<int>& value) {
int root(-1);
for(int i = 0; i < nodes; ++i) {
if(parent[i] == -1)
root = i;
else
g[parent[i]].PB(i);
}
return DFS(value, root).second;
}
PII DFS(vector<int>&value, int root) {
PII ans({value[root], 1}); //{values,size}
for(int u : g[root]) {
PII sub = DFS(value, u);
if(sub.first != 0)
ans.first += sub.first, ans.second += sub.second;
}
return ans;
}
};
5136. 矩形内船只的数目
划分成 4 块或者 块+线
的情况。
/**
* // This is Sea's API interface.
* // You should not implement it, or speculate about its implementation
* class Sea {
* public:
* bool hasShips(vector<int> topRight, vector<int> bottomLeft);
* };
*/
class Solution {
public:
int countShips(Sea sea, vector<int> topRight, vector<int> bottomLeft) {
int ans(0);
if(topRight[0] == bottomLeft[0] && topRight[1] == bottomLeft[1]) {
return sea.hasShips(topRight, bottomLeft) ? 1 : 0;
} else if(!sea.hasShips(topRight, bottomLeft)) {
return 0;
}
if(bottomLeft[0] < topRight[0] && bottomLeft[1] < topRight[1]) {
int mx = (topRight[0] + bottomLeft[0]) / 2, my = (topRight[1] + bottomLeft[1]) / 2;
ans = countShips(sea, vector<int> {mx, my}, bottomLeft)
+ countShips(sea, vector<int> {mx, topRight[1]}, vector<int> {bottomLeft[0], my + 1})
+ countShips(sea, topRight, vector<int> {mx + 1, my + 1})
+ countShips(sea, vector<int> {topRight[0], my}, vector<int> {mx + 1, bottomLeft[1]});
} else if(bottomLeft[0] == topRight[0]) {
int my = (topRight[1] + bottomLeft[1]) / 2;
ans += countShips(sea, vector<int> {bottomLeft[0], my}, bottomLeft)
+ countShips(sea, topRight, vector<int> {bottomLeft[0], my + 1});
} else if(bottomLeft[1] == topRight[1]) {
int mx = (topRight[0] + bottomLeft[0]) / 2;
ans = countShips(sea, vector<int> {mx, bottomLeft[1]}, bottomLeft)
+ countShips(sea, topRight, vector<int> {mx + 1, bottomLeft[1]});
}
return ans;
}
};
原文地址:https://www.cnblogs.com/Forgenvueory/p/11964864.html
时间: 2024-11-08 19:15:14