基础的 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;
}
};

一时没有想起 atollsprintf 等函数,写得很暴力。 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;
}
};

LeetCode 第 14 场双周赛的更多相关文章

  1. LeetCode第8场双周赛(Java)

    这次我只做对一题. 原因是题目返回值类型有误,写的是 String[] ,实际上应该返回 List<String> . 好吧,只能自认倒霉.就当涨涨经验. 5068. 前后拼接 解题思路 ...

  2. Java实现 LeetCode第30场双周赛 (题号5177,5445,5446,5447)

    这套题不算难,但是因为是昨天晚上太晚了,好久没有大晚上写过代码了,有点不适应,今天上午一看还是挺简单的 5177. 转变日期格式   给你一个字符串 date ,它的格式为 Day Month Yea ...

  3. LeetCode 第 15 场双周赛

    1287.有序数组中出现次数超过25%的元素 1288.删除被覆盖区间 1286.字母组合迭代器 1289.下降路径最小和 II 下降和不能只保留原数组中最小的两个,hacked. 1287.有序数组 ...

  4. LeetCode第29场双周赛题解

    第一题 用一个新数组newSalary保存去掉最低和最高工资的工资列表,然后遍历newSalary,计算总和,除以元素个数,就得到了平均值. class Solution { public: doub ...

  5. leetcode-第11场双周赛-5089-安排会议日程

    题目描述: 自己的提交: class Solution: def minAvailableDuration(self, slots1: List[List[int]], slots2: List[Li ...

  6. leetcode-第11场双周赛-5088-等差数列中缺失的数字

    题目描述: 自己的提交: class Solution: def missingNumber(self, arr: List[int]) -> int: if len(arr) == 2: re ...

  7. leetcode-第五场双周赛-1134-阿姆斯特朗数

    第一次提交: class Solution: def isArmstrong(self, N: int) -> bool: n = N l = len(str(N)) res = 0 while ...

  8. leetcode-第五场双周赛-1133-最大唯一数

    第一次提交: class Solution: def largestUniqueNumber(self, A: List[int]) -> int: dict = {} for i in A: ...

  9. LeetCode 第 165 场周赛

    LeetCode 第 165 场周赛 5275. 找出井字棋的获胜者 5276. 不浪费原料的汉堡制作方案 5277. 统计全为 1 的正方形子矩阵 5278. 分割回文串 III C 暴力做的,只能 ...

随机推荐

  1. mac 安装docker

    下载地址: https://download.docker.com/mac/stable/Docker.dmg 从应用中找到 Docker 图标并点击运行.可能会询问 macOS 的登陆密码,输入即可 ...

  2. libpng warning:iCCP:known incorrect sRGB profile

    原因是新版的libpng增强了检查,发出警告.此警告可以忽略.若要消除此警告则要使用v4的色彩配置.GIMP sRGB v4 色彩配置,修改当前图片的色彩配置,设为默认. sRGB profilesO ...

  3. poj 1458 Common Subsequence ——(LCS)

    虽然以前可能接触过最长公共子序列,但是正规的写应该还是第一次吧. 直接贴代码就好了吧: #include <stdio.h> #include <algorithm> #inc ...

  4. JQuery 行内编辑(即点即改)

    行内编辑 下面是详细的代码: <style> .dian { cursor: pointer; } </style> //这个让鼠标 移动到 span上 的时候 是一个小手 & ...

  5. MapReduce shuffle的过程分析

    shuffle阶段其实就是多个map任务的输出,按照不同的分区,通过网络copy到不同的reduce节点上. Map端: 1.在map端首先接触的是InputSplit,在InputSplit中含有D ...

  6. 以太坊geth区块链私链建立

      以太坊geth区块链私链建立 geth的github https://github.com/ethereum/go-ethereum 下载最新(1.8)的geth,windows下安装很简单 关于 ...

  7. CDH构建大数据平台-Kerberos高可用部署【完结篇】

    CDH构建大数据平台-Kerberos高可用部署[完结篇] 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.  一.安装Kerberos相关的软件包并同步配置文件 1>.实验环境 ...

  8. Mysql 清空数据后,释放硬盘文件

    删除表数据后执行:optimize table [表名]

  9. Editplus的运行JAVA的配置

    工具--->参数设置

  10. for-update与for-update nowait

    1.for update 和 for update nowait 的区别: 首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何 ...