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 ...
随机推荐
- Com组件介绍
COM组件简介 面向对象的思想难以适应这种分布式软件模型,于是组件化程序设计思想得到了迅速的发展. 按照组件化的程序设计的思想,复杂的应用程序被设计成一些小的,功能单一的组件模块,这些组件模块可以运行 ...
- ul下的li浮动,如何是ul有li的高度
此时ul展示的界面为: ①给ul加上一个样式,display:inline-block; <html> <head> <title>float</title& ...
- 018--python 函数参数、变量、前向引用、递归
目录 一.python函数的定义 二.函数参数 三.全局变量和局部变量 四.前向引用 五.递归 一.python函数的定义 python函数是对程序逻辑进行结构化或过程化的一种方法 1 python中 ...
- 数据库MySQL技术-基础知识
数据库技术: SQL,关系数据库标准 注意: 环境编码: cmd客户端是固定的gbk编码 而php网页中,是该网页文件的编码(现在主流都是utf8). mysql> set names gb ...
- android调用第三方库——第一篇 (转载)
转自:http://blog.csdn.net/jiuyueguang/article/details/9447245 版权声明:本文为博主原创文章,未经博主允许不得转载. 0:前言: 这两天一直在研 ...
- Tomcat调整JVM大小,启动闪退
Tomcat因调整过JVM运存大小,导致闪退:解决方法是: -XX:PermSize -XX:MaxPermSize 值调小些就可以了 set "JAVA_OPTS=-server -Xms ...
- 【原创】JAVA中令人眼花撩乱的数字魔法
五月的深圳空气中弥漫起初夏的味道,淡淡的,暖暖的.春日里不太张扬的阳光也掺入这股气息...(烟哥好文采!) 这天,烟哥愉快的喝着霸气芝士莓莓莓.一边东张西望,寻找着可以装13的机会.一切正如下面这张图 ...
- 更新gitignore
更新: 2017/04/26 修正windows版本下的命令 git rm -r --cached . (Windows 下的版本) 更新: 2017/06/06 mac下的命令也 ...
- TP3.2单字母函数
A方法 A方法用于在内部实例化控制器 调用格式:A(‘[项目://][分组/]模块’,’控制器层名称’) 最简单的用法: $User = A('User'); 表示实例化当前项目的UserAction ...
- Unity脚本打包android工程
using UnityEngine; using System.Collections; using UnityEditor; public class NewBehaviourScript : Ed ...