今天做了三道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的更多相关文章

  1. 【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree

    解法一:递归 int maxDepth(TreeNode* root) { if (root == NULL) ; int max_left_Depth = maxDepth(root->lef ...

  2. 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 ...

  3. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  4. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. 【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 ...

  9. 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 ...

随机推荐

  1. java 錯誤集錦.

    (1)加载驱动成功com.microsoft.sqlserver.jdbc.SQLServerException: 不支持此服务器版本.目标服务器必须是 SQL Server 2000 或更高版本.链 ...

  2. 简单天气应用开发——自定义TableView

    顺利解析JSON数据后,天气数据已经可以随意提取了,现在要做的就是建立一个简单的UI. 实况信息较为简单,几个Lable就可以解决.主要是七天天气预报有点麻烦,那是一个由七个字典构成的数组,需要提取出 ...

  3. ##DAY6 UIScrollView

    ##DAY6 UIScrollView #pragma mark ———————UIScrollView——————————— 属性: contentSize 内容滚动范围 contentOffset ...

  4. Java 重写(Override)与重载(Overload)

    1.重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写!返回值和形参都不能改变.即外壳不变,核心重写! 参数列表和返回值类型必须与被重写方法相同. 访问权限必须低于父类中 ...

  5. Java 网络编程(二) 两类传输协议:TCP UDP

    链接地址:http://www.cnblogs.com/mengdd/archive/2013/03/09/2951841.html 两类传输协议:TCP,UDP TCP TCP是Transfer C ...

  6. php内核一 一次请求与结束

    php开始 到 结束 有两个阶段 请求开始之间的初始化阶段 请求之后的结束处理阶段 开始阶段: 模块初始化 模块激活 模块初始化:    在整个SAPI生命周期内,只执行一次(apache服务器启动的 ...

  7. python质量控制

    一种编写高质量软件的方式是给代码中每个函数写测试,在开发过程中经常性的进行测试.         doctest模块可以在docstring中嵌套测试代码.例如: def average(values ...

  8. 命令自动补全模块rlcomplete

    rlcomplete定义了针对readline模块的命令自动补全函数.         当在unix平台下导入这个模块之后(前提是readline模块可用),一个Complete的实例会自动生成,并且 ...

  9. IOS 学习笔记(3) 视图UITabbarController

    1.UITabbarViewController标签试图控制器.由于标签页本就起着分类的作用,所以往往呈现的视图内容之间,可以是毫不相关的功能. UITabbarViewController仍然继承自 ...

  10. First AngularJS !

    My first angular! <html ng-app> <head> <meta charset="utf-8"> <script ...