This commit is contained in:
fanxb 2021-09-17 23:10:28 +08:00
parent 3eb45138ef
commit 154a744fdc
8 changed files with 468 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package com.fanxb.common;
import java.util.Arrays;
import java.util.List;
/**
* @author fanxb
* Date: 2020/6/9 15:10
*/
public class Q120 {
public int minimumTotal(List<List<Integer>> triangle) {
int lineNum = triangle.size();
int[][] dp = new int[lineNum][triangle.get(lineNum - 1).size()];
for (int i = 0; i < lineNum; i++) {
List<Integer> line = triangle.get(i);
for (int j = 0; j < i + 1; j++) {
if (i == 0) {
dp[i][j] = line.get(j);
} else if (j == 0) {
dp[i][j] = dp[i - 1][j] + line.get(j);
} else if (j == line.size() - 1) {
dp[i][j] = dp[i - 1][j - 1] + line.get(j);
} else {
dp[i][j] = Math.min(dp[i - 1][j - 1], dp[i - 1][j]) + line.get(j);
}
}
}
int min = dp[lineNum - 1][0];
for (int i = 1; i < lineNum; i++) {
if (dp[lineNum - 1][i] < min) {
min = dp[lineNum - 1][i];
}
}
return min;
}
public static void main(String[] args) {
List<Integer> l1 = Arrays.asList(2);
List<Integer> l2 = Arrays.asList(3, 4);
List<Integer> l3 = Arrays.asList(6, 5, 7);
List<Integer> l4 = Arrays.asList(4, 1, 8, 3);
List<List<Integer>> l5 = Arrays.asList(Arrays.asList(-10));
System.out.println(new Q120().minimumTotal(l5));
}
}

View File

@ -0,0 +1,133 @@
package com.fanxb.common;
import java.util.Stack;
/**
* @author fanxb
* @date 2021-08-31-下午4:50
*/
public class Q232 {
public static void main(String[] args) {
BetterMyQueue myQueue = new BetterMyQueue();
myQueue.push(1);
myQueue.push(2);
System.out.println(myQueue.peek());
System.out.println(myQueue.pop());
System.out.println(myQueue.empty());
}
/**
* main,每次push元素都放到main中并把顺序排列好方法是先把现有元素转移到help中然后将新元素放到main里再把help里的元素转移回来
*
* @author fanxb
* @date 2021/8/31 下午5:08
*/
private static class MyQueue {
private Stack<Integer> main;
private Stack<Integer> help;
/**
* Initialize your data structure here.
*/
public MyQueue() {
main = new Stack<>();
help = new Stack<>();
}
/**
* Push element x to the back of queue.
*/
public void push(int x) {
while (!main.isEmpty()) {
help.push(main.pop());
}
main.push(x);
while (!help.isEmpty()) {
main.push(help.pop());
}
}
/**
* Removes the element from in front of queue and returns that element.
*/
public int pop() {
return main.pop();
}
/**
* Get the front element.
*/
public int peek() {
return main.peek();
}
/**
* Returns whether the queue is empty.
*/
public boolean empty() {
return main.isEmpty();
}
}
/**
* @author fanxb
* @date 2021/8/31 下午5:08
*/
private static class BetterMyQueue {
private Stack<Integer> input;
private Stack<Integer> output;
/**
* Initialize your data structure here.
*/
public BetterMyQueue() {
input = new Stack<>();
output = new Stack<>();
}
/**
* Push element x to the back of queue.
*/
public void push(int x) {
input.push(x);
}
/**
* Removes the element from in front of queue and returns that element.
*/
public int pop() {
if (output.isEmpty()) {
while (!input.empty()) {
output.push(input.pop());
}
}
return output.pop();
}
/**
* Get the front element.
*/
public int peek() {
if (output.isEmpty()) {
while (!input.empty()) {
output.push(input.pop());
}
}
return output.peek();
}
/**
* Returns whether the queue is empty.
*/
public boolean empty() {
return input.isEmpty();
}
}
}

View File

@ -0,0 +1,52 @@
package com.fanxb.common;
import java.util.*;
import java.util.stream.Collectors;
/**
* 定义转移方程dp[i][j]为长度为i+1的窗口在位置j的最大值
* 已知dp[0][j]=num[j]
* 转移方程dp[i][j]=max(dp[i-1][j],num[i+j])
*
* @author fanxb
* @date 2021-08-31-下午11:15
*/
public class Q239 {
public int[] maxSlidingWindow(int[] nums, int k) {
//使用链表构造一个递减的单调链表
LinkedList<Integer> indexList = new LinkedList<>();
int[] res = new int[nums.length - k + 1];
for (int i = 0; i < nums.length; i++) {
while (!indexList.isEmpty() && nums[indexList.peekLast()] < nums[i]) {
indexList.removeLast();
}
indexList.addLast(i);
if (indexList.getFirst() < i - k + 1) {
indexList.removeFirst();
}
if (i >= k - 1) {
res[i - k + 1] = nums[indexList.getFirst()];
}
}
return res;
}
public int[] lowMaxSlidingWindow(int[] nums, int k) {
int n = nums.length;
int[] current = Arrays.copyOf(nums, n - k + 1);
for (int i = 1; i < k; i++) {
for (int j = 0; j < n - k + 1; j++) {
if (nums[i + j] > current[j]) {
current[j] = nums[i + j];
}
}
}
return current;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(new Q239().maxSlidingWindow(new int[]{-7, -8, 7, 5, 7, 1, 6, 0}, 4)));
}
}

View File

@ -0,0 +1,32 @@
package com.fanxb.common;
import java.util.Stack;
/**
* 定义dp[i]为以nums[i]结尾的子序列最大长度
*
* @author fanxb
* @date 2021-08-31-下午11:15
*/
public class Q300 {
public int lengthOfLIS(int[] nums) {
int n = nums.length, res = 1;
int[] dp = new int[n];
dp[0] = 1;
for (int i = 1; i < n; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
res = Math.max(res, dp[i]);
}
return res;
}
public static void main(String[] args) {
System.out.println(new Q300().lengthOfLIS(new int[]{0, 1, 0, 3, 2, 3}));
}
}

View File

@ -0,0 +1,48 @@
package com.fanxb.common;
/**
* 定义dp[i][j]表示从i到j的字符串是否为回文串
* 已知如下情况
* 1. 当j-i==0时dp[i][j]是回文串
* 2. 当j-i==1时判断s[1]是否等于s[j]
* 3. dp[i][j]=dp[i+1][j-1] && s[i]==s[j]
* 注意坑点由于第三个转移情况是从i到i+1所以循环的处理很重要不能直接循环i从0-n,j从i到n,这样会出现dp[i+1][j-1]的状态还不知道的情况
*
* @author fanxb
* @date 2021-08-31-下午10:31
*/
public class Q5 {
public String longestPalindrome(String s) {
int n = s.length(), max = 0, resI = 0, resJ = 0;
if (n < 2) {
return s;
}
boolean[][] dp = new boolean[n][n];
//先从长度为1的字符串开始处理
for (int length = 1; length <= s.length(); length++) {
//i左边界j右边界
for (int i = 0; i < s.length(); i++) {
int j = i + length - 1;
if (j >= s.length()) {
continue;
}
if (length == 1) {
dp[i][j] = true;
} else if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = length == 2 || dp[i + 1][j - 1];
}
if (dp[i][j] && j - i + 1 > max) {
max = j - i + 1;
resI = i;
resJ = j;
}
}
}
return s.substring(resI, resJ + 1);
}
public static void main(String[] args) {
System.out.println(new Q5().longestPalindrome("babad"));
}
}

View File

@ -0,0 +1,46 @@
package com.fanxb.common;
/**
* 编辑距离可以对任意一个单词增加一个字母删除一个字母替换一个字母
* a增加一个字母相当于b删除一个字母
* b增加一个字母相当于a删除一个字母
* a替换一个字母相当于b替换一个字母
* <p>
* 字符串a,字符串b
* <p>
* 定义dp[i][j]为a中前i个字母变化为b中前j个字母需要的步骤
* 考虑上面三种情况存在以下三种转移情况
* dp[i][j]=dp[i-1][j]+1;字母a增加一个字母
* dp[i][j]=dp[i][j-1]+1;字母b增加一个字母
* dp[i][j]=dp[i-1][j-1]+(a[i]==a[j]);字母替换由于a[i],a[j]可能相同不需要替换所以不一定要走替换这一步
* <p>
* 极端情况
* dp[0][j]=j;
* dp[i][0]=i;
*
* @author fanxb
* @date 2021-08-31-下午10:15
*/
public class Q72 {
public int minDistance(String word1, String word2) {
int length1 = word1.length(), length2 = word2.length();
int[][] dp = new int[length1 + 1][length2 + 1];
for (int i = 0; i <= length1; i++) {
for (int j = 0; j <= length2; j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else {
dp[i][j] = Math.min(dp[i - 1][j] + 1, Math.min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + (word1.charAt(i - 1) == word2.charAt(j - 1) ? 0 : 1)));
}
}
}
return dp[length1][length2];
}
public static void main(String[] args) {
System.out.println(new Q72().minDistance("horse", "ros"));
}
}

View File

@ -0,0 +1,46 @@
package com.fanxb.common;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Stack;
/**
* @author fanxb
* @date 2021-08-31-下午11:15
*/
public class Q84 {
public int largestRectangleArea(int[] heights) {
int n = heights.length;
Stack<Integer> stack = new Stack<>();
int res = 0;
for (int i = 0; i < n; i++) {
int currentHeight = heights[i];
if (!stack.isEmpty()) {
//说明下一个是更小的一定能确定一些高度的面积
while (!stack.isEmpty() && heights[stack.peek()] > currentHeight) {
int height = heights[stack.pop()];
while (!stack.isEmpty() && heights[stack.peek()] == height) {
stack.pop();
}
int width = stack.isEmpty() ? i : (i - stack.peek() - 1);
res = Math.max(res, height * width);
}
}
stack.push(i);
}
while (!stack.isEmpty()) {
int height = heights[stack.pop()];
while (!stack.isEmpty() && heights[stack.peek()] == height) {
stack.pop();
}
int width = stack.isEmpty() ? n : (n - stack.peek() - 1);
res = Math.max(res, height * width);
}
return res;
}
public static void main(String[] args) {
System.out.println(new Q84().largestRectangleArea(new int[]{2, 1, 0, 2}));
}
}

View File

@ -0,0 +1,65 @@
package com.fanxb.common.offer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* 两数相加
*
* @author fanxb
* @date 2021/6/1
**/
public class Q52 {
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
Set<ListNode> map = new HashSet<>(128);
while (headA != null) {
map.add(headA);
headA = headA.next;
}
while (headB != null) {
if (map.contains(headB)) {
return headB;
}
headB = headB.next;
}
return null;
}
public static void main(String[] args) {
ListNode a1 = new ListNode(1);
ListNode a2 = new ListNode(2);
ListNode a3 = new ListNode(3);
ListNode a4 = new ListNode(4);
ListNode a5 = new ListNode(5);
ListNode a6 = new ListNode(6);
ListNode b1 = new ListNode(7);
ListNode b2 = new ListNode(8);
ListNode b3 = new ListNode(9);
a1.next = a2;
a2.next = a3;
a3.next = a4;
a4.next = a5;
a5.next = a6;
b1.next = b2;
b2.next = b3;
b3.next = a4;
System.out.println(new Q52().getIntersectionNode(a1, b1).val);
}
}