【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree
今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的。由于c++版的代码网上比較多。所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做LeetCode的题目还是不错的,对以后找工作面试也有帮助!
刚開始就从AC率最高的入手吧!
1.Given an array of integers, every element appears twice except
for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
開始纠结于线性时间复杂度和不要额外的存储空间。思路是遍历一遍数组。想象成翻硬币。碰到一次就翻面,再碰到就翻回来了。剩下的那个仍是背面的就是仅仅出现一次的。可是这样的思路就势必要有额外的存储空间。后来看了别人的思路,原来所有异或就能够了。由于A XOR A =0,0 XOR any = any。简单多了。
class Solution:
# @param A, a list of integer
# @return an integer
def singleNumber(self, A):
result = 0
for i in A:
result = result ^ i
return result
2.Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
递归寻找左子树和右子树的深度,取二者较大的,再加上根的深度1。即为答案。
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return an integer
def maxDepth(self, root):
if root is None:
return 0
else:
return max(self.maxDepth(root.left),self.maxDepth(root.right))+1
【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree的更多相关文章
- 【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree
解法一:递归 int maxDepth(TreeNode* root) { if (root == NULL) ; int max_left_Depth = maxDepth(root->lef ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【LeetCode练习题】Maximum Depth of Binary Tree
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
随机推荐
- ios sdk的制作
制作sdk的主要目的是将自己的code通过接口提供给其他应用使用.接下来介绍.a 静态库的制作注意事项: 1.首先.a文件的静态库要进行随时的测试,因此需要将其放入应用中(创建一个应用,再创建一个.a ...
- Android应用开发基础篇(16)-----ScaleGestureDetector(缩放手势检测)
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/11/2390274.html 一.概述 ScaleGestureDetector这个类是专门用来 ...
- BestCoder Round #46
1001 YJC tricks time 题目链接:1001 题意:给你时针和分针所成的角度,输出现在的时间,以10秒为单位 思路:每10秒,分针走1度,时针走分针的1/12,我们可以根据时间来分别计 ...
- ASP.NET内核几大对象、ASP.NET核心知识(6)
描述 其实今天的博文,是一般处理程序的后续部分,理论上应该叫一般处理程序().但是觉得文章标题后面的系列名已经有个数字,再加一个2有点怪. 这篇博文主要介绍一下几个对象. )HttpContext ) ...
- WCF服务
一.新建windows服务 二.新建wcf服务 三.添加安装程序 四.设置安装程序 五.设置启动代码 6 7 8. 注:当使用管理员身份 安装不成功时,可以使用vs自带的命令工具 ...
- servlet三种实现方式之一实现servlet接口
servlet有三种实现方式: 1.实现servlet接口 2.继承GenericServlet 3.通过继承HttpServlet开发servlet 第一种示例代码如下(已去掉包名): import ...
- 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。
python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...
- 为Chrome添加https搜索 自定义地址栏搜索引擎
转载 http://www.appinn.com/chrome-search-https-google/ 还可以参考一下 ://www.cnblogs.com/iftreasure/archive/2 ...
- 自己动手写List集合(C#)
平时经常使用微软的List集合,觉得理所应当,这阵子突然意识到学编程学这么久,总不能只生存在某个平台某种语言下面.我觉得要跳出这个框,而数据结构是经常用到的,所以呢,作为一个有志向的程序员应该学会它. ...
- PHP查看在线服务器与本地服务器支持函数差别
在本地开发好的程序.代码,当传到服务器后,经常会出现罢工的事情.这时候就要考虑开发环境和在线的环境是否完全一致了.这里所说的一致性,包括了容器的配置.php的配置等等.下面所要解决的只是其中一项,你的 ...