Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge latest updates #1

Open
wants to merge 66 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
bd11ddf
KthSort
yuzhangcmu Sep 23, 2014
841e167
add the demos
yuzhangcmu Sep 23, 2014
dabfdb7
min cut
yuzhangcmu Sep 25, 2014
762dcad
Create README.md
yuzhangcmu Sep 25, 2014
ce3384e
Update README.md
yuzhangcmu Sep 25, 2014
078baa7
modify the directors
yuzhangcmu Sep 26, 2014
ab453fc
move the folder.
yuzhangcmu Sep 26, 2014
af5edeb
modify the tree Demo
yuzhangcmu Sep 26, 2014
52407ba
The LCA problem.
yuzhangcmu Sep 26, 2014
7c9819f
add some comments.
yuzhangcmu Sep 26, 2014
bb3eb2b
Arrange the code.
yuzhangcmu Sep 27, 2014
fa314c2
merge Sort
yuzhangcmu Sep 28, 2014
37c4105
merge
yuzhangcmu Oct 2, 2014
b0e4e32
Five Chessman
yuzhangcmu Oct 2, 2014
441dba7
FiveChessman
yuzhangcmu Oct 2, 2014
2eec824
notes
yuzhangcmu Oct 2, 2014
1551e7f
the largest Common subtree
yuzhangcmu Oct 2, 2014
61e79b2
the largest common
yuzhangcmu Oct 2, 2014
ff798ae
largestCommon
yuzhangcmu Oct 2, 2014
718698d
TestPermutation
yuzhangcmu Oct 5, 2014
7d60ff4
isCompleteBinaryTree
yuzhangcmu Oct 5, 2014
c73a8a7
NextPermutation
yuzhangcmu Oct 5, 2014
8c9f0e8
permutation sequence
yuzhangcmu Oct 5, 2014
b8c8f5f
Fibonacci
yuzhangcmu Oct 5, 2014
92f4168
add new file
Oct 7, 2014
f928b91
add code.
Oct 7, 2014
b6817a6
Recover Binary Search Tree
yuzhangcmu Oct 8, 2014
a758230
isValidBST
yuzhangcmu Oct 8, 2014
c04d009
in order
yuzhangcmu Oct 8, 2014
3292319
tree
yuzhangcmu Oct 8, 2014
8f28a48
tree
yuzhangcmu Oct 8, 2014
2d0cf85
minDepth
yuzhangcmu Oct 8, 2014
dc15508
min
yuzhangcmu Oct 8, 2014
de48d99
facebook
yuzhangcmu Oct 8, 2014
72f914d
Unique BSTs
yuzhangcmu Oct 9, 2014
d92fcd1
tree2
yuzhangcmu Oct 9, 2014
632666f
merge Sort
yuzhangcmu Oct 9, 2014
d1c27bd
merge
yuzhangcmu Oct 9, 2014
ec77e43
remove duplicate
yuzhangcmu Oct 9, 2014
127db30
remove duplicates2
yuzhangcmu Oct 9, 2014
63dd8d0
array
yuzhangcmu Oct 9, 2014
23f07e3
search
yuzhangcmu Oct 9, 2014
bd02708
maxSubArray
yuzhangcmu Oct 9, 2014
88a4d50
the merge
yuzhangcmu Oct 9, 2014
0ccff33
strstr
yuzhangcmu Oct 10, 2014
096b743
list
yuzhangcmu Oct 10, 2014
d955d59
divide
yuzhangcmu Oct 10, 2014
218d63f
pow
yuzhangcmu Oct 10, 2014
4f99b2d
pow
yuzhangcmu Oct 10, 2014
7564463
list
yuzhangcmu Oct 10, 2014
1ddacc5
sort
yuzhangcmu Oct 10, 2014
d5573fc
reorder list
yuzhangcmu Oct 10, 2014
c144ac6
random
yuzhangcmu Oct 10, 2014
2a37fec
reverse2
yuzhangcmu Oct 10, 2014
150f920
tree
yuzhangcmu Oct 10, 2014
82143ad
name
yuzhangcmu Oct 10, 2014
f08140a
lett
yuzhangcmu Oct 11, 2014
42796c9
list
yuzhangcmu Oct 11, 2014
d6d1308
list
yuzhangcmu Oct 11, 2014
0060c07
rotate
yuzhangcmu Oct 11, 2014
72e5ddf
list
yuzhangcmu Oct 11, 2014
cfdbfe8
remove
yuzhangcmu Oct 11, 2014
732d38a
list
yuzhangcmu Oct 11, 2014
53f429f
tree
yuzhangcmu Oct 11, 2014
1693ac2
candy
yuzhangcmu Oct 11, 2014
7a80b46
combine
yuzhangcmu Oct 11, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Divide2/Pow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package Algorithms.Divide2;

public class Pow {
public static void main(String[] strs) {
System.out.println(pow(-3, -2147483648));

}

public static double pow(double x, int n) {
if (x == 0) {
return 0;
}

if (x == 1 || n == 0) {
return 1;
}

// Because when we deal with -2147483648, we can't get right -n
// cause -n == n when it is -2147483648.
// 注意 这样写是不行的,因为-n在n到最小值会出错,
// int的最小值(负数)取-n仍然是n 这样就错了。
if (n < 0) {
double ret1 = x * pow(x, -(1 + n));
return 1/(double)ret1;
}

int m = n%2;

// count
double ret = pow(x, n/2);
ret *= ret;
if (m == 1) {
ret *= x;
}

return ret;
}
}
2 changes: 2 additions & 0 deletions Flatten.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package Algorithms;
import java.util.Stack;

import Algorithms.tree.TreeNode;



public class Flatten {
Expand Down
103 changes: 103 additions & 0 deletions GraphDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package Algorithms;

import java.util.ArrayList;

public class GraphDemo {
private static int M = Integer.MAX_VALUE; // 表示此路不可通

// http://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-algorithm/
// 以上为不能再优美的C语言实现。
public static void main(String[] args) {
int[][] w = { // 用邻接表矩阵表示的无向图
{0, 3, 2000, 7, M},
{3, 0, 4, 2, M},
{M, 4, 0, 5, 4},
{7, 2, 5, 0, 6},
{M, M , 4, 6, 0}
};

int[][] w2 = { // 用邻接表矩阵表示的无向图
{0, 10, M, 30, 100},
{M, 0, 50, M, M},
{M, M, 0, M, 10},
{M, M, 20, 0, 60},
{M, M, M, M, 0}
};

int start = 0;

int[] shortPath = dijkstra(w2, start);

for (int i = 0; i < shortPath.length; i++) {
System.out.println("The shortest path length from start to " + i + " is:" + shortPath[i]);
}
}

public static int[] dijkstra(int[][] graph, int src) {
if (graph == null || graph.length == 0 || graph[0].length == 0) {
return null;
}

// get to know the number of the vertexs.
int v = graph.length;

// We need a indicator to know if we have visited the vertex.
boolean visit[] = new boolean[v];

// record the length result.
int[] pathLen = new int[v];

// record the path.
ArrayList<ArrayList<Integer>> path = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < v; i++) {
path.add(new ArrayList<Integer>());
path.get(i).add(0);
path.get(i).add(i);
}

// setup the source vertex;
visit[0] = true;
pathLen[0] = 0;

// stop when all the vertices has been added into the result set.
for (int i = 0; i < v - 1; i++) {

int minLen = M;
int minIndex = -1;
for (int j = 0; j < v; j++) {
// sometimes there is no route, so just let equal also return.
// so we use graph[src][j] <= minLen
if (!visit[j] && graph[src][j] <= minLen) {
minLen = graph[src][j];
minIndex = j;
}
}

// get the new shortest path, add it into the solution set.
visit[minIndex] = true;
pathLen[minIndex] = graph[src][minIndex];

// update all the neighbors of the new vertex.
for (int k = 0; k < v; k++) {
// if the path which pass the minIndex is shorter than the former path,
// just update it.
if (!visit[k]
&& graph[src][minIndex] != M
&& graph[minIndex][k] != M
&& graph[src][minIndex] + graph[minIndex][k] < graph[src][k]) {
graph[src][k] = graph[src][minIndex] + graph[minIndex][k];

path.set(k, new ArrayList<Integer>(path.get(minIndex)));
path.get(k).add(k);
}
}
}

for (ArrayList<Integer> array: path) {
System.out.println(array.toString());
}

return pathLen;
}

}
2 changes: 2 additions & 0 deletions InOrderTraversal.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import java.util.ArrayList;
import java.util.Stack;

import Algorithms.tree.TreeNode;


public class InOrderTraversal {
public ArrayList<Integer> inorderTraversal(TreeNode root) {
Expand Down
2 changes: 2 additions & 0 deletions IsSymmetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayDeque;

import Algorithms.tree.TreeNode;

public class IsSymmetric {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
Expand Down
43 changes: 43 additions & 0 deletions KthSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Algorithms;

import java.util.HashMap;

public class KthSort {
public static class Graph {
String name;
HashMap<Graph, Integer> subs;
public Graph(String name) {
this.name = name;
}
}

public static void main(String[] strs) {
// int[] input = new int[]{1,2,4,3,9,11,0};
// sort(input);
// for(int n: input) {
// System.out.print(n + " ");
// }
Graph c1 = new Graph("c1");

//for()
}

public static void sort(int[] input) {
if (input == null) {
return;
}

int len = input.length;
for (int i = 1; i < len; i++) {
int cur = input[i];
int j = i - 1;
while (j >= 0 && input[j] > cur) {
input[j + 1] = input[j];
j--;
}

input[j + 1] = cur;
}
}

}
46 changes: 43 additions & 3 deletions ListNodeDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ public static void main(String[] args) {
ListNode c1 = new ListNode(1);
ListNode c2 = new ListNode(12);
c1.next = c2;
c2.next = n1;
//c2.next = n1;

//ListNode mergeNode = mergeSortedListRec(m1, c1);
ListNode mergeNode = mergeLink(m1, c1);
//ListNode mergeNode2 = mergeLink(m1, c1);
//ListNode mergeNode = mergeSortedList(m1, c1);
//printList(mergeNode);
printList(mergeNode);
//printList(mergeNode2);

System.out.println();

n1.next = n2;
n2.next = n3;
Expand Down Expand Up @@ -433,6 +437,42 @@ public static ListNode mergeSortedList(ListNode head1, ListNode head2) {
return dummyNode.next;
}

static class Node {
Node next;
int val;
Node (int val) {
this.val = val;
}
}

public static ListNode mergeLink (ListNode aLink, ListNode bLink) {
ListNode dummy = new ListNode(0);

ListNode root = dummy;

while (aLink != null && bLink != null) {
if (aLink.val < bLink.val) {
dummy.next = aLink;
dummy = aLink;
aLink = aLink.next;

} else {
dummy.next = bLink;
dummy = bLink;
bLink = bLink.next;

}
}

if (aLink != null) {
dummy.next = aLink;
} else {
dummy.next = bLink;
}

return root.next;
}

/*
* 先完成的算法,应该会简单一点儿。
* 简直是优雅的算法啊!太棒了!只不过 为什么自己很难想出这么棒的算法呢?
Expand Down
2 changes: 2 additions & 0 deletions Max_path_BinaryTree.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package Algorithms;
import java.util.ArrayList;

import Algorithms.tree.TreeNode;


public class Max_path_BinaryTree {
public static void main(String args[]){
Expand Down
3 changes: 3 additions & 0 deletions Populate2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package Algorithms;

import Algorithms.tree.TreeLinkNode;

public class Populate2 {
public static void main(String[] args) {
TreeLinkNode root = new TreeLinkNode(2);
Expand Down
94 changes: 0 additions & 94 deletions PostOrder.java

This file was deleted.

Loading