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. ffdshow 源代码分析 2: 位图覆盖滤镜(对话框部分Dialog)

    ===================================================== ffdshow源代码分析系列文章列表: ffdshow 源代码分析 1: 整体结构 ffds ...

  2. Aho-Corasick算法学习

    1.概述 Aho-Corasick自动机算法(简称AC自动机)1975年产生于贝尔实验室.该算法应用有限自动机巧妙地将字符比较转化为了状态转移.此算法有两个特点,一个是扫描文本时完全不需要回溯,另一个 ...

  3. linux的link命令

    sudo ln -s 源文件 目标文件 sudo ln -s /usr/local/mysql/bin/mysqladmin /sbin/mysqladmin 建立软连接 ln -d existfil ...

  4. studio grandle渠道打包

    1. Mainfest 文件中添加一个键值对,这里的value 我定义为  "UMENG_CHANNEL_VALUE"(当然实际应用中可以根据自己的需要命名),后面打包的时候会对这 ...

  5. 【Qt编程】基于Qt的词典开发系列<十三>音频播放

    在上一篇文章中,我是在Qt4平台上调用本地发音的,后来由于用到JSON解析,就将平台转到了Qt5,因为Qt5自带解析JSON的类.然后发现上一篇文章的方法无法运行,当然网上可以找到解决方法,我在这里直 ...

  6. MongoDB之整库备份还原单表collection备份还原

    MongoDB之整库备份还原单表collection备份还原 cd D:\MongoDB\bin 1整库备份: mongodump -h dbhost -d dbname -o dbdirectory ...

  7. LeetCode(34)-Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. 思路: 求一个整数是不是回文树.负数不是, ...

  8. Mac OS X 简单的方法知道何时来电了

    最近本猫所在的小区时常停电,往往半夜或是凌晨才来电啊!早上起来本猫在想如何知道确切的来电时间,但又不费事的方法呢. 方法一是用手机录音器录音,因为来电后门禁会发出"滴"的一声,所以 ...

  9. ASP.NET Provider模式应用之SqlMembershipProvider类的剖析

    太多了,先给个流程图吧 Provider模式就是GOF中的两种设计模式的应用:策略模式和工厂模式,在程序中使用好这个模型能够解除模块与模块之间的耦合甚至是DIP,同时,不管是ASP.NET MVC还是 ...

  10. permutations(全排列)

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...