Skip to content

Instantly share code, notes, and snippets.

@wszdwp
wszdwp / gist:6708927
Created September 26, 2013 02:13
Sorting-insertionSort, shellSort, BubbleSort
public class Sorting
{
public static void insertionSort(int[] a) {
for(int index = 1; index < a.length; index++) {
int key = a[index];
int position = index;
while( position > 0 && a[ position - 1] > key ) {
a[position] = a[position - 1];
position--;
//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 / Longest common prefix
Created November 12, 2014 03:15
Write a function to find the longest common prefix string amongst an array of strings.
public String longestCommonPrefix(String[] strs) {
int n = strs.length;
if (n == 0)
return "";
if (n == 1)
return strs[0];
String prefix = strs[0];
@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);