基础的 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. learning express step(十)

    when develop expree meet some errors, we show how to solve Error: No default engine was specified an ...

  2. 【概率论】4-3:方差(Variance)

    title: [概率论]4-3:方差(Variance) categories: - Mathematic - Probability keywords: - Variance - Standard ...

  3. windows中命令行窗口提权到管理员权限.windows 的 sudo

    命令行环境中获取管理员权限 第一种方法 (最爽,但是被运行的命令会被当成新进程运行,运行完成后就自动关闭了.) 把以下代码复制到记事本中保存为sudo.vbs 然后移动到PATH任意目录中,如wind ...

  4. 解决MySQL5.7在MAC下登录ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)问题

    问题描述 今天在MAC上安装完MYSQL后,MYSQL默认给分配了一个默认密码,但当自己在终端上使用默认密码登录的时候,总会提示一个授权失败的错误:ERROR 1045 (28000): Access ...

  5. c#递归读取菜单树

    1.查询菜单节点下所有子节点id List<sys_module> menus = new List<sys_module>() { }; public async Task& ...

  6. Spring Cloud Eureka(七):DiscoveryClient 源码分析

    1.本节概要 上一节文章主要介绍了Eureka Client 的服务注册的流程,没有对服务治理进行介绍,本文目的就是从源码角度来学习服务实例的治理机制,主要包括以下内容: 服务注册(register) ...

  7. ORM SQLAlchemy - 基本关系模式

    1 一对多 一个parent对多个child,一对多关系添加一个外键到child表,用于保存对应parent.id的值,引用parent.relationship()在parent中指定,引用/保存 ...

  8. IPC远程入侵

    https://mp.weixin.qq.com/s/rQxvp2Sq8E4pBn-E9-COww IPC远程入侵 黑客网络技术 4月19日 一.什么是IPC 进程间通信(IPC,Inter-Proc ...

  9. jeecg中获取用户拥有的角色的数据权限

    String roles1=""; String sql=""; //1.获取用户 TSUser user = ResourceUtil.getSessionU ...

  10. double,float,BigDecimal类型数值的操作

    float四舍五入保留两位小数 /** * float四舍五入保留两位小数 * */ public static float formatDecimal(float n) { return (Math ...