基础的 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. JIRA恢复备份后无法上传附件

    1.在恢复JIRA 备份数据和附件后,上传附件失败,这一般是恢复附件时没有修改附件的拥有者和组 创建JIRA平台,会自动创建一个服务器的账户,如果是服务器第一次部署JIRA那么账户肯定是jira,如果 ...

  2. html 刷新更新背景图

    需求:每次刷新页面,随机获取背景图 实现方式: 1 通过js动态生成标签 <body> <script type="text/javascript"> va ...

  3. intle官方手册下载

    如题:https://software.intel.com/en-us/articles/intel-sdm#three-volume 方便哪些不会怎么搜索的同学吧. 链接: https://pan. ...

  4. python网络爬虫(一):网络爬虫的定义

    网络爬虫,即Web Spider,是一个很形象的名字. 把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛.网络蜘蛛是通过网页的链接地址来寻找网页的. 从网站某一个页面(通常是首页)开 ...

  5. 异步机制 - Overlapped

    1 前面说到 GetOverlappedResult的bWait含义 GetOverlappedResult的bWait含义表示是否需要等待,如果IO还处于PENDING状态,内部大概实现是这样 hO ...

  6. QThread 线程暂停 停止功能的实现

    为了实现Qt中线程的暂停运行,和停止运行的控制功能 需要在设置两个static型控制变量. //终止原始数据单元进队出队,并清空数据. static bool stopSign; //原始数据单元队列 ...

  7. pip 安装报错

    pip3 install uwsgi 报错 Command in /tmp/pip-build-5m77h_mm/uwsgi/ yum -y install python36-devel 解决

  8. Heat map 绘图神奇

    https://study.163.com/provider/400000000398149/index.htm?share=2&shareId=400000000398149(博主视频教学主 ...

  9. 图解Python 【第十二篇】:Django 基础

    本节内容一览表: Django基础:http://www.ziqiangxuetang.com/django/django-tutorial.html 一.Django简介 Django文件介绍:ht ...

  10. docker下搭建owncloud

    在ubuntu下 搭建owncloud 用docker-compose启动,owncloud.yml文件内容 owncloud: image: owncloud: restart: always 开机 ...