今天做了三道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. myeclipse 配置weblogic 异常

    java.lang.UnsupportedClassVersionError: Bad version number in .class file当前JDK与weblogic版本不匹配.

  2. Objective-C继承

    继承只是想谈谈在OC中继承的理解:面向对象语言中一个主要的功能就是继承.继承可以使用现在类的所用功能,是对功能的扩展,通过继承创建的新类称为“子类”或“派生类”,被继承的称为“基类”或者“父类”.继承 ...

  3. 五子棋Web版的开发(三)

    最近在这个上面花费的时间不多,进展不大,而且遇到了一个问题好久也没有解决..我将struct2 改为Spring MVC.但是ziRUL的自动映射却无法起作用.   一直不知道为什么会出现这个问题.. ...

  4. HDU2018-母牛的故事

    描述: 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? 代码: 第n年的牛,等于第n-1年的牛(已有的)+第n-3年 ...

  5. CRM中的一个函数,保存一下,别系统被ぅ崩坏就麻烦了.

    CREATE OR REPLACE function UXQLCRM.GET_WEI_XIU(htfid in varchar2) ); CURSOR cr_bg_jl is select " ...

  6. 转:说说angularjs中的$parse和$eval

    说说AngularJS中的$parse和$eval AngularJS的初学者常常会对$parse和$eval两个内建服务感到有些困惑,今天我们就来说说AngularJS中的$parse和$eval. ...

  7. COB封装的优势

    随着固态照明技术的不断进步,COB(chip-on-board)封装技术得到越来越多的重视,由于COB光源有热阻低,光通量密度高,眩光少,发光均匀等特性,在室内外照明灯具中得到了广泛的应用,如筒灯,球 ...

  8. 17.1.2?Replication Formats 复制格式:

    17.1.2?Replication Formats 复制格式: 17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Ba ...

  9. 如何使用 Barracuda 防火墙设置/保护 Azure 应用程序

     如果某企业在 Windows Azure 上托管某个应用程序,该应用程序会在某个特定时间暴露到 Internet,以用于商业用途.公共 Internet 带来 客户的同时也带来了攻击者. Tim ...

  10. csv批量导入mysql命令

    今天把从Kaggle上下载下来的csv数据导入mysql,想做个统计分析,怎奈csv文件有些大.所以仅仅能用mysql 命令导入,现mark下,以备以后不时之需: 1. 导入: 基本的语法: load ...