how to make a function from using global var to not using it
let say, we want to find the bottom left node in a tree.
one way to do it is using global vars:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution
{ public int findBottomLeftValue(TreeNode root)
{
find(root, 0);
return d_bottomLeft.val;
} private int d_bottomDepth = -1;
private TreeNode d_bottomLeft = null;
private void find(TreeNode node, int depth)
{
if (node == null) return;
if (depth >= d_bottomDepth)
{
d_bottomDepth = depth;
d_bottomLeft = node;
} find(node.right, depth + 1);
find(node.left , depth + 1);
}
}
to avoid using global vars, we can write it in this way:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution
{
public int findBottomLeftValue(TreeNode root)
{
Result result = new Result();
find(root, 0, result);
return result.d_bottomLeft.val;
} private void find(TreeNode node, int depth, Result result)
{
if (node == null) return;
if (depth >= result.d_bottomDepth)
{
result.d_bottomDepth = depth;
result.d_bottomLeft = node;
}
find(node.right, depth + 1, result);
find(node.left , depth + 1, result);
} private class Result
{
public int d_bottomDepth = -1;
public TreeNode d_bottomLeft = null;
}
}
how to make a function from using global var to not using it的更多相关文章
- 【兼容调试】cffi library '_openssl' has no function, constant or global variable named 'Cryptography_HAS
重装cryptography就好了. conda uninstall cryptography conda install cryptography https://github.com/pyca/c ...
- Fatal error: Call to undefined function Think\C() in /var/www/html/ceshi.hzheee.com/think/ThinkPHP/Library/Think/Think.class.php on line 334 这个问题解决
当APP_DEBUG为true时,包含图中这个文件,文件中又引导包含这些库文件,可以看出安装thinkphp3.2.3时ThinkPHP/Common/下是functions.php,把它改成func ...
- PHP内核探索之变量(2)-理解引用
本文主要内容: 引论 符号表与zval 引用原理 回到最初的问题 一.引论 很久之前写了一篇关于引用的文章,当时写的寥寥草草,很多原理都没有说清楚.最近在翻阅Derick Rethans(home: ...
- 面向对象的JS随笔
Scoping 全局与局部 全局变量可用在所有环境中,局部变量只可用在局部 js中连接变量至一个从未声明的变量,后面的变量自动提升成一个全局变量(不要这样用,不易阅读) 只有function(){中才 ...
- Javascript - 预编译与函数词法作用域
预编译与函数词法作用域(Precompiled & Scoped) 预编译 Javascript脚本的宿主在执行代码之前对脚本做了预编译处理,比如浏览器对Js进行了预编译,编译器会扫描所有的声 ...
- canvas 笔记整理
canvas Retina 屏幕优化 /** * HiDPI Canvas Polyfill (1.0.9) * * Author: Jonathan D. Johnson (http://jonda ...
- Global variable in ABAP function group
Function group is loaded into runtime memory by the FIRST call of a function module inside this func ...
- 全局变量:global与$GLOBALS的区别和使用
今天在写框架的时候想把SaeMySQL初始化之后作为全局变量使用.但是后来发现PHP中的全局变量和Java或者OC中的全局变量还是有较大区别的.下面记录一下php里面的global的使用相关注意事项. ...
- PHP中global与$GLOBALS['']的区别
+++ 探讨(一)+++++++++++++++++++++++++++++++++++++++ 很多人都认为global和$GLOBALS[]只是写法上面的差别,其实不然. 根据官方的解释是 $GL ...
随机推荐
- 【NOI 2005】 维修数列
[题目链接] 点击打开链接 [算法] 本题所运用的也是Splay的区间操作,但是实现较为困难 INSERT操作 将pos splay至根节点,pos+1 splay至根节点的右 ...
- Top的VIRT是什么
Top命令监控某个进程的资源占有情况 下面是各种内存: VIRT:virtual memory usage 1.进程“需要的”虚拟内存大小,包括进程使用的库.代码.数据等 2.假如进程申请1 ...
- 解决IIS中部署WCF时,访问.svc文件的404错误问题
如果你直接在IIS 7中配置WCF,访问.svc文件时会出现404错误.解决方法,以管理员身份进入命令行模式,运行:"%windir%\Microsoft.NET\Framework\v3. ...
- Bootstrap Notify
https://github.com/mouse0270/bootstrap-notify $.notify('Hello World', { offset: { x: 50, y: 100 } }) ...
- 指针 * &
int main() { ; //定义int变量updates int * p_updates; //定义指针p_updates p_updates=&updates;//将updates的地 ...
- logging日志类
#! /usr/bin/env python #coding=utf-8 import logging,os class Logger: def __init__(self, path,clevel ...
- HDU 1207 汉诺塔II (简单DP)
题意:中文题. 析:在没有第四个柱子时,把 n 个盘子搬到第 3 个柱子时,那么2 ^ n -1次,由于多了一根,不知道搬到第四个柱子多少根时是最优的, 所以 dp[i] 表示搬到第4个柱子 i 个盘 ...
- float以后设置的小细节
先看看下面这段css代码,是不是很完美?没错? #pageBodyMain .articleList a: after { content: ""; clear: both; di ...
- (水题)洛谷 - P1618 - 三连击(升级版)
https://www.luogu.org/problemnew/show/P1618 枚举所有的A,最多 $A_9^3$ ,然后生成B和C(先判断是不是能够生成),判断有没有重复数字(比之前那个优雅 ...
- bzoj 2882: 工艺【SAM】
看上去比较SA,但是在学SAM所以就用SAM来做-- 把串复制一遍接在后面,对这个新串求SAM(这里的儿子节点要用map转移),然后从根节点每次都向最小的转移走,这样走n次转移的串就是答案 #incl ...