Skip to content

Instantly share code, notes, and snippets.

//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;
* TreeNode right;
@wszdwp
wszdwp / LCA - Lowest common ancestor in a binary tree
Created November 8, 2014 17:50
LCA - Lowest common ancestor in a binary tree
//http://leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-i.html
public class LCA
{
public static TreeNode findTheLowestCommonAncestor(TreeNode root, TreeNode nd1, TreeNode nd2) {
if (root == null)
return null;
if (root == nd1 || root == nd2)
@wszdwp
wszdwp / Rotate List
Created November 8, 2014 17:53
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.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@wszdwp
wszdwp / Convert Sorted Array to Binary Search Tree
Created November 8, 2014 17:54
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@wszdwp
wszdwp / Reverse Linked List II
Created November 8, 2014 17:55
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@wszdwp
wszdwp / Divide Two Integers
Created November 8, 2014 17:56
Divide two integers without using multiplication, division and mod operator.
public class Solution {
public int divide(int dividend, int divisor) {
//http://www.lifeincode.net/programming/leetcode-divide-two-integers-java/
long p = Math.abs((long)dividend);
long q = Math.abs((long)divisor);
int ret = 0;
while (p >= q) {
int counter = 0;
while (p >= (q << counter)) {
@wszdwp
wszdwp / Subsets II
Created November 8, 2014 17:58
Given a collection of integers that might contain duplicates, S, return all possible subsets.
/*
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,2], a solution is:
[
@wszdwp
wszdwp / Median of Two Sorted Arrays
Created November 12, 2014 15:48
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
public double findMedianSortedArrays(int A[], int B[]) {
//http://www.cnblogs.com/tenosdoit/p/3554479.html
int aLen = A.length;
int bLen = B.length;
if ((aLen + bLen) % 2 != 0) { //odd
return findKth(A, B, (aLen + bLen) / 2, 0, aLen - 1, 0, bLen - 1);
} else {
return (findKth(A, B, (aLen + bLen) / 2 - 1, 0, aLen - 1, 0, bLen - 1)
+ findKth(A, B, (aLen + bLen) / 2, 0, aLen - 1, 0, bLen - 1)) * (0.5);
@wszdwp
wszdwp / Two sum
Created November 12, 2014 16:53
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…
public int[] twoSum(int[] numbers, int target) {
int[] ans = new int[2];
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
int num = numbers[i];
if (map.containsKey(num)) {
ans[0] = map.get(num) + 1;
ans[1] = i + 1;
@wszdwp
wszdwp / 3Sum cloest
Created November 12, 2014 17:13
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. For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
public int threeSumClosest(int[] num, int target) {
http://www.programcreek.com/2013/02/leetcode-3sum-closest-java/
int min = Integer.MAX_VALUE;
int result = 0;
Arrays.sort(num);
for (int i = 0; i < num.length; i++) {
int start = i + 1;
int end = num.length - 1;