Skip to content

Commit 22819ff

Browse files
committed
20190410
1 parent d0d7673 commit 22819ff

File tree

4 files changed

+86
-19
lines changed

4 files changed

+86
-19
lines changed

code/lc148.java

+43
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package code;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
26
/*
37
* 148. Sort List
48
* 题意:链表排序
59
* 难度:Medium
610
* 分类:Linked List, Sort
711
* 思路:快慢指针把链表分成两半,在merge两个链表
12+
* 快排方法自己不会写,记下思路
13+
* 快排尝试直接以ListNode互相交换位置,节点间的指向会乱掉的,当递归子情况的时候会修改指针,父方法不知道子调用做了哪些操作
14+
* https://www.cnblogs.com/morethink/p/8452914.html
815
* Tips:空间复杂度不是O(1)的,但是几个高票答案都是这样写的,面试给出这样的代码应该也够了
916
*/
1017
public class lc148 {
@@ -52,4 +59,40 @@ public ListNode mergeList(ListNode l1, ListNode l2){
5259
}
5360
return head.next;
5461
}
62+
63+
public ListNode sortList2(ListNode head) { //链表快排
64+
//采用快速排序
65+
quickSort(head, null);
66+
return head;
67+
}
68+
public void quickSort(ListNode head, ListNode end) {
69+
if (head != end) {
70+
ListNode node = partion(head, end);
71+
quickSort(head, node);
72+
quickSort(node.next, end);
73+
}
74+
}
75+
76+
public ListNode partion(ListNode head, ListNode end) {
77+
ListNode p1 = head, p2 = head.next;
78+
79+
//走到末尾才停
80+
while (p2 != end) { //p1与p2间都是大于pivot的数
81+
if (p2.val < head.val) { //lc922 类似的思想, 把小于的值放到该放的位置上
82+
p1 = p1.next;
83+
84+
int temp = p1.val;
85+
p1.val = p2.val;
86+
p2.val = temp;
87+
}
88+
p2 = p2.next;
89+
}
90+
91+
//与pivot交换下位置
92+
int temp = p1.val;
93+
p1.val = head.val;
94+
head.val = temp;
95+
96+
return p1; //返回
97+
}
5598
}

code/lc51.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 难度:Hard
66
* 分类:Backtracking
77
* 思路:回溯+判断,注意怎么判断两个斜线方向
8-
* Tips:
8+
* Tips:lc52
99
*/
1010
import java.util.ArrayList;
1111
import java.util.HashSet;

code/lc52.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package code;
2+
3+
import java.util.HashSet;
4+
/*
5+
* 52. N-Queens II
6+
* 题意:8皇后问题
7+
* 难度:Hard
8+
* 分类:Backtracking
9+
* 思路:和 lc51 一样,输出变为种类数,反而简单了
10+
* Tips:
11+
*/
12+
public class lc52 {
13+
HashSet<String> hs = new HashSet();
14+
int res = 0;
15+
public int totalNQueens(int n) {
16+
dfs(n, 0);
17+
return res;
18+
}
19+
20+
public void dfs(int n, int row){
21+
if(row==n){
22+
res++;
23+
}
24+
for (int i = 0; i < n ; i++) {
25+
if(isValid(row, i)){
26+
int a = row+i; // row+col 作为key
27+
int b = row-i; // row-col 作为key
28+
hs.add("row"+row); hs.add("col"+i); hs.add("k1"+a); hs.add("k2"+b);
29+
dfs(n, row+1);
30+
hs.remove("row"+row); hs.remove("col"+i); hs.remove("k1"+a); hs.remove("k2"+b); //别忘了删掉
31+
}
32+
}
33+
}
34+
35+
public boolean isValid(int row, int col){
36+
int a = row+col;
37+
int b = row-col;
38+
if(hs.contains("row"+row)||hs.contains("col"+col)||hs.contains("k1"+a)||hs.contains("k2"+b)) return false;
39+
return true;
40+
}
41+
}

code/lc923.java

+1-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* 分类:Two Pointers
1010
* 思路:由于本题只要求给出多少种Int值,所以不一定非要用3Sum的思路,有很多更简答的方法
1111
* 3种思路
12-
* Tips:
12+
* Tips:lc15, lc16, lc923
1313
*/
1414
public class lc923 {
1515
public int threeSumMulti(int[] A, int target) {
@@ -53,21 +53,4 @@ else if(A[left]==A[right]) { //如果相等,则直接 C N 取 2,计算出
5353
return res;
5454
}
5555

56-
public int threeSumMulti3(int[] A, int target) { //直接数学计算
57-
long[] c = new long[101];
58-
for (int a : A) c[a]++;
59-
long res = 0;
60-
for (int i = 0; i <= 100; i++) // 题目给了值不超过100
61-
for (int j = i; j <= 100; j++) {
62-
int k = target - i - j;
63-
if (k > 100 || k < 0) continue;
64-
if (i == j && j == k)
65-
res += c[i] * (c[i] - 1) * (c[i] - 2) / 6;
66-
else if (i == j && j != k)
67-
res += c[i] * (c[i] - 1) / 2 * c[k];
68-
else if (j < k)
69-
res += c[i] * c[j] * c[k];
70-
}
71-
return (int)(res % (1e9 + 7));
72-
}
7356
}

0 commit comments

Comments
 (0)