Skip to content

Instantly share code, notes, and snippets.

@Ray1988
Ray1988 / Combinations.java
Created April 12, 2014 17:10
Combinations
/*
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
@Ray1988
Ray1988 / RemoveNthNodeFromEndOfList .java
Created April 5, 2014 16:48
Remove Nth Node From End of List
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@Ray1988
Ray1988 / PopulatingNext RightPointersInEachNodeII.py
Created March 11, 2014 04:05
Populating Next Right Pointers in Each Node II (Python)
"""
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
You may only use constant extra space.
For example,
Given the following binary tree,
@Ray1988
Ray1988 / PopulatingNextRightPointersInEachNodeII.java
Created March 11, 2014 03:51
Populating Next Right Pointers in Each Node II
/*
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
You may only use constant extra space.
For example,
Given the following binary tree,
@Ray1988
Ray1988 / PalindromeNumber.java
Created March 7, 2014 20:42
Palindrome Number (Java)
/*
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
@Ray1988
Ray1988 / SumRootToLeafNumbers.py
Created March 6, 2014 18:56
Sum Root to Leaf Numbers(Python)
"""
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,
1
@Ray1988
Ray1988 / SumRootTOLeafNum.java
Created March 6, 2014 18:36
Sum Root to Leaf Numbers (Java)
/*
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,
1
@Ray1988
Ray1988 / MinimumDepthofBinaryTree.py
Created March 5, 2014 19:27
Minimum Depth of Binary Tree(Python)
"""
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
"""
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
@Ray1988
Ray1988 / SearchInRotatedSortedArray.py
Created March 4, 2014 20:12
Search in Rotated Sorted Array
class Solution:
# @param A, a list of integers
# @param target, an integer to be searched
# @return an integer
def search(self, A, target):
if A==None or len(A)==0:
return -1;
st=0;
ed=len(A)-1
return self.searchHelper(A, target, st, ed)
@Ray1988
Ray1988 / TrappingRainWater.py
Created March 3, 2014 01:49
Trapping Rain Water (Python)
"""
Given n non-negative integers representing an elevation map where the width of each bar is 1,
compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1].
In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!