add
This commit is contained in:
parent
3eb45138ef
commit
154a744fdc
46
5.leetcode/src/com/fanxb/common/Q120.java
Normal file
46
5.leetcode/src/com/fanxb/common/Q120.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
133
5.leetcode/src/com/fanxb/common/Q232.java
Normal file
133
5.leetcode/src/com/fanxb/common/Q232.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
52
5.leetcode/src/com/fanxb/common/Q239.java
Normal file
52
5.leetcode/src/com/fanxb/common/Q239.java
Normal 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)));
|
||||||
|
}
|
||||||
|
}
|
32
5.leetcode/src/com/fanxb/common/Q300.java
Normal file
32
5.leetcode/src/com/fanxb/common/Q300.java
Normal 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}));
|
||||||
|
}
|
||||||
|
}
|
48
5.leetcode/src/com/fanxb/common/Q5.java
Normal file
48
5.leetcode/src/com/fanxb/common/Q5.java
Normal 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"));
|
||||||
|
}
|
||||||
|
}
|
46
5.leetcode/src/com/fanxb/common/Q72.java
Normal file
46
5.leetcode/src/com/fanxb/common/Q72.java
Normal 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"));
|
||||||
|
}
|
||||||
|
}
|
46
5.leetcode/src/com/fanxb/common/Q84.java
Normal file
46
5.leetcode/src/com/fanxb/common/Q84.java
Normal 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}));
|
||||||
|
}
|
||||||
|
}
|
65
5.leetcode/src/com/fanxb/common/offer/Q52.java
Normal file
65
5.leetcode/src/com/fanxb/common/offer/Q52.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user