- 博客(147)
- 收藏
- 关注
原创 Find the First Common Ancestor
class Tnode{ Tnode parent; Tnode left; Tnode right; int val; } //if it has parent node public Tnode ancestor(Tnode node1, Tnode node2) { if(node1 == null || node2 == null) { retu
2013-11-14 03:45:56 612
原创 Find Median With Two heaps
Numbers are randomly generated and passed to a method. Write a program to find and maintain the median values as new values are generated. public int solution(int[] a) { PriorityQueue maxHeap = n
2013-11-14 02:17:07 747
原创 Find M Integers from an Array of Size N equally
public int[] getRandom(int[] a, int m) { int[] result = new int[m]; //we should change the array, so it is better to have a clone of that int[] tmp = a.clone(); for(int i = 0; i <m; i++) {
2013-11-14 01:13:25 602
原创 If t is the subString of s
//judge if t is the substring of s public boolean isSubString(String s, String t){ if(s == null || t == null) return false; if(t.length() == 0) return true; int index = 0;
2013-11-14 00:59:00 753
原创 Binary Tree To Double LinkedList
Change a binary tree to a double linkedlist. class Node{ Node left; Node right; int val; } //Time complexity: O(n^2). public Node convert(Node root) { if(root == null) return null;
2013-11-14 00:02:11 581
原创 Reverse LinkedList
//Time Complexity: O(N), Space Complexity: O(N). public static Node recursive(Node head) { if(head == null || head.next == null) { return head; } Node recursiveNode = recursive(head.next)
2013-11-13 22:35:52 574
原创 Find Max Subarray
//one dimension dp public int maxSubArray(int[] A) { // Start typing your Java solution below // DO NOT write main() function int max = 0; int result = Integer.MIN_VALUE
2013-11-13 22:20:38 721
原创 Compare two numbers Without if-else and comparison
write a method which finds the maximum of two numbers. You should not use if-else or any other comparision operator. //input a, b //return a * flag + b * (1 ^ flag) (which means if a > b then flag =
2013-11-13 21:59:09 621
原创 Search String in a String array with empty string
//find a string in a sorted string array with empty stringpublic int search(String[] ss, String s) { if(ss == null || s == null || s.length() == 0) return -1; return search(ss, s, 0, ss.length - 1)
2013-11-13 13:08:52 665
原创 Sort String Array Make All Anagrams Next To each Other
public void sort(String[] a) { Map> map = new HashMap>(); for(String s : a) { char[] c = s.toCharArray(); Arrays.sort(c); String key = new String(c); if(!map.containsKey(key)) { map.put(k
2013-11-13 12:55:23 553
原创 Permutations of a String
//time complexity: O(n!) public ArrayList permutations(String s) { if(s == null) return null; ArrayList result = new ArrayList(); //base case if(s.length() == 0) { result.add(""); retur
2013-11-13 12:37:31 647
原创 Magic index
find the index with value equal to index. Time Complexity: O(logn), Space Complexity: O(1). //numbers are distinct public int getMagic(int[] a, int start, int end) { if(start > end) return -1; in
2013-11-13 12:29:31 1009
原创 Fibonacci
//recursive, dp public int[] fib(int n) { int[] result = new int[n + 1]; int tmp = fib(n, result); return result; } public int fib(int n, int[] a) { if(n == 0) { a[0] = 0; return 0;
2013-11-13 10:44:21 490
原创 Implement Queue with Two Stacks
class Node { Node next; int data;}class Stack { Node top; boolean isEmpty() { return top == null; } int pop()throws Exception{ if(isEmpty()) { throw new Exception("The stack is Em
2013-11-13 10:00:07 688
原创 LinkedList Numbers Add
//recursive, Time Complexity: O(N), Space Complexity: O(N). public Node addLinkedList(Node node1, Node node2) { int carry = 0; return recursive(node1, node2, carry); } public static Node recu
2013-11-13 09:54:16 529
原创 Reverse String and Word
public String reverseWord(String s) { if(s == null || s.length() == 0) return s; char[] array = s.toCharArray(); reverseArray(array, 0, array.length - 1); for(int i = 0; i < array.
2013-11-13 09:01:19 609
原创 Find All Primes
//print all primes <= n.public ArrayList prime(int n) { ArrayList result = new ArrayList(); if(n <= 0) return result; int[] tmp = new int[n + 1]; int p = 2; while(p <= n) {
2013-11-13 08:29:51 534
原创 Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.public int maximalRectangle(char[][] matrix) { int lengthX = matrix.length;
2013-11-13 08:03:32 473
原创 Divide Two Numbers Without Symbol And Also Minus And Multiply
public int divide(int dividend, int divisor) { if(dividend == 0) return 0; if(divisor == 0) return (dividend > 0) ? Integer.MAX_VALUE : Integer.MIN_VALUE; boolean flag
2013-11-13 05:47:45 653
原创 LinkedList Palindrome
public boolean isPalindrome(Node head) { if(head == null || head.next == null) return true; Node slow = head; Node fast = head; while(fast.next != null && fast.next.next != null) {
2013-11-13 01:03:55 485
原创 Serialize and deserialize a binary tree
//time: O(n), space: O(n).public ArrayList serialize(Node root) { ArrayList result = new ArrayList(); help(root, result); return result;}public void help(TreeNode root, ArrayList resul
2013-11-13 00:50:48 793
原创 Copy LinkedList With Arbitrary Node
To copy a linked list. Every node's next point to the next node, and another point to an arbitrary node. //time complexity: O(n), space: O(1) public Node copyList(Node head) { if(head == null)
2013-11-12 08:43:01 521
原创 Subsets with repeated elements
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 duplica
2013-11-12 05:54:55 559
原创 Subsets
Given a set of distinct integers, 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,I
2013-11-12 05:47:46 512
原创 All possible paths
public class AllPath { public static void main(String[] args) { int[][] matrix = new int[3][3]; /*int[][] matrix = { {0,0,0,0}, {0,1,0,0}, {0,0,1,0} };*/ ArrayList> result = fin
2013-11-12 04:51:25 500
原创 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space? public ListNode detectCycle(ListNode
2013-11-10 21:44:56 438
原创 Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space? public boolean hasCycle(ListNode head) { // IMPORTANT: Please reset
2013-11-10 21:44:12 450
原创 Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to
2013-11-10 21:43:10 457
原创 Binary Tree Postorder Traversal
public ArrayList postorderTraversal(TreeNode root) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case.
2013-11-10 21:41:50 474
原创 Binary Tree Preorder Traversal
public ArrayList preorderTraversal(TreeNode root) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case.
2013-11-10 21:40:15 503
原创 reverse string
public static String reverseString(String s) { if(s == null || s.length() == 0) return s; int n = s.length() - 1; char[] array = s.toCharArray(); for(int i = 0; i < n / 2; i++) {
2013-10-04 10:13:12 352
原创 subArray Related
//Given an input array find all subarrays with given sum K //time complexity: 0(n), this is for all subarrays public static ArrayList> findSubArray(int[] array, int k) { int sum = 0; int s
2013-10-04 09:47:30 371
原创 isSubTree
public boolean isSubtree(TreeNode root1, TreeNode root2) { if(root1 == null) return true; else if(root2 == null) return false; else if(root1.val == root2.val) { if(isEqual(root1.le
2013-10-03 09:36:24 494 1
原创 heapSort
public class heapsort { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {2,4,3,3,1,7,8,5}; buildMaxHeap(a); for(int i : a)
2013-10-03 09:14:36 454
原创 Minimum Window Substring
public String minWindow(String S, String T) { if(S == null || T == null || S.length() == 0 || T.length() == 0 || S.length() < T.length()) return ""; int[] shouldFound = new int[256];
2013-09-24 06:10:23 407
原创 Multiply Strings
public String multiply(String num1, String num2) { // Start typing your Java solution below // DO NOT write main() function if(num1 == null || num2 == null || num1.length() ==
2013-09-07 11:19:51 330
原创 Valid Number
//time complexity: O(n). public boolean isNumber(String s) { // Start typing your Java solution below // DO NOT write main() function if(s == null || s.length() == 0) retur
2013-09-07 09:42:20 344
原创 Spiral Matrix II
public int[][] generateMatrix(int n) { // Start typing your Java solution below // DO NOT write main() function int[][] matrix = new int[n][n]; if(n == 0) return matrix
2013-09-07 01:26:57 403
原创 Spiral Matrix
public ArrayList spiralOrder(int[][] matrix) { // Start typing your Java solution below // DO NOT write main() function ArrayList result = new ArrayList(); int m = matr
2013-09-07 01:00:52 465
原创 Container With Most Water
//time complexity: O(n). //the max area between twp lines should be decided by the min line public int maxArea(int[] height) { // Start typing your Java solution below // DO NO
2013-09-06 22:40:58 332
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人