Skip to content

Commit 510716d

Browse files
committed
对List排序
1 parent 74d496e commit 510716d

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

LeetCode/Doc/同一条直线上的最多的点的数量.md

+63
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,69 @@ return maxCount
5555
5656
完整逻辑请看详细代码。
5757

58+
```java
59+
public int maxPoints(Point[] points) {
60+
61+
int length = points.length;
62+
int maxPointCount = 0;
63+
for (int startIndex = 0; startIndex < length; startIndex++) {
64+
Map<String, Integer> maxPointMap = new HashMap<>();
65+
int commonPointCount = 0;
66+
int tempMaxCount = 1;
67+
for (int endIndex = startIndex + 1; endIndex < length; endIndex++) {
68+
int distanceX = points[startIndex].x - points[endIndex].x;
69+
int distanceY = points[startIndex].y - points[endIndex].y;
70+
71+
72+
if (distanceX == 0 && distanceY == 0) {
73+
commonPointCount += 1;
74+
} else {
75+
String curStrDis;
76+
if (distanceX == 0) {
77+
curStrDis = "distanceX";
78+
} else if (distanceY == 0) {
79+
curStrDis = "distanceY";
80+
} else {
81+
int maxCommonDivisor = commonDivisor(distanceX, distanceY);
82+
if (maxCommonDivisor != 0) {
83+
distanceX = distanceX / maxCommonDivisor;
84+
distanceY = distanceY / maxCommonDivisor;
85+
}
86+
87+
int flag = 0;
88+
89+
if (distanceX < 0) {
90+
distanceX = -distanceX;
91+
flag++;
92+
}
93+
if (distanceY < 0) {
94+
distanceY = -distanceY;
95+
flag++;
96+
}
97+
98+
curStrDis = String.valueOf(distanceX) + distanceY;
99+
if (flag == 1) {
100+
curStrDis = "-" + curStrDis;
101+
}
102+
}
103+
104+
if (maxPointMap.containsKey(curStrDis)) {
105+
tempMaxCount = Math.max(tempMaxCount, maxPointMap.get(curStrDis) + 1);
106+
maxPointMap.put(curStrDis, maxPointMap.get(curStrDis) + 1);
107+
} else {
108+
//初始时put进去2,一个点是起始点,另一个点是当前点
109+
maxPointMap.put(curStrDis, 2);
110+
tempMaxCount = Math.max(tempMaxCount, 2);
111+
}
112+
}
113+
}
114+
maxPointCount = Math.max(maxPointCount, tempMaxCount + commonPointCount);
115+
}
116+
117+
return maxPointCount;
118+
}
119+
```
120+
58121
## 代码
59122

60123
[这里](../src/three/Solution.java)

LeetCode/Doc/排序List.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# sort-list(排序List)
2+
3+
<center>知识点:排序,链表</center>
4+
5+
6+
## 题目描述
7+
8+
Sort a linked list in O(n log n) time using constant space complexity.
9+
10+
使用常量空间在O(n log n)时间复杂度内对链表进行排序。
11+
12+
## 解题思路
13+
14+
首先思考常见排序算法的事件复杂度及空间复杂度(参考:[八大常见排序算法介绍](https://ai-exception.blog.csdn.net/article/details/79053814)):
15+
16+
| 排序方法 | 时间复杂度 | 空间复杂度 |
17+
| ------------ | ------------------------------------- | ---------------- |
18+
| 直接插入排序 | $T(n)=O(n^2)$ | $S(n)=O(1)$ |
19+
| 希尔排序 | $T(n)=O(n^{1.5})$ | $S(n)=O(1)$ |
20+
| 冒泡排序 | $T(n)=O(n^2)$ | $S(n)=O(1)$ |
21+
| 快速排序 | $T(n)=O(nlog_2n)$ | $S(n)=O(log_2n)$ |
22+
| 选择排序 | $S(n)=O(1)$ | $T(n)=O(n^2)$ |
23+
| 堆排序 | $T(n)=O(nlog_2n)$ | $S(n)=O(1)$ |
24+
| 归并排序 | $T(n)=O(nlog_2n)$ | $S(n)=O(n)$ |
25+
| 基数排序 | $T(n)=O(d*n)$ d为排序数中最大数的位数 | $S(n)=O(n)$ |
26+
27+
从上表中可以看出,对数组而言,时间复杂度为$O(nlogn)$的排序算法有:快速排序、堆排序、归并排序。
28+
29+
快速排序和堆排序都对数组中下标的使用较为依赖,相比之下,归并排序更适合链表,再考虑到数组中归并排序的空间复杂度之所以为$O(n)$是因为合并的时候需要用一个辅助数组记录合并后的值以保证原数组中的数字排序不被破坏,而链表可以使用指针保证这一点而不需要额外辅助数组,所以空间复杂度会降为$O(1)$.
30+
31+
综上,使用归并排序可以很好地解决本问题。
32+
33+
大致逻辑:
34+
35+
```
36+
fun listSort(head):
37+
centerNode=head链表的中间节点
38+
//分而治之的思想,分别排序后一半链表和前一半链表
39+
left=listSort(centerNode.next)
40+
//将centerNode的next置位null,保证同样是head为头结点,现在的链表长度为原来的一半
41+
right=centerNode.next=null
42+
listSort(head)
43+
//合并两个子链表
44+
return merge(left,right)
45+
```
46+
47+
这里涉及到找链表中间节点的问题,方法是使用两个指针p、q,初始时p指向head,q指向head.next,当q不为null时:
48+
49+
```
50+
p=p.next
51+
q=q.next.next
52+
```
53+
54+
利用两个指针行走速度快慢的不同最终实现找出链表的中间节点,注意处理好空指针异常即可,这个算法称为快慢指针算法。
55+
56+
57+
## 代码
58+
59+
[这里](../src/four/Solution.java)
Binary file not shown.
Binary file not shown.

LeetCode/src/four/Solution.java

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package four;
2+
3+
/**
4+
* @author dmrfcoder
5+
* @date 2019/4/10
6+
*/
7+
8+
/**
9+
* Definition for singly-linked list.
10+
*/
11+
12+
class ListNode {
13+
int val;
14+
ListNode next;
15+
16+
ListNode(int x) {
17+
val = x;
18+
next = null;
19+
}
20+
}
21+
22+
public class Solution {
23+
public ListNode sortList(ListNode head) {
24+
if (head == null || head.next == null) {
25+
return head;
26+
}
27+
ListNode startNode, endNode;
28+
startNode = head;
29+
endNode = head.next;
30+
31+
while (startNode != null && endNode != null && endNode.next != null) {
32+
startNode = startNode.next;
33+
endNode = endNode.next.next;
34+
}
35+
36+
37+
ListNode right = sortList(startNode.next);
38+
startNode.next = null;
39+
ListNode left = sortList(head);
40+
41+
42+
return mergeList(left, right);
43+
}
44+
45+
private ListNode mergeList(ListNode left, ListNode right) {
46+
ListNode tempListNode = new ListNode(0);
47+
ListNode p = tempListNode;
48+
49+
while (left != null && right != null) {
50+
if (left.val < right.val) {
51+
p.next = left;
52+
p = p.next;
53+
left = left.next;
54+
} else {
55+
p.next = right;
56+
p = p.next;
57+
right = right.next;
58+
}
59+
}
60+
if (left != null) {
61+
p.next = left;
62+
}
63+
if (right != null) {
64+
p.next = right;
65+
}
66+
67+
68+
return tempListNode.next;
69+
}
70+
71+
public static void main(String[] args) {
72+
ListNode head = new ListNode(1);
73+
ListNode node2 = new ListNode(2);
74+
ListNode node3 = new ListNode(3);
75+
ListNode node4 = new ListNode(4);
76+
ListNode node5 = new ListNode(5);
77+
ListNode node6 = new ListNode(6);
78+
ListNode node7 = new ListNode(7);
79+
80+
head.next = node7;
81+
node7.next = node3;
82+
node3.next = node2;
83+
node2.next = node4;
84+
node4.next = node5;
85+
86+
Solution solution = new Solution();
87+
head = solution.sortList(head);
88+
int a = 0;
89+
}
90+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,5 @@
145145
- [minimum-depth-of-binary-tree(求解二叉树的最小深度)](./LeetCode/Doc/二叉树的最小深度.md)
146146
- [evaluate-reverse-polish-notation(计算逆波兰式的值)](./LeetCode/Doc/计算逆波兰式的值.md)
147147
- [max-points-on-a-line(同一条直线上的最多的点的数量)](./LeetCode/Doc/同一条直线上的最多的点的数量.md)
148+
- [sort-list(排序List)](./LeetCode/Doc/排序List.md.md)
148149

0 commit comments

Comments
 (0)