545. Boundary of Binary Tree
题目:Recursion

  • 给定一个树形结构,返回 [ 根节点,左边界(为一个list),从左到右的所有叶子结点,及反序右边界] 的concatenation
  • 左边界中的节点包括:
    1. 根节点的左儿子。若根节点无左儿子,左边界中无节点
    1. 左边界中节点的左儿子,或无左儿子的节点的右儿子
    1. 须删掉最左的叶节点。* 由于左边界为沿着一棵树的最左走到底,遇到的第一个也是唯一一个叶节点即为最左的叶节点
  • 右边界定义同理
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):        def boundaryOfBinaryTree(self, root):""":type root: TreeNode:rtype: List[int]"""def findLeaves(node):if (not node.left and not node.right):if (node!=root):res.append(node.val)returnif (node.left):findLeaves(node.left)if (node.right):findLeaves(node.right)def findLeftB(res, node):if (node==root and not root.left):returnif (node!=root):res.append(node.val)if (not node.left and not node.right):res.pop() #remove leftmost leafreturnif (node.left):findLeftB(res, node.left)elif (node.right):findLeftB(res, node.right)def findRightB(res, node):if (node==root and not root.right):returnif (node!=root):res.append(node.val)if (not node.left and not node.right):res.pop()returnif (node.right):findRightB(res, node.right)elif (node.left):findRightB(res, node.left)res = [root.val]findLeftB(res, root)findLeaves(root)idx = len(res)findRightB(res, root)a = res[idx:]a.reverse()res = res[0:idx]+areturn res

915. Partition Array into Disjoint Intervals
题目大意:给定数组nums = [5,0,3,8,6],划分成两个subarray: left & right使得max{left} <= min{right}
思路: 顺序记录leftmax,倒序记录minright,从左到右找到第一个满足max{left} <= min{right}的划分点

class Solution {public int partitionDisjoint(int[] nums) {int N = nums.length;int[] maxL = new int[N];int[] minR = new int[N];maxL[0] = nums[0];for (int i=1; i<N; i++) {maxL[i] = Math.max(maxL[i-1], nums[i]);//System.out.println(maxL[i]);}minR[N-1] = nums[N-1];for (int i=N-2; i>=0; i--) {minR[i] = Math.min(minR[i+1], nums[i]);//System.out.println(minR[i]);}int idx = 0;while (idx<N && maxL[idx]>minR[idx+1]) {idx++;}return idx+1;}
}

1647. Minimum Deletions to Make Character Frequencies Unique
先数频次,对频次排序,hashset记录distinct frequency,若频次出现重复则尝试删除,* 忽略频次0的重复出现

class Solution {public int minDeletions(String s) {int[] count = new int[26];char[] chars = s.toCharArray();for (char c : chars) {count[c-'a']++;}Arrays.sort(count);int idx = 0;while (count[idx]==0) idx++;int ans = 0;HashSet<Integer> freq =new HashSet<Integer>();while (idx<26) {int t = count[idx];if (!freq.contains(t)) {freq.add(t);} else {while (t>0) {if (!freq.contains(t)) break;t--;}if (t!=0) freq.add(t);ans += count[idx] - t;//System.out.println(ans);}idx++;}return ans;}
}

722 Remove Comments

  • 题目描述
    • 从C++代码中删除注释,多行"/* … */",单行"//"。注意:
    1. “/* … */” 中的 “//” 不起作用 ,同理 同一行中"//" 后的 “/*” 不起作用
    1. 特殊情况!!"/*/" 作为多行注释的开始并不能结束多行注释,但作为多行注释的结尾可以结束多行注释(故只能在开始时跳过"/*/", 而不能在判断closing的时候处理该coner case)
    1. 空字符串不应被加入答案
    1. 删掉多行注释的开头行和结束行的内容应该被合并, 即[“a/.", "…/b”]需要被合并为[“ab”]而非 [“a”, “b”]
  • 最好使用while循环而非for循环来控制index,因为需要连续跳过注释的内容
class Solution {public List<String> removeComments(String[] source) {List<String> ans = new ArrayList<>();int flag1 = 0, flag2=0; // flag1 for /*, flag2 for //, 1 = existStringBuilder sb = new StringBuilder();for (String s : source) {char[] chars = s.toCharArray();int N = chars.length;int i=0;while (i<N) {if (flag1 == 1) { // in /*if (chars[i]=='*' && i+1<N && chars[i+1]=='/') {flag1 = 0;i+=2;continue;}} else {//not in /*if (chars[i]=='/') { if (i+1<N && chars[i+1]=='*') { //start an /*flag1 = 1;i+=2;if (i<N && chars[i]=='/') i++; //corner case /*/continue;}if (i+1<N && chars[i+1]=='/') { //start an // : jump to the end of the linei=N;continue;}}}if (flag1==0) {sb.append(chars[i]);}i++;}String str = sb.toString();if (!str.equals("") && flag1==0) {ans.add(str);        //only add and refresh when block comment is closedsb = new StringBuilder();}//System.out.println(str);  }return ans;}
}

2021-07-01 Leetcode题解:545,915,1647,722相关推荐

  1. Neovim开发环境搭建(2021.07.01)

    Neovim开发环境搭建(2021.07.01) 一.搭建环境 Ubuntu 21.04 Neovim 0.4.4 二.Neovim安装 # 下载 neovim,如遇网络问题可以采用 https:// ...

  2. 项目实训2021.07.01

    学习Flask框架. 参考:https://blog.csdn.net/weixin_43778491/article/details/86661285

  3. 使用Go开发的数字书架应用 | Gopher Daily (2021.07.05) ʕ◔ϖ◔ʔ

    每日一谚:API consumers: if it is not part of the contract, don't depend on it. Go技术生态 Myreads:一个使用Go.Rea ...

  4. Doris Weekly FAQ】2021.07.19~2021.08.01

    观众朋友们: 晚上好! 欢迎收看[ Doris 近日要闻]~本次为您带来的是 2021年07月19日 - 2021年08月01日 的双周总结. Doris 社区周报每期会包含 FAQ 环节.我们会在社 ...

  5. 2021.07.16 总结

    2021.07.16 总结 ​ 今天状态不怎么好,几道那么容易的题就只有140分,毕竟也就打了前两道 T1 花生采摘 题目描述 鲁宾逊先生有一只宠物猴,名叫多多.这天,他们两个正沿着乡间小路散步,突然 ...

  6. 2021.07.16【普及组】模拟赛C组

    2021.07.16[普及组]模拟赛C组 文章目录 2021.07.16[普及组]模拟赛C组 前言 花生采摘 题目 解析 代码 FBI树 题目 解析 代码 火星人 题目 解析 代码 麦森数 题目 解析 ...

  7. 900 多道 LeetCode 题解,这个 GitHub 项目值得 Star!

    转自 | 码农有道 大家好,我是小 G. 周末风和日丽,适合刷 LeetCode 今天给你们推荐个 GitHub 项目,里面收集了 900 多道 LeetCode 题解,并包含中英文两个版本,适合大多 ...

  8. PHP版Leetcode题解开始随缘更新

    2019独角兽企业重金招聘Python工程师标准>>> PHP版Leetcode题解 我叫skys215,是一名bug工程师. 我接触编程的时间比较早,但是因为我数学不好加上比较懒, ...

  9. [LeetCode 题解]: Binary Tree Preorder Traversal

    前言 [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a binary ...

  10. LeetCode 题解汇总

    为什么80%的码农都做不了架构师?>>>    LeetCode 题解汇总 转载于:https://my.oschina.net/michao/blog/801863

最新文章

  1. 学计算机一定要加班吗,老梁推荐:毕业后经常加班的几类专业,工作压力大,对能力要求很高...
  2. python graph_python graph什么意思
  3. python接口测试面试题及答案_100道接口测试面试题收好了!【建议收藏】
  4. 智能运维监管系统终端_城市轨道交通关键设备智能运维系统初步建构
  5. 学习笔记:Zookeeper 应用案例(上下线动态感知)
  6. linux下的a.out文件
  7. ByteBuffer常用方法详解
  8. TIBCO BusinessWorks 6和Container Edition与BW5的比较
  9. 在Java 8中进行投射(还有其他功能?)
  10. Web Worker
  11. [转载]如何高效、轻松地利用一天?
  12. 深度残差网络(ResNet)详解与实现(tensorflow2.x)
  13. 本地java源代码上传码云
  14. WORD文档插入页码时有几页不显示不显示页码?怎么解决
  15. macOS 10.14配置APUE环境
  16. debian 7 安装 rz sz lrzsz
  17. Wifi文件传输项目总结
  18. 免费在线pdf转换成word转换器
  19. 用C语言解9×9数独 (亲测可用)(思路分析)
  20. jQuery.ajax 使用点滴

热门文章

  1. iphone阅读模式翻页_iPad的safari浏览器阅读模式如何翻页
  2. 几种PHP实现网页抓取的程序代码
  3. 《安富莱嵌入式周报》第268期:2022.05.30--2022.06.05
  4. 对数几率回归(Logistic Regression)总结
  5. 星云日记是什么?流量共享,一键解决卖货难题
  6. mysql 1356错误_MySQL ERROR 1356 (HY000)
  7. echart 折线从左到右动画效果_echarts多条折线图动态分层的实现方法
  8. QQ隐藏福利一------------------------文件极速下载
  9. oracle10g http server HTMLDB
  10. 名帖296 傅山 行书《行书帖选》