110. Balanced Binary Tree

Standard

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

Java Solution:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Result {
    boolean val;
    Result() {
        this.val = true;
    }
    public void setValue(boolean val) {
        this.val = val;
    }
    public boolean getValue() {
        return val;
    }
}
class Solution {
    public boolean isBalanced(TreeNode root) {
        Result isBalanced = new Result();
        getHeight(root, isBalanced);
        return isBalanced.getValue();
    }
    
    private int getHeight(TreeNode node, Result isBalanced) {
        if (node == null) return 0;
        int leftHeight = getHeight(node.left, isBalanced);
        int rightHeight = getHeight(node.right, isBalanced);
        if (Math.abs(leftHeight - rightHeight) > 1) {
            isBalanced.setValue(false);
        }
        return Math.max(leftHeight, rightHeight) + 1;
    }
}

Essentially, trying to “hack” the pass-by-reference C++ has into the Java solution. But it solves the problem in one pass.

Deploy Jupyter behind Apache reverse proxy

Standard

Jupyter was deployed on one LXD container and Apache was deployed on another. At first Jupyter kept complaining “Connecting to kernel… Failed.”

Ubuntu: 16.04LTS

Apache: Server version: Apache/2.4.18 (Ubuntu)

Solution:

Add those two lines BEFORE “/” one:

ProxyPass /api/kernels/ ws://xxx.lxd:8888/api/kernels/
ProxyPassReverse /api/kernels/ http://xxx.lxd:8888/api/kernels/

AND: make sure proxy_wstunnel is enabled for Apache:

a2enmod proxy proxy_wstunnel

Convertion of grey code and binary

Standard

Source: http://www.cnblogs.com/grandyang/p/4315607.html

以下转换代码摘自维基百科 Wikipedia:

/*
The purpose of this function is to convert an unsigned
binary number to reflected binary Gray code.

The operator >> is shift right. The operator ^ is exclusive or.
*/
unsigned int binaryToGray(unsigned int num)
{
return (num >> 1) ^ num;
}

/*
The purpose of this function is to convert a reflected binary
Gray code number to a binary number.
*/
unsigned int grayToBinary(unsigned int num)
{
unsigned int mask;
for (mask = num >> 1; mask != 0; mask = mask >> 1)
{
num = num ^ mask;
}
return num;
}

Left handed cursor for Mac

Standard

Thank bondingfortoday for this amazing method. It saves me, and it works on OS El Capitan:

Source: https://discussions.apple.com/thread/3046732

bondingfortodayApr 12, 2015 7:55 PM
Re: Left handed mouse pointerin response to sipilevi
You can use the following app called Mousecape:

https://github.com/alexzielenski/Mousecape/releases/tag/0.0.5

After downloading, installing both the app and the helper app, do the following:

  1. Go to the Mousecape preferences and choose “I am left handed”.
  2. Dump the system cursors into a cape from the menu: “Capes” → “Dump Cursors”
  3. Apply the Cursor Dump cape (right click → apply)
  4. Done!

Ghost Blog试用

Standard

今天安装好了Ghost,感觉很轻很快捷。相比Wordpress确实更佳小巧快捷,但是后台功能也就更不直接。用Wordpress可以很方便地更换主题、安装插件等,用Ghost就比较麻烦了,需要SSH过去。不过原生支持Markdown编辑器很方便。

关于macbook的wifi问题

Standard

wifi可以打开,有时候可以搜索,但是搜索到的热点不全。新建账户以后可以正常搜索,然后用parallel安装win thin以后无法正常使用,期间应该没有别的改动,所以怀疑问题出在parallel。安装苹果新pre-release的更新10.10.1后wifi可以正常使用。根据该更新介绍,该更新focus on中的项目有一个就是wifi。

目前怀疑问题出在:parallel的兼容性上

解决方法:安装10.10.1的更新

但是由于没有严格的实验,不知道问题是否真的出在parallel上。并且也不知道现在wifi恢复正常,是由于更新10.10.1时重新配置了wifi设置,还是已经彻底修复了这个问题。总之,遇到mac的wifi不能正常使用,临时解决方法是新建一个账户,使用新账户连接wifi;或者安装10.10.1更新。

操作系统:Mac OS Yosemite,10.10

型号:macbook pro with retina 2013 late,内存8G版

Yosemite开机无法登陆出现风火轮的解决方法

Standard

原文刊载于:http://bbs.feng.com/read-htm-tid-8010234.html

查明是第三方输入法引起的问题,我用的是搜狗输入法,所以把搜狗卸载就好了。(注意是卸载,不是单纯的从输入源里移除)
操作的思路是,首先要进入计算机,才能进行操作。

办法是开机进入单机模式,删除苹果一个文件,让系统认为该计算机还没有被配置,开机后直接进入配置计算机和用户的界面,新建立一个用户,就可以进入桌面删除万恶的输入法了。

首先开机的时候按 Command+s;

进入命令提示符画面后

1, 输入 sbin/mount -uaw 回车
这里注意看回车后的提示,只要不是not found之类的就可以了

2,输入 rm /var/db/.AppleSetupDone 回车
这里注意rm后面有空格。这里回车后可能无提示,如果有,只要不是not found一类的就可以了,如果老提示not found 仔细检查有没有弄错,rm前面加/试试看等等。

字母符号别打错

3,输入 reboot 回车重启

重启后配置建立完用户就进入系统了

4,下载一个搜狗输入法的DMG,打开后选择卸载搜狗输入法。

5,重启,删除刚才为了进入系统新配置的账户,天下大吉。