Skip to content

Commit ff47f60

Browse files
committed
Merge pull request Hearen#101 from wuxianxingkong/master
update
2 parents 592ba7a + e62c1df commit ff47f60

File tree

50 files changed

+2387
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2387
-25
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.xingkong;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
import java.util.Stack;
8+
9+
/**
10+
* @author cuiguangfan [email protected]:
11+
* @version create time:2016年1月6日 下午10:29:04 class description
12+
*/
13+
public class BinaryTreeZigzagLevelOrderTraversal_103 {
14+
private static class TreeNode {
15+
int val;
16+
TreeNode left;
17+
TreeNode right;
18+
TreeNode(int x) {
19+
val = x;
20+
}
21+
@Override
22+
public String toString() {
23+
return "TreeNode [val=" + val+ "]";
24+
}
25+
}
26+
27+
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
28+
List<List<Integer>> list=new ArrayList<List<Integer>>();
29+
if(root==null) return list;
30+
Queue<TreeNode> queue=new LinkedList<TreeNode>();
31+
Stack<TreeNode> stack=new Stack<TreeNode>();
32+
queue.add(root);
33+
stack.push(root);
34+
int flag=0;
35+
//偶数次读队列,奇数次读栈
36+
while(!queue.isEmpty()&&!stack.isEmpty()){
37+
List<Integer> tempList=new ArrayList<Integer>();
38+
Queue<TreeNode> currentQueue=new LinkedList<TreeNode>();
39+
Stack<TreeNode> currentStack=new Stack<TreeNode>();
40+
if(flag%2==0){
41+
while(!queue.isEmpty()){
42+
TreeNode tnValue = queue.poll();
43+
tempList.add(tnValue.val);
44+
TreeNode tnNext =tnValue;
45+
if(tnNext.left!=null){
46+
currentQueue.add(tnNext.left);
47+
currentStack.push(tnNext.left);
48+
}
49+
if(tnNext.right!=null){
50+
currentQueue.add(tnNext.right);
51+
currentStack.push(tnNext.right);
52+
}
53+
}
54+
}else{
55+
while(!stack.isEmpty()){
56+
TreeNode tnValue = stack.pop();
57+
tempList.add(tnValue.val);
58+
TreeNode tnNext =queue.poll();
59+
if(tnNext.left!=null){
60+
currentQueue.add(tnNext.left);
61+
currentStack.push(tnNext.left);
62+
}
63+
if(tnNext.right!=null){
64+
currentQueue.add(tnNext.right);
65+
currentStack.push(tnNext.right);
66+
}
67+
68+
69+
}
70+
}
71+
list.add(tempList);
72+
queue=currentQueue;
73+
stack=currentStack;
74+
flag++;
75+
}
76+
return list;
77+
}
78+
79+
public static void main(String[] args) {
80+
// TreeNode tn1=new TreeNode(3);
81+
// TreeNode tn2=new TreeNode(9);
82+
// TreeNode tn3=new TreeNode(20);
83+
// TreeNode tn4=new TreeNode(11);
84+
// TreeNode tn5=new TreeNode(4);
85+
// TreeNode tn6=new TreeNode(15);
86+
// TreeNode tn7=new TreeNode(7);
87+
// TreeNode tn8=new TreeNode(8);
88+
// TreeNode tn9=new TreeNode(6);
89+
// TreeNode tn10=new TreeNode(13);
90+
// TreeNode tn11=new TreeNode(14);
91+
// TreeNode tn12=new TreeNode(22);
92+
// TreeNode tn13=new TreeNode(23);
93+
// tn1.left=tn2;
94+
// tn1.right=tn3;
95+
// tn2.left=tn4;
96+
// tn2.right=tn5;
97+
// tn3.left=tn6;
98+
// tn3.right=tn7;
99+
// tn4.left=tn8;
100+
// tn4.right=tn9;
101+
// tn5.left=tn10;
102+
// tn6.right=tn11;
103+
// tn10.left=tn12;
104+
// tn11.right=tn13;
105+
BinaryTreeZigzagLevelOrderTraversal_103 btzlo=new BinaryTreeZigzagLevelOrderTraversal_103();
106+
System.out.println(btzlo.zigzagLevelOrder(null));
107+
}
108+
109+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.xingkong;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author cuiguangfan [email protected]:
8+
* @version create time:2016年1月7日 下午9:32:10
9+
* class description
10+
*/
11+
public class ConstructBinaryTreeFromPreorderandInorderTraversal_105 {
12+
// private Map<Integer,Integer> preOrderMap=new HashMap<Integer,Integer>();
13+
private Map<Integer,Integer> inOrderMap=new HashMap<Integer,Integer>();
14+
public static class TreeNode {
15+
int val;
16+
TreeNode left;
17+
TreeNode right;
18+
TreeNode(int x) {
19+
val = x;
20+
}
21+
@Override
22+
public String toString() {
23+
return "TreeNode [val=" + val+ "]";
24+
}
25+
}
26+
27+
/**
28+
* @param preorder
29+
* @param inorder
30+
* @param rootIndex preorder中的下一个元素
31+
* @param startIndex 本次递归的inorder中的开始下标
32+
* @param endIndex 本次递归的inorder中的结束下标
33+
* @return
34+
*/
35+
public TreeNode backTrace(int[] preorder, int[] inorder,int rootIndex,int startIndex,int endIndex){
36+
if(startIndex>endIndex) return null;
37+
int midIndex=inOrderMap.get(preorder[rootIndex]);
38+
TreeNode root=new TreeNode(inorder[midIndex]);
39+
if(startIndex==endIndex)
40+
return root;
41+
int leftLength=midIndex-startIndex;//通过中序遍历获取左子树
42+
int rightLength=endIndex-midIndex;//通过中序遍历获取右子树
43+
//获取左子树部分
44+
root.left=backTrace(preorder,inorder,rootIndex+1,midIndex-leftLength,midIndex-1);
45+
//获取右子树部分
46+
root.right=backTrace(preorder,inorder,rootIndex+leftLength+1,midIndex+1,midIndex+rightLength);
47+
return root;
48+
}
49+
public TreeNode buildTree(int[] preorder, int[] inorder) {
50+
for(int i=0;i<preorder.length;i++){
51+
// preOrderMap.put(preorder[i], i);
52+
inOrderMap.put(inorder[i], i);
53+
}
54+
return backTrace(preorder,inorder,0,0,preorder.length-1);
55+
}
56+
57+
public static void main(String[] args) {
58+
int[] preorder=new int[]{1,2,4,5,6,7,8,3,9,10,11,12,13};
59+
int[] inorder=new int[]{6,5,7,4,2,8,1,9,3,11,10,12,13};
60+
ConstructBinaryTreeFromPreorderandInorderTraversal_105 t=new ConstructBinaryTreeFromPreorderandInorderTraversal_105();
61+
TreeNode tn=t.buildTree(preorder, inorder);
62+
System.out.println(tn.val);
63+
}
64+
65+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.xingkong;
2+
3+
import com.xingkong.Test.ListNode;
4+
5+
/**
6+
* @author cuiguangfan [email protected]:
7+
* @version create time:2016年1月7日 下午2:27:37 class description
8+
*/
9+
public class ConvertSortedListtoBinarySearchTree_109 {
10+
public static class ListNode {
11+
int val;
12+
ListNode next;
13+
14+
ListNode(int x) {
15+
val = x;
16+
}
17+
}
18+
19+
public static class TreeNode {
20+
int val;
21+
TreeNode left;
22+
TreeNode right;
23+
TreeNode(int x) {
24+
val = x;
25+
}
26+
}
27+
28+
// 返回中间节点的前一个节点
29+
static ListNode getLeftNodeFromList(ListNode head) {
30+
ListNode next = head;
31+
ListNode previous = head;
32+
while (next != null) {
33+
next = next.next;
34+
if (next == null) {
35+
break;
36+
}
37+
next = next.next;
38+
if (next == null) {
39+
break;
40+
}
41+
previous = head;
42+
head = head.next;
43+
}
44+
return previous;
45+
}
46+
public static TreeNode sortedListToBST(ListNode head) {
47+
if(head==null) return null;
48+
if(head.next==null){
49+
return new TreeNode(head.val);
50+
}
51+
ListNode leftListNode=getLeftNodeFromList(head);
52+
ListNode rootListNode=leftListNode.next;
53+
ListNode rightListNode=rootListNode.next;
54+
leftListNode.next=null;
55+
rootListNode.next=null;
56+
TreeNode root=new TreeNode(rootListNode.val);
57+
root.left=sortedListToBST(head);
58+
root.right=sortedListToBST(rightListNode);
59+
return root;
60+
}
61+
public static void main(String[] args) {
62+
ListNode head=new ListNode(1);
63+
ListNode pre=head;
64+
for(int i=2;i<=9;i++){
65+
ListNode temp=new ListNode(i);
66+
pre.next=temp;
67+
pre=temp;
68+
}
69+
TreeNode root=sortedListToBST(head);
70+
System.out.println(root);
71+
}
72+
73+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.xingkong;
2+
/**
3+
* @author cuiguangfan [email protected]:
4+
* @version create time£º2016Äê1ÔÂ14ÈÕ ÏÂÎç10:52:26
5+
* class description
6+
*/
7+
public class CountOfRangeSum_327 {
8+
public int countRangeSum(int[] nums, int lower, int upper) {
9+
10+
return 0;
11+
}
12+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.xingkong;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* @author cuiguangfan [email protected]:
9+
* @version create time£º2016Äê1ÔÂ14ÈÕ ÉÏÎç8:46:46 class description
10+
*/
11+
public class CountOfSmallerNumbersAfterSelf_315 {
12+
private static class TreeNode {
13+
TreeNode left;
14+
TreeNode right;
15+
int thisCount=1;
16+
int value;
17+
int leftCount;
18+
19+
TreeNode(int value) {
20+
this.value = value;
21+
}
22+
23+
}
24+
public List<Integer> countSmaller(int[] nums) {
25+
List<Integer> returnList=new ArrayList<Integer>();
26+
if(nums.length==0) return returnList;
27+
returnList.add(0);
28+
TreeNode treeRoot = new TreeNode(nums[nums.length - 1]);
29+
for (int i = nums.length - 2; i >= 0; i--) {
30+
TreeNode currentRoot = treeRoot;
31+
Integer count=0;
32+
while (currentRoot!=null) {
33+
if (nums[i] > currentRoot.value) {
34+
count+=currentRoot.leftCount+currentRoot.thisCount;
35+
if (currentRoot.right != null) {
36+
currentRoot = currentRoot.right;
37+
} else {
38+
currentRoot.right = new TreeNode(nums[i]);
39+
break;
40+
}
41+
} else if(nums[i] < currentRoot.value){
42+
currentRoot.leftCount++;
43+
if (currentRoot.left != null) {
44+
currentRoot = currentRoot.left;
45+
} else {
46+
currentRoot.left = new TreeNode(nums[i]);
47+
break;
48+
}
49+
}else{
50+
count+=currentRoot.leftCount;
51+
currentRoot.thisCount++;
52+
break;
53+
}
54+
}
55+
returnList.add(count);
56+
}
57+
Collections.reverse(returnList);
58+
return returnList;
59+
}
60+
61+
public static void main(String[] args) {
62+
CountOfSmallerNumbersAfterSelf_315 csn=new CountOfSmallerNumbersAfterSelf_315();
63+
System.out.println(csn.countSmaller(new int[]{26,78,27,100,33,67,90,23,66,5,38,7,35,23,52,22,83,51,98,69,81,32,78,28,94,13,2,97,3,76,99,51,9,21,84,66,65,36,100,41}));
64+
}
65+
66+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.xingkong;
2+
3+
/**
4+
* @author cuiguangfan [email protected]:
5+
* @version create time:2016年1月20日 上午8:44:09 class description
6+
*/
7+
public class HouseRobber_198 {
8+
9+
/**
10+
* 转移方程:money[(i)=max{money(i-1),money(i-2)+nums[i]}
11+
* @param nums
12+
* @return
13+
*/
14+
public int rob(int[] nums) {
15+
if (nums.length == 0)
16+
return 0;
17+
int[] money = new int[nums.length];
18+
money[0] = nums[0];
19+
if (nums.length >= 2)
20+
money[1] = nums[1] > nums[0] ? nums[1] : nums[0];
21+
for (int i = 2; i < nums.length; i++) {
22+
money[i] = money[i - 1] >= (money[i - 2] + nums[i]) ? money[i - 1] : (money[i - 2] + nums[i]);
23+
}
24+
return money[nums.length - 1];
25+
}
26+
27+
public static void main(String[] args) {
28+
// TODO Auto-generated method stub
29+
HouseRobber_198 hr = new HouseRobber_198();
30+
System.out.println(hr.rob(new int[] {}));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)