LeetCode 第 14 场双周赛
基础的 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;
}
};
LeetCode 第 14 场双周赛的更多相关文章
- LeetCode第8场双周赛(Java)
这次我只做对一题. 原因是题目返回值类型有误,写的是 String[] ,实际上应该返回 List<String> . 好吧,只能自认倒霉.就当涨涨经验. 5068. 前后拼接 解题思路 ...
- Java实现 LeetCode第30场双周赛 (题号5177,5445,5446,5447)
这套题不算难,但是因为是昨天晚上太晚了,好久没有大晚上写过代码了,有点不适应,今天上午一看还是挺简单的 5177. 转变日期格式 给你一个字符串 date ,它的格式为 Day Month Yea ...
- LeetCode 第 15 场双周赛
1287.有序数组中出现次数超过25%的元素 1288.删除被覆盖区间 1286.字母组合迭代器 1289.下降路径最小和 II 下降和不能只保留原数组中最小的两个,hacked. 1287.有序数组 ...
- LeetCode第29场双周赛题解
第一题 用一个新数组newSalary保存去掉最低和最高工资的工资列表,然后遍历newSalary,计算总和,除以元素个数,就得到了平均值. class Solution { public: doub ...
- leetcode-第11场双周赛-5089-安排会议日程
题目描述: 自己的提交: class Solution: def minAvailableDuration(self, slots1: List[List[int]], slots2: List[Li ...
- leetcode-第11场双周赛-5088-等差数列中缺失的数字
题目描述: 自己的提交: class Solution: def missingNumber(self, arr: List[int]) -> int: if len(arr) == 2: re ...
- leetcode-第五场双周赛-1134-阿姆斯特朗数
第一次提交: class Solution: def isArmstrong(self, N: int) -> bool: n = N l = len(str(N)) res = 0 while ...
- leetcode-第五场双周赛-1133-最大唯一数
第一次提交: class Solution: def largestUniqueNumber(self, A: List[int]) -> int: dict = {} for i in A: ...
- LeetCode 第 165 场周赛
LeetCode 第 165 场周赛 5275. 找出井字棋的获胜者 5276. 不浪费原料的汉堡制作方案 5277. 统计全为 1 的正方形子矩阵 5278. 分割回文串 III C 暴力做的,只能 ...
随机推荐
- 【概率论】3-9:多随机变量函数(Functions of Two or More Random Variables)
title: [概率论]3-9:多随机变量函数(Functions of Two or More Random Variables) categories: - Mathematic - Probab ...
- 关于scala
对函数式编程感兴趣了 雪下scala吧
- P1582 倒水,P2158 [SDOI2008]仪仗队——数学,二进制
有n个瓶子,里面都有一升水,但是只想保留k个瓶子,只能两个瓶子里面的水体积相等时才能倒在一个瓶子里:不能丢弃有水的瓶子:瓶子容量无限: 问需要购买几个额外的瓶子才能满足条件: 因为每个瓶子一开始只有一 ...
- input输入框限制只能输入数字
js: function onlyNumber(event){ var keyCode = event.keyCode; if((keyCode<48&&keyC ...
- 未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0, Culture=neutral, PublicKeyToken...
刚开始看老师 用VS新建一个“ADO.NET 实体数据模型” 但是一直报错:未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=11. ...
- codeforces gym #101161E - ACM Tax(lca+主席树)
题目链接: http://codeforces.com/gym/101161/attachments 题意: 给出节点数为$n$的树 有$q$次询问,输出$a$节点到$b$节点路程中,经过的边的中位数 ...
- Go语言 之捧腹网爬虫案例
package main import ( "fmt" "net/http" "os" "regexp" "s ...
- ROS机器人开发实践学习笔记1
刚刚开始学习ROS,打算入机器人的坑了,参考教材是<ROS及其人开发实践>胡春旭编著 机械工业出版社 华章科技出品.本来以为可以按照书上的步骤一步步来,但是,too young to si ...
- 【java】Java.math.BigDecimal.subtract()方法实例
java.math.BigDecimal.subtract(BigDecimal subtrahend) 返回一个BigDecimal,其值为 (this - subtrahend), 精度为 max ...
- DLL:操作数据库和表
1. 操作数据库 C(Create 创建) R(Retrieve 查询) U(Update 更新) D(Delete 删除) (1) 查询数据库 1) 查询所有数据库名称 SHOW DATABASES ...