Skip to content

Instantly share code, notes, and snippets.

View shixiaoyu's full-sized avatar
🤪
Why so Serious?

Xiaoyu shixiaoyu

🤪
Why so Serious?
View GitHub Profile
public class BinarySearchTreeIterator {
private Stack<TreeNode> stack = null;
public BinarySearchTreeIterator(TreeNode root) {
this.stack = new Stack<TreeNode>();
while (root != null) {
this.stack.push(root);
root = root.left;
}
public int calculate(String s) {
if (s == null || s.length() == 0) {
throw new IllegalArgumentException("invalid input");
}
s = s.trim();
int i = 0;
Stack<Long> operands = new Stack<>();
Stack<Character> operators = new Stack<>();
public int calculate(String s) {
if (s == null || s.length() == 0) {
throw new IllegalArgumentException("invalid input");
}
int i = 0;
Stack<Long> operands = new Stack<>();
Stack<Character> operators = new Stack<>();
StringBuilder number = new StringBuilder(); // deal with non single digit numbers
// Another thought is having 2 pass, first pass */, second pass +-
// "1 + 2 * 3 / 2" = 4, pretty clean
// "1 - 2 * 3 / 2" = -2
public int calculateTwoPass(String s) {
int len;
if (s == null || (len = s.length()) == 0) {
return 0;
}
Stack<Integer> stack = new Stack<Integer>();
int num = 0;
public int calculate(String s) {
if (s == null || s.length() == 0) {
throw new IllegalArgumentException("invalid input");
}
int i = 0;
// even though leetcode does not have this use case System.out.println(ins.calculate("0-2147483648")); // -2147483648
// It can still pass with Integer, but use long for overflow case in general
Stack<Long> operands = new Stack<>();
Stack<Character> operators = new Stack<>();
// The main idea of this is the left bracket might change the sign of a number, however, this does not seem to be very generalized
// https://leetcode.com/problems/basic-calculator/discuss/62362/JAVA-Easy-Version-To-Understand!!!!!
public class Solution {
// 1-2-(4+5+2)-3
public int calculate(String s) {
int len = s.length(), sign = 1, result = 0;
// Use one stack for numbers, for operators used the sign, + -> 1, - -> -1
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < len; i++) {
public int calculate(String s) {
if (s == null || s.length() == 0) {
throw new IllegalArgumentException("Invalid input");
}
int i = 0;
Stack<Integer> operands = new Stack<>();
Stack<Character> operators = new Stack<>();
StringBuilder number = new StringBuilder();
public boolean isBalanced(TreeNode root) {
return maxHeight(root) != -1;
}
// if -1, means it is not a balanced tree, since it will also return the normal height(int), so boolean is not an option.
// Kinda of a hack for the return type.
// @return -1 means it's already not a balanced tree, else t;he tree height
public int maxHeight(TreeNode root) {
if (root == null) {
return 0;
public int addDigits(int num) {
assert num >= 0;
while (num / 10 > 0) {
num = this.calDigitSum(num);
}
return num;
}
private int calDigitSum(int num) {
public boolean wordPatternMatch(String pattern, String str) {
if (pattern == null || str == null) {
return false;
}
Map<Character, String> lookup = new HashMap<>();
Set<String> dup = new HashSet<>();
return this.isMatch(pattern, str, lookup, dup);
}