Skip to content

Instantly share code, notes, and snippets.

View fever324's full-sized avatar

Hongfei Li fever324

View GitHub Profile
@fever324
fever324 / SymmetricTree.java
Last active August 29, 2015 14:09
Symmetric Tree
/*
Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
import java.util.Stack;
/*
* Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
*
* push(x) -- Push element x onto stack.
* pop() -- Removes the element on top of the stack.
* top() -- Get the top element.
* getMin() -- Retrieve the minimum element in the stack.
*/
@fever324
fever324 / MinStack.java
Last active August 29, 2015 14:09
MinStack
import java.util.LinkedList;
import java.util.Stack;
/*
* Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
*
* push(x) -- Push element x onto stack.
* pop() -- Removes the element on top of the stack.
* top() -- Get the top element.
* getMin() -- Retrieve the minimum element in the stack.
@fever324
fever324 / Rotate List.java
Last active August 29, 2015 14:09
Rotate List
/*
* Given a list, rotate the list to the right by k places, where k is non-negative.
*
* For example:
* Given 1->2->3->4->5->NULL and k = 2,
* return 4->5->1->2->3->NULL.
*/
public class Solution {
public ListNode rotateRight(ListNode head, int n) {
// make sure list length > 1
@fever324
fever324 / ReverseInteger.java
Last active August 29, 2015 14:09
Reverse Integer
public class Solution1 {
public int reverse(int x) {
if(x < 10 && x > -10){
return x;
}
boolean negative = x < 0;
char[] c = null;
if(negative) {
c = (x+"").substring(1).toCharArray();
@fever324
fever324 / MergeSortedList.java
Last active August 29, 2015 14:09
Merge Sorted Lists
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@fever324
fever324 / EditDistance.java
Created November 12, 2014 04:01
Edit Distance
/*
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
*/
@fever324
fever324 / ReverseLevelOrderTraversal.java
Created November 12, 2014 05:01
Binary Tree Reverse Level Order Traversal
/*
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
@fever324
fever324 / Add Binary.java
Created November 13, 2014 04:09
Add Binary
/*
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
*/
public class Solution {
@fever324
fever324 / Majority Element.java
Created December 24, 2014 06:14
Majority Element
/*
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
*/
public class Solution {
public int majorityElement(int[] num) {
int candidate = num[0];
int counter = 0;