提交 abfdac66 编写于 作者: L liu13

20190711

上级 372335e7
......@@ -27,12 +27,12 @@ public class lc148 {
}
ListNode slow = head;
ListNode fast = head.next;
while( fast.next!=null && fast.next.next!=null ){ //把链表分成两半
while( fast!=null && fast.next!=null ){ //把链表分成两半
slow = slow.next;
fast = fast.next.next;
}
ListNode l2 = sortList(slow.next);
slow.next = null;
slow.next = null; //别忘了这要断开
ListNode l1 = sortList(head);
return mergeList(l1, l2);
}
......
......@@ -4,8 +4,9 @@ package code;
* 题意:反转数组找最小值
* 难度:Medium
* 分类:Array, Binary Search
* 思路:
* 思路:想清楚,不用那么多判断
* Tips:边界条件想清楚
* nums[mid]和nums[right]比就行了
*/
public class lc153 {
public int findMin(int[] nums) {
......@@ -13,18 +14,10 @@ public class lc153 {
int right = nums.length-1;
while(left<right){
int mid = (left+right)/2;
if(nums[left]<nums[mid]){ //左边有序
if(nums[left]<nums[right]){ //在左边找
right = mid-1;
}else{
left = mid;
}
}else{ //右边有序
if(nums[right]<nums[mid]){ //边界条件想清楚
left = mid+1;
}else{
right = mid;
}
if(nums[mid]<nums[right]){
right = mid; //这不加1
}else if(nums[mid]>nums[right]){
left = mid+1;
}
}
return nums[left];
......
......@@ -6,6 +6,7 @@ package code;
* 分类:Linked List
* 思路:2中方法:设置一个快走一步的快指针,注意赋值操作顺序。还有一种递归的方法。
* Tips:递归的方法有点绕,多看下
* lc25, lc206
*/
public class lc206 {
public class ListNode {
......
......@@ -9,6 +9,7 @@ import java.util.*;
* 分类:Tree
* 思路:递归,迭代两种方法
* Tips:注意递归时怎么返回。很经典的题目。
*
*/
public class lc236 {
public class TreeNode {
......@@ -18,7 +19,7 @@ public class lc236 {
TreeNode(int x) { val = x; }
}
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {//递归
if( root==null || root==p || root==q )
if( root==null || root==p || root==q ) //注意这个条件
return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
......
package code;
/*
* 25. Reverse Nodes in k-Group
* 题意:每k个反转一下,不足k的不反转,直接接上
* 难度:Hard
* 分类:Linked List
* 思路:递归调用反转,反转完下一段的返回节点,节点这一段上
* Tips:lc25, lc206
*/
public class lc25 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public ListNode reverseKGroup(ListNode head, int k) {
ListNode curr = head;
int count = 0;
while (curr != null && count != k) { // 找下一段要反转的起始节点
curr = curr.next;
count++;
}
if (count == k) { // 不足k的不执行
curr = reverseKGroup(curr, k); // 递归调用,反转下一段,并返回翻转后的头结点
// 反转当前段
while (count-- > 0) { // 链表反转的思路
ListNode tmp = head.next;
head.next = curr;
curr = head;
head = tmp;
}
head = curr;
}
return head;
}
}
......@@ -14,7 +14,7 @@ public class lc300 {
if(nums.length<2)
return nums.length;
int[] dp = new int[nums.length]; //dp[i] 存储以nums[i]结尾的最大长度
Arrays.fill(dp,1);
Arrays.fill(dp,1); //记住fill 1
int res = 1;
for (int i = 1; i < nums.length ; i++) {
for (int j = 0; j < i ; j++) {
......
......@@ -11,7 +11,7 @@ import java.util.List;
* 思路:两种思路,一种用二叉搜索树这类数据结构 https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76580/9ms-short-Java-BST-solution-get-answer-when-building-BST
* 一种归并排序的思路,归并的时候统计左右交换数目。如果一个数从这个数的右边交换到左边,则+1。因为有重复数字,所以用将index进行排序
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76583/11ms-JAVA-solution-using-merge-sort-with-explanation
* 再有一种复杂度稍微高点的思路,从后往前插入排序,插入的时候二分搜索
* 再有一种复杂度稍微高点的思路,从后往前插入排序,插入的时候二分搜索,插入其实是不行的,因为插入操作以后还要移动value位置,又是一个O(N)的操作
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76576/My-simple-AC-Java-Binary-Search-code
* Tips:好难呀,我日!
*/
......
......@@ -9,7 +9,7 @@ package code;
*/
public class lc72 {
public static void main(String[] args) {
System.out.println(minDistance("intention","execution"));
System.out.println(minDistance("horse","ros"));
}
public static int minDistance(String word1, String word2) {
int[][] dp = new int[word1.length()+1][word2.length()+1];
......
......@@ -18,7 +18,7 @@ public class lc75 {
public static void sortColors(int[] nums) {
int l = 0;
int r = nums.length-1;
for (int i = 0; i <= r ; i++) { // i<r而不是length, 否则又换回来了
for (int i = 0; i <= r ; i++) { // i<=r而不是length, 否则又换回来了
if(nums[i]==0 && i!=l ){ //避免自己与自己交换
int temp = nums[l];
nums[l] = nums[i];
......
......@@ -78,4 +78,43 @@ public class lc912 {
cur++;
}
}
//堆排
public int[] sortArray3(int[] nums) {
//建堆
int pos = nums.length/2;
while(pos>=0){
AdjustTree(nums, nums.length-1, pos);
pos--;
}
//排序,每次找出最大的,从堆中去掉,调整堆
int cur = nums.length-1;
while(cur>=0){
//交换位置
int temp = nums[0];
nums[0] = nums[cur];
nums[cur] = temp;
cur--;
AdjustTree(nums, cur, 0);
}
return nums;
}
public void AdjustTree(int[] nums, int len, int pos){ //调整堆
int pos_exchange = pos*2+1;
while(pos_exchange<=len){ //left
if(pos_exchange+1<=len&&nums[pos_exchange+1]>nums[pos_exchange]){ //比较左右节点,挑出来大的
pos_exchange += 1;
}
if(nums[pos_exchange]>nums[pos]){ //和父节点比较
int temp = nums[pos];
nums[pos] = nums[pos_exchange];
nums[pos_exchange] = temp;
pos = pos_exchange;
pos_exchange = pos*2+1;
}else{
break;
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册