Skip to content

Instantly share code, notes, and snippets.

@zac-xin
zac-xin / validParenthesis.java
Created December 27, 2012 20:23
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.
public class Solution {
public boolean isValid(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if(s == null || s.length() == 0)
return true;
if(s.length() % 2 == 1)
return false;
@zac-xin
zac-xin / romanToInt.java
Created December 27, 2012 16:49
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
public class Solution {
char symbols[] = {'M', 'D', 'C', 'L', 'X', 'V', 'I'};
int values[] = {1000, 500, 100, 50, 10, 5, 1};
public int romanToInt(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if(s.length() == 0)
return 0;
@zac-xin
zac-xin / Permutations.java
Created December 27, 2012 16:34
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
return permuteHelper(num, 0);
}
public ArrayList<ArrayList<Integer>> permuteHelper(int num[], int index){
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(index == num.length - 1){
@zac-xin
zac-xin / AddBinary.java
Created December 26, 2012 16:58
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100".
public class Solution {
public String addBinary(String a, String b) {
// Start typing your Java solution below
// DO NOT write main() function
int la = a.length();
int lb = b.length();
int max = Math.max(la, lb);
StringBuilder sum = new StringBuilder("");
@zac-xin
zac-xin / Anagrams.java
Created December 26, 2012 04:54
Given an array of strings, return all groups of strings that are anagrams.
public class Solution {
public ArrayList<String> anagrams(String[] strs) {
// Start typing your Java solution below
// DO NOT write main() function
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
ArrayList<String> result = new ArrayList<String>();
if(strs.length < 2)
return result;
@zac-xin
zac-xin / testBalance.java
Last active December 10, 2015 03:28
Given a binary tree, determine if it is height-balanced.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@zac-xin
zac-xin / 3sumClosest.java
Created December 24, 2012 16:22
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
public class Solution {
public int threeSumClosest(int[] num, int target) {
// Start typing your Java solution below
// DO NOT write main() function
Arrays.sort(num);
int diff = Integer.MAX_VALUE;
int closest = Integer.MAX_VALUE;
for(int i = 0; i < num.length - 2; i++){
int k = i + 1;
@zac-xin
zac-xin / largestRectangleArea.java
Last active December 10, 2015 01:19
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
import java.util.Stack;
public class Solution {
public int largestRectangleArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
int i = 0;
int max = 0;
Stack<Pair> stack = new Stack<Pair>();
@zac-xin
zac-xin / letterCombinations.java
Last active December 10, 2015 01:19
Given a digit string, return all possible letter combinations that the number could represent.
public class Solution {
public ArrayList<String> letterCombinations(String digits) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<String> result = new ArrayList<String>();
char str[] = new char[digits.length()];
helper(0, str, result, digits);
return result;
@zac-xin
zac-xin / Sqrt.java
Created December 21, 2012 16:21
Implement int sqrt(int x). Compute and return the square root of x.
public class Solution {
public int sqrt(int x) {
// Start typing your Java solution below
// DO NOT write main() function
long low = 0;
long high = x;
while(low <= high){
long mid = low + (high - low) / 2;
long result = mid * mid;
if(result == x){