258. Add Digits

Digit root 数根问题

/**
* @param {number} num
* @return {number}
*/
var addDigits = function(num) {
var b = (num-1) % 9 + 1 ;
return b;
}; //之所以num要-1再+1;是因为特殊情况下:当num是9的倍数时,0+9的数字根和0的数字根不同。

性质说明

1.任何数加9的数字根还是它本身。(特殊情况num=0)

       小学学加法的时候我们都明白,一个数字加9,就是把十位加1,各位减1。因此十位加个位的和是不变的;如果有进位,即十位上是9,那么进位之后十位会变成0,百位会加1,道理和一个一位数加9是一样的。

2.9乘任何数字的数字根都是9。

      同样是小学时学乘法时,我们在计算一位数乘九的时候,把十只手指头排开,乘几便弯下第几只手指头,前后的手指个数便是那个结果。它的数字根永远是10-1=9。多位数的化,拆分每一位数字即可。
 

104. Maximum Depth of Binary Tree

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if(root===null){
return 0;
}
var le = maxDepth(root.left);
var ri = maxDepth(root.right); return 1+Math.max(le,ri); };

【注】custom test里面的binary tree visualizer使用数组产生二叉树,如[1,2,3,4,5]

      这题考察二叉树深度的计算,使用遍历完成,从底层return0不断+1到初始位置完成计算


226. Invert Binary Tree

题目描述里面的这句话笑了- -: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if(root===null){
return root
} var tem = root.left;
root.left = root.right;
root.right = tem;
invertTree(root.left);
invertTree(root.right); return root
};

【注】这题和上面的二叉树深度的题有点像,一下就做出来了,没什么好说的

LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree的更多相关文章

  1. [LeetCode]题解(python):104 Maximum Depth of Binary Tree

    题目来源 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maxim ...

  2. [LeetCode&Python] Problem 258. Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  3. LN : leetcode 258 Add Digits

    lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...

  4. 258. Add Digits(C++)

    258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...

  5. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  6. 104. Maximum Depth of Binary Tree(C++)

    104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...

  7. <LeetCode OJ> 226. Invert Binary Tree

    226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...

  8. Python解Leetcode: 226. Invert Binary Tree

    leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...

  9. 【LeetCode】258. Add Digits (2 solutions)

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

随机推荐

  1. LAV Filter 源代码分析 3: LAV Video (1)

    LAV Video 是使用很广泛的DirectShow Filter.它封装了FFMPEG中的libavcodec,支持十分广泛的视频格式的解码.在这里对其源代码进行详细的分析. LAV Video ...

  2. Linux - 动态(Dynamic)与静态(Static)函数库

    首先我们要知道的是,函式库的类型有哪些?依据函式库被使用的类型而分为两大类,分别是静态 (Static) 与动态 (Dynamic) 函式库两类. 静态函式库的特色: 扩展名:(扩展名为 .a)   ...

  3. Linux - 延伸正则表达式

    RE 字符 意义与范例 + 意义:重复『一个或一个以上』的前一个 RE 字符 范例:搜寻 (god) (good) (goood)... 等等的字串. 那个 o+ 代表『一个以上的 o 』所以,底下的 ...

  4. COSO企业风险管理框架2017版发布!看看有哪些变化?

    近期,COSO发布了新版(2017版)的企业风险管理框架:<企业风险管理—与战略和业绩的整合>.相较于2004年发布的上一版框架<企业风险管理—整合框架>,新框架强调了制定战略 ...

  5. Google Bigtable (中文版)

    http://dblab.xmu.edu.cn/post/google-bigtable/ Abstract BigTable是一个分布式存储系统,它可以支持扩展到很大尺寸的数据:PB级别的数据,包含 ...

  6. 解决ubuntu unity下gvim菜单消失的问题

    #问题描述:在终端下用gvim 指令打开 gvim就不显示菜单.在不启用unity的桌面环境下用终端打开gvim是有菜单的.从程序菜单中打开gvim是显示菜单的.用sudo打开gvim也可以显示菜单, ...

  7. Android两级导航菜单栏的实现--FragmentTabHost结合ViewPager与Android 开源项目PagerSlidingTabStrip

    http://www.cnblogs.com/aademeng/articles/6119737.html 转载注:简单总结一下,外层Tab用TabHost,类层Tab用Viepager+Framen ...

  8. laravel项目使用twemproxy部署redis集群

    twemproxy是twitter开发的一个redis代理proxy,Twemproxy可以把多台redis server当作一台使用,开发人员通过twemproxy访问这些redis servers ...

  9. Niop2017初赛滚粗记

    初赛踢蹬滚粗 TOT (╯°Д°)╯︵┻━┻ ヽ(`Д´)ノ︵ ┻━┻ ┻━┻ 排序啊排序,净是排序,自打我学了C++就再没学过排序!!wtf! (╯°Д°)╯︵ /(.□ . )我tm怎么知道建国那 ...

  10. FileReader读取本地文件

    FileReader是一种异步读取文件机制,结合input:file可以很方便的读取本地文件. 一.input:type[file] file类型的input会渲染为一个按钮和一段文字.点击按钮可打开 ...