Skip to content

Commit 4a655d2

Browse files
committed
添加RBTree实现
1 parent 16873a5 commit 4a655d2

12 files changed

+428
-0
lines changed

leetcode/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>algorithm-study</artifactId>
7+
<groupId>com.mistray</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>leetcode</artifactId>
13+
14+
15+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.mistray.array;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
/**
9+
* @author ZJY(MistRay)
10+
* @Project algorithm-study
11+
* @Package com.mistray.array
12+
* @create 2020年03月02日 17:39
13+
* @Desc
14+
*/
15+
public class HowManyNumbersAreSmallerThanTheCurrentNumber1365 {
16+
17+
public static void main(String[] args) {
18+
int[] nums = {8, 1, 2, 2, 3};
19+
int[] ints = smallerNumbersThanCurrent(nums);
20+
for (int i = 0; i < ints.length; i++) {
21+
System.out.println(ints[i]);
22+
23+
}
24+
}
25+
26+
public static int[] smallerNumbersThanCurrent(int[] nums) {
27+
int length = nums.length;
28+
int[] temp = new int[length];
29+
// 复制并排序
30+
System.arraycopy(nums, 0, temp, 0, length);
31+
Arrays.sort(temp);
32+
Map<Integer, Integer> map = new HashMap<>();
33+
// temp是有序的
34+
for (int i = 0, num = 0, t = temp[0]; i < temp.length; i++) {
35+
// t用来临时存储temp中相邻的重复数据
36+
if (temp[i] == t) {
37+
// 如果map中不包含temp[i]
38+
if (!map.containsKey(temp[i])) {
39+
// 将该元素插入到map中.
40+
map.put(temp[i], 0);
41+
}
42+
} else {
43+
map.put(temp[i], num);
44+
t = temp[i];
45+
}
46+
num++;
47+
}
48+
int[] res = new int[length];
49+
for (int i = 0; i < nums.length; i++) {
50+
res[i] = map.get(nums[i]);
51+
}
52+
return res;
53+
}
54+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.mistray.link;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.link
7+
* @create 2020年03月02日 15:50
8+
* @Desc
9+
*/
10+
public class AddTwoNumbers2 {
11+
12+
public static void main(String[] args) {
13+
ListNode node = new ListNode(5);
14+
node.next = new ListNode(5);
15+
node.next.next = new ListNode(5);
16+
17+
ListNode node2 = new ListNode(6);
18+
node2.next = new ListNode(7);
19+
node2.next.next = new ListNode(6);
20+
21+
22+
addTwoNumbers(node,node2);
23+
}
24+
25+
/**
26+
* 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
27+
* 输出:7 -> 0 -> 8
28+
* 输入:(5 -> 5 -> 3) + (6 -> 7 -> 2)
29+
* 输出:6 -> 3 -> 1
30+
* 原因:342 + 465 = 807
31+
*
32+
* @param l1
33+
* @param l2
34+
* @return
35+
*/
36+
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
37+
ListNode pre = new ListNode(0);
38+
ListNode cur = pre;
39+
int carry = 0;
40+
while(l1 != null || l2 != null) {
41+
int x = l1 == null ? 0 : l1.val;
42+
int y = l2 == null ? 0 : l2.val;
43+
int sum = x + y + carry;
44+
45+
carry = sum / 10;
46+
sum = sum % 10;
47+
cur.next = new ListNode(sum);
48+
49+
cur = cur.next;
50+
if(l1 != null) {
51+
l1 = l1.next;
52+
}
53+
if(l2 != null) {
54+
l2 = l2.next;
55+
}
56+
}
57+
if(carry == 1) {
58+
cur.next = new ListNode(carry);
59+
}
60+
return pre.next;
61+
}
62+
63+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.mistray.link;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.link
7+
* @create 2020年03月03日 15:48
8+
* @Desc
9+
*/
10+
public class IntersectionOfTwoLinkedLists160 {
11+
12+
public static void main(String[] args) {
13+
14+
}
15+
16+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
17+
ListNode a = headA;
18+
ListNode b = headB;
19+
while (a != b) {
20+
a = a.next;
21+
if (a == null) {
22+
a = headB;
23+
}
24+
b = b.next;
25+
if (b == null) {
26+
b = headA;
27+
}
28+
}
29+
return a;
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.mistray.link;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author ZJY(MistRay)
7+
* @Project algorithm-study
8+
* @Package com.mistray.link
9+
* @create 2020年03月03日 14:18
10+
* @Desc
11+
*/
12+
public class LinkedListCycle141 {
13+
14+
public static void main(String[] args) {
15+
16+
}
17+
18+
public boolean hasCycle(ListNode head) {
19+
ListNode fast = head;
20+
ListNode slow = head;
21+
while (fast != null && fast.next != null) {
22+
fast = fast.next.next;
23+
slow = slow.next;
24+
if (fast == slow) {
25+
return true;
26+
}
27+
}
28+
return false;
29+
}
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.mistray.link;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.link
7+
* @create 2020年03月03日 16:23
8+
* @Desc
9+
*/
10+
public class LinkedListCycleii142 {
11+
public static void main(String[] args) {
12+
13+
}
14+
15+
public ListNode detectCycle(ListNode head) {
16+
ListNode slow = head, fast = head;
17+
while (fast.next != null && fast.next.next != null) {
18+
slow = slow.next;
19+
fast = fast.next.next;
20+
if (slow == fast) {
21+
break;
22+
}
23+
}
24+
if (fast.next == null || fast.next.next == null) {
25+
return null;
26+
}
27+
28+
slow = head;
29+
while (slow != fast) {
30+
slow = slow.next;
31+
fast = fast.next;
32+
}
33+
return slow;
34+
}
35+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.mistray.link;
2+
3+
public class ListNode {
4+
int val;
5+
ListNode next;
6+
7+
ListNode(int x) {
8+
val = x;
9+
}
10+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.mistray.link;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.link
7+
* @create 2020年03月02日 11:41
8+
* @Desc
9+
*/
10+
public class MergeTwoSortedLists21 {
11+
12+
13+
public static void main(String[] args) {
14+
15+
}
16+
17+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
18+
// 当l1为空,返回l2
19+
if (l1 == null) {
20+
return l2;
21+
}
22+
23+
// 当l2位空,返回l1
24+
if (l2 == null) {
25+
return l1;
26+
}
27+
28+
// 当l1的值小于l2时
29+
if (l1.val < l2.val) {
30+
l1.next = mergeTwoLists(l1.next, l2);
31+
return l1;
32+
} else {
33+
l2.next = mergeTwoLists(l2.next, l2);
34+
return l2;
35+
}
36+
37+
}
38+
39+
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.mistray.link;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.link
7+
* @create 2020年03月02日 10:57
8+
* @Desc
9+
*/
10+
public class ReverseLinkedList206 {
11+
12+
13+
public static void main(String[] args) {
14+
}
15+
16+
17+
public static ListNode reverseList(ListNode head) {
18+
// 上一个节点
19+
ListNode pre = null;
20+
// 当前节点
21+
ListNode cur = head;
22+
// 承载下一个节点的临时节点
23+
ListNode tmp = null;
24+
25+
while (cur != null) {
26+
// 将当前节点cur的下一个节点赋给临时节点tmp
27+
tmp = cur.next;
28+
// 将上一个节点赋给当前节点的下一个节点
29+
cur.next = pre;
30+
// 将pre和cur后移一位
31+
pre = cur;
32+
cur = tmp;
33+
}
34+
return pre;
35+
}
36+
37+
38+
}

0 commit comments

Comments
 (0)