## How does Java handle aliasing?

What is Java aliasing? Aliasing means there are multiple aliases to a location that can be updated, and these aliases have different types. In the following example, a and b are two variable names that have two different types A and B. B extends A. B[] b = new B[10]; A[] a = b;   … Read more

## LeetCode – Set Matrix Zeroes (Java)

Given a m * n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Analysis This problem should be solved in place, i.e., no other array should be used. We can use the first column and the first row to track if a row/column should be … Read more

## How to make a Web crawler using Java?

There are a lot of useful information on the Internet. How can we automatically get those information? – Yes, Web Crawler. This post shows how to make a simple Web crawler prototype using Java. Making a Web crawler is not as difficult as it sounds. Just follow the guide and you will quickly get there … Read more

## LeetCode – Validate Binary Search Tree (Java)

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. Both the … Read more

## LeetCode – Insert Interval

Problem: Given a set of non-overlapping & sorted intervals, insert a new interval into the intervals (merge if necessary). Example 1: Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9]. Example 2: Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16]. This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10]. Java Solution 1 … Read more

## ASTParser ignore embedded inner class

When using Eclipse ASTParser, if you want to get all methods and fields of the outer class and ignore the embedded inner class(es), you may try to visit all methods and fields and try to differentiate outer class and inter class.

Each class is a TypeDeclaration in AST, and Inner class is another TypeDeclaration inside the TypeDeclaration in the AST. The key question is: how to know if stuff is in a root TypeDeclaration or a inner TypeDeclaration?

## Manually Set Page Margins in Latex

One line solves the problem. This will change margin of top, bottom, left and right at the same time. More complicated usage can be found in MIT site. \usepackage[margin=1in]{geometry}\usepackage[margin=1in]{geometry} Add this line before \begin{document}

## Latex Remove Space At the Beginning of Paragraph

One line before \begin{document} solves the problem! \usepackage[parfill]{parskip}\usepackage[parfill]{parskip}

## LeetCode – Pow(x, n)

Problem: Implement pow(x, n). This is a great example to illustrate how to solve a problem during a technical interview. The first and second solution exceeds time limit; the third and fourth are accepted. Java Solution public double myPow(double x, int n){ if(n==0) return 1;   if(n<0){ return 1/helper(x, -n); }   double v = … Read more

## LeetCode – Implement strStr() (Java)

Problem: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Java Solution 1 – Naive public int strStr(String haystack, String needle) { if(haystack==null || needle==null) return 0;   if(needle.length() == 0) return 0;   for(int i=0; i<haystack.length(); i++){ if(i + needle.length() > … Read more

## Java: Convert File to Char Array

Here is the code for converting a file to a char array.

## LeetCode – Valid Parentheses (Java)

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not. Analysis A typical problem which can be solved by using a stack data structure. … Read more

## LeetCode – Merge Sorted Array (Java)

Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. Analysis The key to solve this problem is moving element … Read more

## LeetCode – Merge Two Sorted Lists (Java)

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Java Solution The key to solve the problem is defining a fake head. Then compare the first elements from each list. Add the smaller one to the … Read more