This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for binary tree | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
public class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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: | |
[ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String longestCommonPrefix(String[] strs) { | |
int n = strs.length; | |
if (n == 0) | |
return ""; | |
if (n == 1) | |
return strs[0]; | |
String prefix = strs[0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |