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 / SingleNumber.java
Last active January 1, 2016 04:29
Single Number
/**
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*/
public class Solution{
public int singleNumber(int[] A){
int num = A[0];
for(int i = 1; i < A.length; i++){
@wayetan
wayetan / MaximumDepthOfBinaryTree.java
Created December 23, 2013 06:36
Maximum Depth of Binary Tree
/**
* Given a binary tree, find its maximum depth.
* The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
@wayetan
wayetan / SameTree.java
Created December 23, 2013 06:40
Same Tree
/**
* Given two binary trees, write a function to check if they are equal or not.
* Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
@wayetan
wayetan / ReverseInteger.java
Last active January 1, 2016 04:39
Reverse Integer
/**
* Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
1. If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
2. Did you notice that the reversed integer might overflow?
@wayetan
wayetan / BestTimetoSellandBuyStocks.java
Last active March 22, 2017 07:33
Best Time to Sell and Buy Stocks
/**
* Say you have an array for which the ith element is the price of a given stock on day i.
*
* If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
*/
public class Solution{
public int maxProfit(int[] prices) {
if(prices.length == 0 || prices.length == 1) return 0;
int max_profit = 0;
@wayetan
wayetan / UniqueBinarySearchTree.java
Created December 24, 2013 06:09
Unique Binary Search Tree
/**
* Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
* For example,
* Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
@wayetan
wayetan / LinkedListCycle.java
Created December 24, 2013 10:39
Linked List Cycle
/**
* Given a linked list, determine if it has a cycle in it.
*/
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
@wayetan
wayetan / SearchInsertPosition.java
Last active June 23, 2016 18:49
Search Insert Position
/**
* Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
* You may assume no duplicates in the array.
* Here are few examples.
* [1,3,5,6], 5 → 2
* [1,3,5,6], 2 → 1
* [1,3,5,6], 7 → 4
* [1,3,5,6], 0 → 0
*/
@wayetan
wayetan / TwoSum.java
Last active January 1, 2016 10:09
Two Sum
/**
* Given an array of integers, find two numbers such that they add up to a specific target number.
* The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
* Please note that your returned answers (both index1 and index2) are not zero-based.
* You may assume that each input would have exactly one solution.
* Input: numbers={2, 7, 11, 15}, target=9
* Output: index1=1, index2=2
*
*/
public class Solution{
@wayetan
wayetan / RemoveDuplicatesfromSortedList.java
Last active August 21, 2016 04:36
Remove Duplicates from Sorted List
/**
* Given a sorted linked list, delete all duplicates such that each element appear only once.
* For example,
* Given 1->1->2, return 1->2.
* Given 1->1->2->3->3, return 1->2->3.
*/
/**
* Definition for singly-linked list.
* public class ListNode {