Skip to content

Instantly share code, notes, and snippets.

View wayetan's full-sized avatar

Wei Tan wayetan

  • Microsoft
  • United States
View GitHub Profile
@wayetan
wayetan / PopulatingNextRightPointersinEachNode.java
Last active August 19, 2016 07:04
Populating Next Right Pointers in Each Node
/**
* Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
@wayetan
wayetan / BinaryTreeInorderTraversal.java
Created December 27, 2013 06:08
Binary Tree Inorder Traversal
/**
* Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
@wayetan
wayetan / BinaryTreePreorderTraversal.java
Created December 27, 2013 07:01
Binary Tree Preorder Traversal
/**
* Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
@wayetan
wayetan / RemoveElement.java
Created December 27, 2013 07:37
Remove Element
/**
* Given an array and a value, remove all instances of that value in place and return the new length.
* The order of elements can be changed. It doesn't matter what you leave beyond the new length.
*/
public class Solution {
public int removeElement(int[] A, int elem) {
int len = A.length;
for(int i = 0; i < len; ){
if(A[i] == elem)
@wayetan
wayetan / ClimbingStairs.java
Created December 27, 2013 07:54
Climbing Stairs
/**
* You are climbing a stair case. It takes n steps to reach to the top.
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
*/
public class Solution {
public int climbStairs(int n) {
int[] arr = new int[n + 1];
arr[0] = 1;
arr[1] = 1;
@wayetan
wayetan / RemoveDuplicatesfromSortedArray.java
Last active January 1, 2016 12:19
Remove Duplicates from Sorted Array
/**
* Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
* Do not allocate extra space for another array, you must do this in place with constant memory.
* For example,
* Given input array A = [1,1,2],
* Your function should return length = 2, and A is now [1,2].
*/
public class Solution {
public int removeDuplicates(int[] A) {
@wayetan
wayetan / MaximumSubarray.java
Created December 27, 2013 09:17
Maximum Subarray
/**
* Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
* For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
* the contiguous subarray [4,−1,2,1] has the largest sum = 6.
*/
public class Solution {
public int maxSubArray(int[] A) {
int res = A[0], sum = 0;
for(int i = 0; i < A.length; ++i){
sum = Math.max(sum + A[i], A[i]);
@wayetan
wayetan / RomantoInteger.java
Created December 27, 2013 09:18
Roman to Integer
/**
* Given a roman numeral, convert it to an integer.
* Input is guaranteed to be within the range from 1 to 3999.
*/
public class Solution {
public int romanToInt(String s) {
HashMap<Character, Integer> dic = new HashMap<Character, Integer>();
dic.put('I', 1);
dic.put('V', 5);
@wayetan
wayetan / SymmetricTree.java
Created December 27, 2013 10:35
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
/ \ / \
3 4 4 3
* But the following is not:
1
@wayetan
wayetan / MergeTwoSortedListsIterative.java
Last active January 1, 2016 12:38
Merge Two Sorted Lists
/**
* 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.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;