Skip to content

Commit abfdac6

Browse files
committed
20190711
1 parent 372335e commit abfdac6

10 files changed

+93
-20
lines changed

code/lc148.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public ListNode sortList(ListNode head) {
2727
}
2828
ListNode slow = head;
2929
ListNode fast = head.next;
30-
while( fast.next!=null && fast.next.next!=null ){ //把链表分成两半
30+
while( fast!=null && fast.next!=null ){ //把链表分成两半
3131
slow = slow.next;
3232
fast = fast.next.next;
3333
}
3434
ListNode l2 = sortList(slow.next);
35-
slow.next = null;
35+
slow.next = null; //别忘了这要断开
3636
ListNode l1 = sortList(head);
3737
return mergeList(l1, l2);
3838
}

code/lc153.java

+6-13
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,20 @@
44
* 题意:反转数组找最小值
55
* 难度:Medium
66
* 分类:Array, Binary Search
7-
* 思路:
7+
* 思路:想清楚,不用那么多判断
88
* Tips:边界条件想清楚
9+
* nums[mid]和nums[right]比就行了
910
*/
1011
public class lc153 {
1112
public int findMin(int[] nums) {
1213
int left = 0;
1314
int right = nums.length-1;
1415
while(left<right){
1516
int mid = (left+right)/2;
16-
if(nums[left]<nums[mid]){ //左边有序
17-
if(nums[left]<nums[right]){ //在左边找
18-
right = mid-1;
19-
}else{
20-
left = mid;
21-
}
22-
}else{ //右边有序
23-
if(nums[right]<nums[mid]){ //边界条件想清楚
24-
left = mid+1;
25-
}else{
26-
right = mid;
27-
}
17+
if(nums[mid]<nums[right]){
18+
right = mid; //这不加1
19+
}else if(nums[mid]>nums[right]){
20+
left = mid+1;
2821
}
2922
}
3023
return nums[left];

code/lc206.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* 分类:Linked List
77
* 思路:2中方法:设置一个快走一步的快指针,注意赋值操作顺序。还有一种递归的方法。
88
* Tips:递归的方法有点绕,多看下
9+
* lc25, lc206
910
*/
1011
public class lc206 {
1112
public class ListNode {

code/lc236.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 分类:Tree
1010
* 思路:递归,迭代两种方法
1111
* Tips:注意递归时怎么返回。很经典的题目。
12+
*
1213
*/
1314
public class lc236 {
1415
public class TreeNode {
@@ -18,7 +19,7 @@ public class TreeNode {
1819
TreeNode(int x) { val = x; }
1920
}
2021
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {//递归
21-
if( root==null || root==p || root==q )
22+
if( root==null || root==p || root==q ) //注意这个条件
2223
return root;
2324
TreeNode left = lowestCommonAncestor(root.left, p, q);
2425
TreeNode right = lowestCommonAncestor(root.right, p, q);

code/lc25.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package code;
2+
/*
3+
* 25. Reverse Nodes in k-Group
4+
* 题意:每k个反转一下,不足k的不反转,直接接上
5+
* 难度:Hard
6+
* 分类:Linked List
7+
* 思路:递归调用反转,反转完下一段的返回节点,节点这一段上
8+
* Tips:lc25, lc206
9+
*/
10+
public class lc25 {
11+
public class ListNode {
12+
int val;
13+
ListNode next;
14+
ListNode(int x) {
15+
val = x;
16+
}
17+
}
18+
19+
public ListNode reverseKGroup(ListNode head, int k) {
20+
ListNode curr = head;
21+
int count = 0;
22+
while (curr != null && count != k) { // 找下一段要反转的起始节点
23+
curr = curr.next;
24+
count++;
25+
}
26+
if (count == k) { // 不足k的不执行
27+
curr = reverseKGroup(curr, k); // 递归调用,反转下一段,并返回翻转后的头结点
28+
// 反转当前段
29+
while (count-- > 0) { // 链表反转的思路
30+
ListNode tmp = head.next;
31+
head.next = curr;
32+
curr = head;
33+
head = tmp;
34+
}
35+
head = curr;
36+
}
37+
return head;
38+
}
39+
}

code/lc300.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public int lengthOfLIS(int[] nums) {
1414
if(nums.length<2)
1515
return nums.length;
1616
int[] dp = new int[nums.length]; //dp[i] 存储以nums[i]结尾的最大长度
17-
Arrays.fill(dp,1);
17+
Arrays.fill(dp,1); //记住fill 1
1818
int res = 1;
1919
for (int i = 1; i < nums.length ; i++) {
2020
for (int j = 0; j < i ; j++) {

code/lc315.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* 思路:两种思路,一种用二叉搜索树这类数据结构 https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76580/9ms-short-Java-BST-solution-get-answer-when-building-BST
1212
* 一种归并排序的思路,归并的时候统计左右交换数目。如果一个数从这个数的右边交换到左边,则+1。因为有重复数字,所以用将index进行排序
1313
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76583/11ms-JAVA-solution-using-merge-sort-with-explanation
14-
* 再有一种复杂度稍微高点的思路,从后往前插入排序,插入的时候二分搜索
14+
* 再有一种复杂度稍微高点的思路,从后往前插入排序,插入的时候二分搜索,插入其实是不行的,因为插入操作以后还要移动value位置,又是一个O(N)的操作
1515
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76576/My-simple-AC-Java-Binary-Search-code
1616
* Tips:好难呀,我日!
1717
*/

code/lc72.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public class lc72 {
1111
public static void main(String[] args) {
12-
System.out.println(minDistance("intention","execution"));
12+
System.out.println(minDistance("horse","ros"));
1313
}
1414
public static int minDistance(String word1, String word2) {
1515
int[][] dp = new int[word1.length()+1][word2.length()+1];

code/lc75.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void main(String[] args) {
1818
public static void sortColors(int[] nums) {
1919
int l = 0;
2020
int r = nums.length-1;
21-
for (int i = 0; i <= r ; i++) { // i<r而不是length, 否则又换回来了
21+
for (int i = 0; i <= r ; i++) { // i<=r而不是length, 否则又换回来了
2222
if(nums[i]==0 && i!=l ){ //避免自己与自己交换
2323
int temp = nums[l];
2424
nums[l] = nums[i];

code/lc912.java

+39
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,43 @@ public static void merge(int[] nums, int left, int mid, int right){
7878
cur++;
7979
}
8080
}
81+
82+
//堆排
83+
public int[] sortArray3(int[] nums) {
84+
//建堆
85+
int pos = nums.length/2;
86+
while(pos>=0){
87+
AdjustTree(nums, nums.length-1, pos);
88+
pos--;
89+
}
90+
//排序,每次找出最大的,从堆中去掉,调整堆
91+
int cur = nums.length-1;
92+
while(cur>=0){
93+
//交换位置
94+
int temp = nums[0];
95+
nums[0] = nums[cur];
96+
nums[cur] = temp;
97+
cur--;
98+
AdjustTree(nums, cur, 0);
99+
}
100+
return nums;
101+
}
102+
103+
public void AdjustTree(int[] nums, int len, int pos){ //调整堆
104+
int pos_exchange = pos*2+1;
105+
while(pos_exchange<=len){ //left
106+
if(pos_exchange+1<=len&&nums[pos_exchange+1]>nums[pos_exchange]){ //比较左右节点,挑出来大的
107+
pos_exchange += 1;
108+
}
109+
if(nums[pos_exchange]>nums[pos]){ //和父节点比较
110+
int temp = nums[pos];
111+
nums[pos] = nums[pos_exchange];
112+
nums[pos_exchange] = temp;
113+
pos = pos_exchange;
114+
pos_exchange = pos*2+1;
115+
}else{
116+
break;
117+
}
118+
}
119+
}
81120
}

0 commit comments

Comments
 (0)