leecode更新
This commit is contained in:
parent
e2d2f8925f
commit
2345b12dde
|
|
@ -0,0 +1,59 @@
|
|||
package com.markilue.leecode.hot100.interviewHot.listnode;
|
||||
|
||||
import com.markilue.leecode.listnode.ListNode;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100.interviewHot.listnode
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-05-11 11:26
|
||||
*@Description: TODO 力扣83 删除排序链表中的重复元素:重复的留一个
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class LC_83_DeleteDuplicates {
|
||||
|
||||
|
||||
//一个一个删
|
||||
public ListNode deleteDuplicates(ListNode head) {
|
||||
if (head == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode fake = new ListNode(-101);
|
||||
fake.next = head;
|
||||
ListNode cur = fake;
|
||||
|
||||
while (cur.next != null) {
|
||||
if (cur.val == cur.next.val) {
|
||||
cur.next = cur.next.next;
|
||||
} else {
|
||||
cur = cur.next;
|
||||
}
|
||||
}
|
||||
|
||||
return fake.next;
|
||||
|
||||
}
|
||||
|
||||
|
||||
//找到重复的,连环删除一系列(通用)
|
||||
public ListNode deleteDuplicates1(ListNode head) {
|
||||
if (head == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode fake = new ListNode(-101);
|
||||
fake.next = head;
|
||||
ListNode cur = fake;
|
||||
while (cur.next != null) {
|
||||
if (cur.val == cur.next.val) {
|
||||
ListNode temp = cur.next;
|
||||
while (temp.next != null && temp.val == temp.next.val) {
|
||||
temp = temp.next;
|
||||
}
|
||||
cur.next = temp.next;
|
||||
} else {
|
||||
cur = cur.next;
|
||||
}
|
||||
}
|
||||
return fake.next;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.markilue.leecode.hot100.interviewHot.listnode;
|
||||
|
||||
import com.markilue.leecode.listnode.ListNode;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100.interviewHot.listnode
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-05-11 11:38
|
||||
*@Description: TODO 力扣82 删除排序链表中的重复元素II:重复全删
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class LC_84_DeleteDuplicatesII {
|
||||
|
||||
public ListNode deleteDuplicates(ListNode head) {
|
||||
ListNode fake = new ListNode(-101);
|
||||
fake.next = head;
|
||||
ListNode cur = fake;
|
||||
|
||||
while (cur != null && cur.next != null && cur.next.next != null) {
|
||||
if (cur.next.val == cur.next.next.val) {
|
||||
ListNode temp = cur.next;
|
||||
while (temp.next != null && temp.val == temp.next.val) {
|
||||
temp = temp.next;
|
||||
}
|
||||
cur.next = temp.next;
|
||||
} else {
|
||||
cur = cur.next;
|
||||
}
|
||||
}
|
||||
return fake.next;
|
||||
}
|
||||
}
|
||||
|
|
@ -70,7 +70,7 @@ public class T23_42_Trap {
|
|||
}
|
||||
Integer left = stack.peek();
|
||||
int curWidth = i - left - 1;
|
||||
int curHeight = Math.min(height[left], height[i])-height[top];
|
||||
int curHeight = Math.min(height[left], height[i]) - height[top];
|
||||
result += curHeight * curWidth;
|
||||
}
|
||||
stack.push(i);
|
||||
|
|
@ -79,4 +79,49 @@ public class T23_42_Trap {
|
|||
return result;
|
||||
|
||||
}
|
||||
|
||||
//二刷:使用单调栈法
|
||||
public int trap2(int[] height) {
|
||||
LinkedList<Integer> stack = new LinkedList<>();
|
||||
int result = 0;
|
||||
|
||||
for (int i = 0; i < height.length; i++) {
|
||||
while (!stack.isEmpty() && height[stack.peek()] < height[i]) {
|
||||
Integer top = stack.pop();
|
||||
if (stack.isEmpty()) {
|
||||
continue;//stack必须有左边的元素才能计算接雨水量
|
||||
}
|
||||
Integer left = stack.peek();
|
||||
result += (i - left - 1) * (Math.min(height[i], height[left]) - height[top]);//宽*高
|
||||
}
|
||||
stack.push(i);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
//二刷:双指针法
|
||||
public int trap3(int[] height) {
|
||||
int left = 0;
|
||||
int right = height.length - 1;
|
||||
int leftMax = height[left];
|
||||
int rightMax = height[right];
|
||||
int result = 0;
|
||||
|
||||
while (left < right) {
|
||||
leftMax = Math.max(leftMax, height[left]);
|
||||
rightMax = Math.max(rightMax, height[right]);
|
||||
if (leftMax < rightMax) {
|
||||
//左边一定能积水
|
||||
result += (leftMax - height[left]);
|
||||
left++;
|
||||
} else {
|
||||
result += (rightMax - height[right]);
|
||||
right--;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,8 +20,8 @@ public class T34_75_SortColors {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
int[] nums = {2,0,2,1,1,0};
|
||||
sortColors(nums);
|
||||
int[] nums = {2, 0, 2, 1, 1, 0};
|
||||
sortColors1(nums);
|
||||
System.out.println(Arrays.toString(nums));
|
||||
}
|
||||
|
||||
|
|
@ -50,4 +50,29 @@ public class T34_75_SortColors {
|
|||
nums[i] = nums[j];
|
||||
nums[j] = temp;
|
||||
}
|
||||
|
||||
|
||||
//二刷:遇上1就直接交换,遇上0就先交换,在判断要不要和1换
|
||||
public void sortColors1(int[] nums) {
|
||||
|
||||
int index0 = 0;
|
||||
int index1 = 0;
|
||||
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == 0) {
|
||||
swap(nums, i, index0);
|
||||
//判断是不是把1给交换到后面了
|
||||
if (index0 < index1) {
|
||||
//把1交换到后面了,交换回来
|
||||
swap(nums, i, index1);
|
||||
}
|
||||
index0++;
|
||||
index1++;
|
||||
} else if (nums[i] == 1) {
|
||||
swap(nums, i, index1);
|
||||
index1++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
package com.markilue.leecode.hot100.second;
|
||||
|
||||
import com.markilue.leecode.listnode.ListNode;
|
||||
import com.markilue.leecode.listnode.ListNodeUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100.second
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-05-11 11:00
|
||||
*@Description: TODO 力扣148 排序链表:
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T56_148_SortList {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
ListNode root = ListNodeUtils.build(new int[]{4, 2, 1, 3});
|
||||
sortList(root);
|
||||
}
|
||||
|
||||
//利用count直接构造:提前记录每一个数出现的次数,直接构造
|
||||
public ListNode sortList(ListNode head) {
|
||||
|
||||
int min = Integer.MAX_VALUE;
|
||||
int max = Integer.MIN_VALUE;
|
||||
ListNode cur = head;
|
||||
while (cur != null) {
|
||||
if (cur.val < min) min = cur.val;
|
||||
if (cur.val > max) max = cur.val;
|
||||
cur = cur.next;
|
||||
}
|
||||
int[] count = new int[max - min + 1];
|
||||
ListNode temp = head;
|
||||
while (temp != null) {
|
||||
count[temp.val - min]++;
|
||||
temp = temp.next;
|
||||
}
|
||||
ListNode fake = new ListNode();
|
||||
ListNode tt = fake;
|
||||
for (int i = 0; i < count.length; i++) {
|
||||
int num = count[i];
|
||||
while (num-- > 0) {
|
||||
tt.next = new ListNode(i + min);
|
||||
tt = tt.next;
|
||||
}
|
||||
}
|
||||
return fake.next;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue