258. Add Digits

Digit root 数根问题

  1. /**
  2. * @param {number} num
  3. * @return {number}
  4. */
  5. var addDigits = function(num) {
  6. var b = (num-1) % 9 + 1 ;
  7. return b;
  8. };
  9.  
  10. //之所以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

  1. /**
  2. * Definition for a binary tree node.
  3. * function TreeNode(val) {
  4. * this.val = val;
  5. * this.left = this.right = null;
  6. * }
  7. */
  8. /**
  9. * @param {TreeNode} root
  10. * @return {number}
  11. */
  12. var maxDepth = function(root) {
  13. if(root===null){
  14. return 0;
  15. }
  16. var le = maxDepth(root.left);
  17. var ri = maxDepth(root.right);
  18.  
  19. return 1+Math.max(le,ri);
  20.  
  21. };

【注】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.

  1. /**
  2. * Definition for a binary tree node.
  3. * function TreeNode(val) {
  4. * this.val = val;
  5. * this.left = this.right = null;
  6. * }
  7. */
  8. /**
  9. * @param {TreeNode} root
  10. * @return {TreeNode}
  11. */
  12. var invertTree = function(root) {
  13. if(root===null){
  14. return root
  15. }
  16.  
  17. var tem = root.left;
  18. root.left = root.right;
  19. root.right = tem;
  20. invertTree(root.left);
  21. invertTree(root.right);
  22.  
  23. return root
  24. };

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

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. 销售行业ERP数据统计分析都有哪些维度?

    场景描述 当前的企业信息化建设主要包括ERP系统.OA系统等.企业希望实现信息系统数据的整合,对企业资源进行分析汇总,方便对企业相关数据的掌控从而便于对业务流程进行及时调整监控. 但是由于系统间数据的 ...

  2. 苹果新的编程语言 Swift 语言进阶(九)--方法和下标

    一.方法 方法是与特定类型相关的函数.与属性一样,方法也包括实例方法和类型方法. 类.结构.枚举都能定义实例方法,用来封装或实现给定类型的一个实例相关的功能或特定任务. 类.结构.枚举也能定义与类型本 ...

  3. SharePoint JS感悟-js脚本

    最近有些迷恋js脚本,因为自己对Asp.net不够熟悉,又是Moss的一年级新生,不是对代码开发不感兴趣,面向对象自己也了解一些,代码也能大致读懂,个人觉得还是经验积累,作为代码开发人员,还是需要3- ...

  4. java--GUI(图形用户接口)

    转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9795435 day22 01-GUI(概述) GUI(图形用户界面) 1. GUI(Griph ...

  5. 瑞芯微RK3188摄像头相关参数的配置

  6. jQuery插件学习基础

    1.给jQuery添加全局的函数: $.zgz={  fn1:function(){ alert('我是刚设置的第一个全局函数') },fn2:function(){ alert('我是刚设置的第二个 ...

  7. Populating Next Right Pointers in Each Node(I and II)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  8. 三种Tomcat集群方式的优缺点分析

    三种Tomcat集群方式的优缺点分析 2009-09-01 10:00 kit_lo kit_lo的博客 字号:T | T 本文对三种Tomcat集群方式的优缺点进行了分析.三种集群方式分别是:使用D ...

  9. JVM笔记8-虚拟机性能监控与故障处理工具

    1.JDK命令行工具 Java开发人员肯定都知道JDK的bin目录有“java.exe”,"javac.exe"这两个命令行工具,但并非所有程序员都了解过JDK的bin目录之中其他 ...

  10. 从__acrt_first_block == header 谈起,记录dll链接不一致的问题

    最近写了一个postgresql的数据库连接池dll.写的比较随意,某个头文件如下: #pragma once #include "common.h"#include " ...