基础的 api 还是不够熟悉啊

5112. 十六进制魔术数字

  1. class Solution {
  2. public:
  3. char *lltoa(long long num, char *str, int radix) {
  4. const char index[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  5. unsigned long long unum;
  6. int i = 0, j, k;
  7. if(radix == 10 && num < 0) {
  8. unum = (unsigned) - num;
  9. str[i++] = '-';
  10. } else
  11. unum = (unsigned long long)num;
  12. do {
  13. str[i++] = index[unum % (unsigned)radix];
  14. unum /= radix;
  15. } while(unum);
  16. str[i] = '\0';
  17. if(str[0] == '-')
  18. k = 1;
  19. else
  20. k = 0;
  21. char temp;
  22. for(j = k; j <= (i - k - 1) / 2.0; j++) {
  23. temp = str[j];
  24. str[j] = str[i - j - 1];
  25. str[i - j - 1] = temp;
  26. }
  27. return str;
  28. }
  29. string toHexspeak(string num) {
  30. long long val = atoll(num.c_str());
  31. char str[1024];
  32. lltoa(val, str, 16);
  33. num = string(str);
  34. //cout <<val <<"\n"<< str << "\n" <<num << "\n";
  35. for(int i = 0, sz = num.length(); i < sz; ++i) {
  36. if(num[i] == '0')
  37. num[i] = 'O';
  38. else if(num[i] == '1')
  39. num[i] = 'I';
  40. }
  41. string ans = num;
  42. for(int i = 0, sz = num.length(); i < sz; ++i) {
  43. if(num[i] >= 'A' && num[i] <= 'Z')
  44. continue;
  45. ans = "ERROR";
  46. }
  47. return ans;
  48. }
  49. };

一时没有想起 atollsprintf 等函数,写得很暴力。 long long 16 进制输出可用 %llX

  1. class Solution {
  2. public:
  3. string toHexspeak(string num) {
  4. long long val = atoll(num.c_str());
  5. char str[100];
  6. sprintf(str, "%llX", val);
  7. for(int i = 0, sz = strlen(str); i < sz; ++i)
  8. if(str[i] == '1')
  9. str[i] = 'I';
  10. else if(str[i] == '0')
  11. str[i] = 'O';
  12. else if(str[i] > '0' && str[i] <= '9') {
  13. string ans("ERROR");
  14. return ans;
  15. }
  16. string ans = string(str);
  17. return ans;
  18. }
  19. };

5113. 删除区间

留意区间交集是一个点的情况。

  1. #define PB push_back
  2. class Solution {
  3. public:
  4. vector<vector<int>> removeInterval(vector<vector<int>>& intervals, vector<int>& toBeRemoved) {
  5. vector<vector<int>> ans;
  6. for(vector<int> interval : intervals) {
  7. if(interval[0] >= toBeRemoved[1] || (interval[1] <= toBeRemoved[0])) {
  8. ans.PB(interval);
  9. } else if(toBeRemoved[0] < interval[0]) {
  10. if(toBeRemoved[1] >= interval[1])
  11. continue;
  12. else if(toBeRemoved[1] < interval[1])
  13. ans.PB(vector<int> {toBeRemoved[1], interval[1]});
  14. } else if(toBeRemoved[0] < interval[1]) {
  15. if(toBeRemoved[1] < interval[1]) {
  16. if(interval[0] != toBeRemoved[0])
  17. ans.PB(vector<int> {interval[0], toBeRemoved[0]});
  18. if(toBeRemoved[1] != interval[1])
  19. ans.PB(vector<int> {toBeRemoved[1], interval[1]});
  20. } else if(toBeRemoved[1] >= interval[1]) {
  21. if(interval[0] != toBeRemoved[0])
  22. ans.PB(vector<int> {interval[0], toBeRemoved[0]});
  23. }
  24. }
  25. }
  26. return ans;
  27. }
  28. };

5114. 删除树节点

  1. #define PB push_back
  2. typedef pair<int, int> PII;
  3. class Solution {
  4. public:
  5. static const int maxn = 1e4 + 7;
  6. vector<int> g[maxn];
  7. int deleteTreeNodes(int nodes, vector<int>& parent, vector<int>& value) {
  8. int root(-1);
  9. for(int i = 0; i < nodes; ++i) {
  10. if(parent[i] == -1)
  11. root = i;
  12. else
  13. g[parent[i]].PB(i);
  14. }
  15. return DFS(value, root).second;
  16. }
  17. PII DFS(vector<int>&value, int root) {
  18. PII ans({value[root], 1}); //{values,size}
  19. for(int u : g[root]) {
  20. PII sub = DFS(value, u);
  21. if(sub.first != 0)
  22. ans.first += sub.first, ans.second += sub.second;
  23. }
  24. return ans;
  25. }
  26. };

5136. 矩形内船只的数目

划分成 4 块或者 块+线 的情况。

  1. /**
  2. * // This is Sea's API interface.
  3. * // You should not implement it, or speculate about its implementation
  4. * class Sea {
  5. * public:
  6. * bool hasShips(vector<int> topRight, vector<int> bottomLeft);
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. int countShips(Sea sea, vector<int> topRight, vector<int> bottomLeft) {
  12. int ans(0);
  13. if(topRight[0] == bottomLeft[0] && topRight[1] == bottomLeft[1]) {
  14. return sea.hasShips(topRight, bottomLeft) ? 1 : 0;
  15. } else if(!sea.hasShips(topRight, bottomLeft)) {
  16. return 0;
  17. }
  18. if(bottomLeft[0] < topRight[0] && bottomLeft[1] < topRight[1]) {
  19. int mx = (topRight[0] + bottomLeft[0]) / 2, my = (topRight[1] + bottomLeft[1]) / 2;
  20. ans = countShips(sea, vector<int> {mx, my}, bottomLeft)
  21. + countShips(sea, vector<int> {mx, topRight[1]}, vector<int> {bottomLeft[0], my + 1})
  22. + countShips(sea, topRight, vector<int> {mx + 1, my + 1})
  23. + countShips(sea, vector<int> {topRight[0], my}, vector<int> {mx + 1, bottomLeft[1]});
  24. } else if(bottomLeft[0] == topRight[0]) {
  25. int my = (topRight[1] + bottomLeft[1]) / 2;
  26. ans += countShips(sea, vector<int> {bottomLeft[0], my}, bottomLeft)
  27. + countShips(sea, topRight, vector<int> {bottomLeft[0], my + 1});
  28. } else if(bottomLeft[1] == topRight[1]) {
  29. int mx = (topRight[0] + bottomLeft[0]) / 2;
  30. ans = countShips(sea, vector<int> {mx, bottomLeft[1]}, bottomLeft)
  31. + countShips(sea, topRight, vector<int> {mx + 1, bottomLeft[1]});
  32. }
  33. return ans;
  34. }
  35. };

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. 【概率论】3-9:多随机变量函数(Functions of Two or More Random Variables)

    title: [概率论]3-9:多随机变量函数(Functions of Two or More Random Variables) categories: - Mathematic - Probab ...

  2. 关于scala

    对函数式编程感兴趣了 雪下scala吧

  3. P1582 倒水,P2158 [SDOI2008]仪仗队——数学,二进制

    有n个瓶子,里面都有一升水,但是只想保留k个瓶子,只能两个瓶子里面的水体积相等时才能倒在一个瓶子里:不能丢弃有水的瓶子:瓶子容量无限: 问需要购买几个额外的瓶子才能满足条件: 因为每个瓶子一开始只有一 ...

  4. input输入框限制只能输入数字

    js: function onlyNumber(event){     var keyCode = event.keyCode;     if((keyCode<48&&keyC ...

  5. 未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0, Culture=neutral, PublicKeyToken...

    刚开始看老师 用VS新建一个“ADO.NET 实体数据模型” 但是一直报错:未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=11. ...

  6. codeforces gym #101161E - ACM Tax(lca+主席树)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给出节点数为$n$的树 有$q$次询问,输出$a$节点到$b$节点路程中,经过的边的中位数 ...

  7. Go语言 之捧腹网爬虫案例

    package main import ( "fmt" "net/http" "os" "regexp" "s ...

  8. ROS机器人开发实践学习笔记1

    刚刚开始学习ROS,打算入机器人的坑了,参考教材是<ROS及其人开发实践>胡春旭编著 机械工业出版社 华章科技出品.本来以为可以按照书上的步骤一步步来,但是,too young to si ...

  9. 【java】Java.math.BigDecimal.subtract()方法实例

    java.math.BigDecimal.subtract(BigDecimal subtrahend) 返回一个BigDecimal,其值为 (this - subtrahend), 精度为 max ...

  10. DLL:操作数据库和表

    1. 操作数据库 C(Create 创建) R(Retrieve 查询) U(Update 更新) D(Delete 删除) (1) 查询数据库 1) 查询所有数据库名称 SHOW DATABASES ...