首页 文章 精选 留言 我的

精选列表

搜索[leetcode],共441篇文章
优秀的个人博客,低调大师

【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

原题 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题目大意 给定一棵两叉树求树的最小深度。 解题思路 遍历法,对整个树进行遍历,找出最小的深度。 代码实现 树结果定义 public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } 1 2 3 4 5 6 算法实现类 public class Solution { private int min = Integer.MAX_VALUE; // 记录树的最小深度 private int cur = 0; // i当前处理的树的尝试 public int minDepth(TreeNode root) { depth(root); return min; } /** * 计算树的深度 * * @param node 当前结点 */ private void depth(TreeNode node) { if (node == null) { min = cur; return; } cur++; // 当前处理的层次加1 // 如果是叶节点,并且路径比记录的最小还小 if (node.left == null && node.right == null && cur < min) { min = cur; // 更新最小值 } // 处理左子树 if (node.left != null) { depth(node.left); } // 处理右子树 if (node.right != null) { depth(node.right); } cur--; // 还原 } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 /** * 计算树的深度 * * @param node 当前结点 */ private void depth (TreeNode node) { if (node == null ) { min = cur; return ; } cur++; // 当前处理的层次加1 // 如果是叶节点,并且路径比记录的最小还小 if (node.left == null && node.right == null && cur < min) { min = cur; // 更新最小值 } // 处理左子树 if (node.left != null ) { depth(node.left); } // 处理右子树 if (node.right != null ) { depth(node.right); } cur--; // 还原 }}

优秀的个人博客,低调大师

LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words in a String

公众号:爱写bug(ID:icodebugs) 翻转字符串里的单词 Given an input string, reverse the string word by word. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 2: 输入: " hello world! " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。 示例 3: 输入: "a good example" 输出: "example good a" 解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。 说明: 无空格字符构成一个单词。 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。 进阶: 请选用 C 语言的用户尝试使用 O(1) 额外空间复杂度的原地解法。 Note: A word is defined as a sequence of non-space characters. Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces. You need to reduce multiple spaces between two words to a single space in the reversed string. Follow up: For C programmers, try to solve it in-place in O(1) extra space. 解题思路: Java 字符串不支持运算符重载,无法用原地解法。 我们将字符串转为字符型数组并用两个指针来解这道题。指针 i 作为原字符串转为字符数组的索引,从右向左移。指针 j 作为新字符数组索引,从左向右赋值得到原数组 count 长度的字符。count记录遇到的字母数量,每次遇到 空格 字符,新数组得到从该空格字符 向右 count 个字符并刷新count 计数。 Java: class Solution { public String reverseWords(String s) { if (s.length()==0)return s;//如果为空直接返回 char strs[]=s.toCharArray(),ans[]=new char[s.length()];//字符串转为char字符数组 int count=0,j=0;//全局变量j记录新数组索引 for(int i=s.length()-1;i>=0;i--){指针i从右向左遍历strs字符 if(strs[i]==' '){//判断是否为空格字符 int k=i+1; if(count>0){ while (--count>=0){//从字符i向右count个字符赋给新数组ans ans[j++]=strs[k++]; } ans[j++]=' '; count=0;//count初始化为0 } }else if(i==0){ for(;i<=count;i++)ans[j++]=strs[i];//左移到第一个字符时证明不是以空格开头,则从0获取count+1个个字符赋给ans j+=1; break; } else { count++;//如果是字母,则count累加1 } } if(j<1)return "";//如果j依然是0,则原字符串全为空格,返回空字符串 String string=String.valueOf(ans,0,j-1);//char数组转为字符串返回 return string; } } 为了考虑性能,转成了多个判断,所以有些繁琐。最终运行:Your runtime beats 99.91 % of java submissions Python3: python完全可以实现Java的思路,不再复现。这里利用函数投机取巧: split() ,它可以把传入字符串剔除空格后返回 所有单词的数组 join() ,它可以指定一个数组以特定字符为间隔,拼接成一个字符串 加上 [::-1] 反转数组,一行代码既可实现该题目要求 ' abc def ' 原字符串 ['abc' , 'def'] 剔除空格返回String型单词数组 ['def' , 'abc'] 切片反转数组 'def abc' 拼接成字符串 class Solution: def reverseWords(self, s: str) -> str: return " ".join(s.split()[::-1]) # 剔除所有空格字符返回数组并反转,以空格为间隔把数组拼成字符串

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册