art
This commit is contained in:
parent
43cb0f3cde
commit
aa0f159bcb
|
|
@ -17,3 +17,77 @@
|
|||
/out/production/offer/ds/linklist/*.class
|
||||
/out/production/offer/algorithm/bitoperation/*.class
|
||||
/.idea/inspectionProfiles/Project_Default.xml
|
||||
/out/production/offer/.prettierrc
|
||||
/out/production/offer/leecode/javascript/4.寻找两个正序数组的中位数.js
|
||||
/out/production/offer/leecode/javascript/5.最长回文子串.js
|
||||
/src/leecode/java/6.z-字形变换.java
|
||||
/src/leecode/java/7.整数反转.java
|
||||
/out/production/offer/leecode/python/9.回文数.py
|
||||
/src/leecode/java/10.正则表达式匹配.java
|
||||
/out/production/offer/leecode/javascript/12.整数转罗马数字.js
|
||||
/out/production/offer/leecode/python/14.最长公共前缀.py
|
||||
/out/production/offer/leecode/javascript/17.电话号码的字母组合.js
|
||||
/out/production/offer/leecode/javascript/18.四数之和.js
|
||||
/src/leecode/java/19.删除链表的倒数第-n-个结点.java
|
||||
/src/leecode/java/20.有效的括号.java
|
||||
/out/production/offer/leecode/python/21.合并两个有序链表.py
|
||||
/src/leecode/java/22.括号生成.java
|
||||
/out/production/offer/leecode/javascript/24.两两交换链表中的节点.js
|
||||
/out/production/offer/leecode/javascript/27.移除元素.js
|
||||
/src/leecode/java/29.两数相除.java
|
||||
/out/production/offer/leecode/javascript/35.搜索插入位置.js
|
||||
/out/production/offer/leecode/javascript/44.通配符匹配.js
|
||||
/out/production/offer/leecode/javascript/51.n-皇后.js
|
||||
/src/leecode/javascript/51.n-皇后.js
|
||||
/out/production/offer/leecode/javascript/58.最后一个单词的长度.js
|
||||
/src/leecode/java/65.有效数字.java
|
||||
/out/production/offer/leecode/javascript/66.加一.js
|
||||
/out/production/offer/leecode/javascript/67.二进制求和.js
|
||||
/out/production/offer/leecode/javascript/69.x-的平方根.js
|
||||
/out/production/offer/leecode/javascript/70.爬楼梯.js
|
||||
/src/leecode/javascript/70.爬楼梯.js
|
||||
/out/production/offer/leecode/javascript/78.子集.js
|
||||
/out/production/offer/leecode/javascript/79.单词搜索.js
|
||||
/out/production/offer/leecode/javascript/80.删除有序数组中的重复项-ii.js
|
||||
/out/production/offer/leecode/javascript/86.分隔链表.js
|
||||
/out/production/offer/leecode/javascript/89.格雷编码.js
|
||||
/src/leecode/java/91.解码方法.java
|
||||
/src/leecode/java/122.买卖股票的最佳时机-ii.java
|
||||
/out/production/offer/leecode/javascript/139.单词拆分.js
|
||||
/out/production/offer/leecode/javascript/148.排序链表.js
|
||||
/out/production/offer/leecode/javascript/149.直线上最多的点数.js
|
||||
/out/production/offer/leecode/javascript/162.寻找峰值.js
|
||||
/out/production/offer/leecode/javascript/1094.拼车.js
|
||||
/out/production/offer/leecode/javascript/1172.餐盘栈.js
|
||||
/out/production/offer/ds/bit/Bit.class
|
||||
/out/production/offer/ds/diagram/CommonGraph$Edge.class
|
||||
/out/production/offer/ds/diagram/CommonGraph$Node.class
|
||||
/out/production/offer/ds/diagram/CommonGraph.class
|
||||
/out/production/offer/ds/diagram/Dijkstra.class
|
||||
/out/production/offer/ds/diagram/GraphIterator.class
|
||||
/out/production/offer/leecode/java/No6.class
|
||||
/out/production/offer/leecode/java/No7.class
|
||||
/out/production/offer/leecode/java/No8.class
|
||||
/src/leecode/java/No8.java
|
||||
/out/production/offer/leecode/java/No10.class
|
||||
/out/production/offer/leecode/java/No20.class
|
||||
/out/production/offer/leecode/java/No22.class
|
||||
/out/production/offer/leecode/java/No65.class
|
||||
/out/production/offer/leecode/java/No91.class
|
||||
/out/production/offer/leecode/java/No122.class
|
||||
/out/production/offer/ds/tree/PrefixTree$Node.class
|
||||
/out/production/offer/ds/tree/PrefixTree.class
|
||||
/out/production/offer/ds/dp/RobotMove.class
|
||||
/.vscode/settings.json
|
||||
/out/production/offer/leecode/java/Solution$ListNode.class
|
||||
/out/production/offer/leecode/java/Solution.class
|
||||
/out/production/offer/Solution.class
|
||||
/out/production/offer/juc/ThreadLocalDemo$1.class
|
||||
/out/production/offer/juc/ThreadLocalDemo$2.class
|
||||
/out/production/offer/juc/ThreadLocalDemo.class
|
||||
/out/production/offer/ds/tree/TreeTraverse.class
|
||||
/out/production/offer/ds/diagram/UnionFindSet$Node.class
|
||||
/out/production/offer/ds/diagram/UnionFindSet.class
|
||||
src/leecode/java/Solution.class
|
||||
.gitignore
|
||||
.idea/.gitignore
|
||||
|
|
|
|||
|
|
@ -6,3 +6,5 @@
|
|||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# GitHub Copilot persisted chat sessions
|
||||
/copilot/chatSessions
|
||||
|
|
|
|||
|
|
@ -1,48 +0,0 @@
|
|||
package leecode;
|
||||
|
||||
/*
|
||||
* @lc app=leetcode.cn id=10 lang=java
|
||||
*
|
||||
* [10] 正则表达式匹配
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class No10 {
|
||||
public boolean isMatch(String s, String p) {
|
||||
int m = s.length();
|
||||
int n = p.length();
|
||||
boolean[][] dp = new boolean[m + 1][n + 1];
|
||||
dp[0][0] = true;
|
||||
for (int i = 0; i <= m; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
if (p.charAt(j - 1) == '*') {
|
||||
dp[i][j] = dp[i][j - 2];
|
||||
if (match(s, p, i, j - 1)) {
|
||||
dp[i][j] = dp[i][j] || dp[i - 1][j];
|
||||
}
|
||||
} else {
|
||||
if (match(s, p, i, j)) {
|
||||
dp[i][j] = dp[i - 1][j - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[m][n];
|
||||
}
|
||||
|
||||
boolean match(String s, String p, int i, int j) {
|
||||
if (i == 0) return false;
|
||||
if (p.charAt(j - 1) == '.') {
|
||||
return true;
|
||||
}
|
||||
return s.charAt(i - 1) == p.charAt(j - 1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
No10 solution = new No10();
|
||||
solution.isMatch("aa", "a");
|
||||
}
|
||||
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
|
|
@ -1,103 +0,0 @@
|
|||
package leecode;/*
|
||||
* @lc app=leetcode.cn id=122 lang=java
|
||||
*
|
||||
* [122] 买卖股票的最佳时机 II
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class No122 {
|
||||
//贪心算法
|
||||
public int maxProfit(int[] prices) {
|
||||
int len = prices.length;
|
||||
if (len < 2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int res = 0;
|
||||
for (int i = 1; i < len; i++) {
|
||||
int diff = prices[i] - prices[i - 1];
|
||||
if (diff > 0) {
|
||||
res += diff;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
//动态规划
|
||||
public int maxProfit2(int[] prices) {
|
||||
int len = prices.length;
|
||||
if (len < 2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 0:没有股票时的现金
|
||||
// 1:持有股票时的现金
|
||||
// 状态转移:0 → 1 → 0 → 1 → 0 → 1 → 0
|
||||
int[][] dp = new int[len][2];
|
||||
|
||||
dp[0][0] = 0;
|
||||
dp[0][1] = -prices[0];
|
||||
|
||||
for (int i = 1; i < len; i++) {
|
||||
//今天未持有股票,昨天未持有或者昨天持有今天卖掉
|
||||
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
|
||||
//今天持有股票,持有的是昨天的股票或者今天买入的股票
|
||||
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
|
||||
}
|
||||
return dp[len - 1][0];
|
||||
// int[][] dp = new int[prices.length + 1][prices.length + 1];
|
||||
// for (int i = 1; i <= prices.length; i++) {
|
||||
// dp[prices.length][i] = prices[prices.length - 1];
|
||||
// }
|
||||
// for (int i = prices.length - 1; i >= 1; i--) {
|
||||
// for (int j = 0; j < prices.length; j++) {
|
||||
// int nextSellNoBuy = 0, nextSellBuy = 0, nextBuy = 0;
|
||||
// //不交易
|
||||
// int nextNoTrade = dp[i + 1][j];
|
||||
// if (j == 0) {
|
||||
// nextBuy = -prices[i] + dp[i + 1][i];
|
||||
// } else {
|
||||
// //卖了不买
|
||||
// nextSellNoBuy = prices[i] + dp[i + 1][0];
|
||||
// //卖了买
|
||||
// nextSellBuy = -prices[j] + dp[i + 1][i];
|
||||
// }
|
||||
// dp[i][j] = Math.max(Math.max(nextSellNoBuy, nextSellBuy), Math.max(nextBuy, nextNoTrade));
|
||||
// }
|
||||
// }
|
||||
// return dp[1][0];
|
||||
// return loop(prices, 0, -1);
|
||||
}
|
||||
|
||||
//暴力递归
|
||||
private int dfs(int[] prices, int index, int cur) {
|
||||
if (index == prices.length - 1) {
|
||||
if (cur != -1) {
|
||||
//最后一天还持股,直接卖
|
||||
return prices[index];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
int nextSellNoBuy = 0, nextSellBuy = 0, nextBuy = 0;
|
||||
//不交易
|
||||
int nextNoTrade = dfs(prices, index + 1, cur);
|
||||
if (cur == -1) {
|
||||
nextBuy = -prices[index] + dfs(prices, index + 1, index);
|
||||
} else {
|
||||
//卖了不买
|
||||
nextSellNoBuy = prices[index] + dfs(prices, index + 1, -1);
|
||||
//卖了买
|
||||
nextSellBuy = -prices[cur] + dfs(prices, index + 1, index);
|
||||
}
|
||||
return Math.max(Math.max(nextNoTrade, nextBuy), Math.max(nextSellNoBuy, nextSellBuy));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] price = new int[]{1, 2, 3};
|
||||
No122 solution = new No122();
|
||||
System.out.println(solution.maxProfit(price));
|
||||
}
|
||||
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
package leecode;/*
|
||||
* @lc app=leetcode.cn id=19 lang=java
|
||||
*
|
||||
* [19] 删除链表的倒数第 N 个结点
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
|
||||
// Definition for singly-linked list.
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Solution {
|
||||
public ListNode removeNthFromEnd(ListNode head, int n) {
|
||||
List<ListNode> cache =new ArrayList<>();
|
||||
ListNode ret = head;
|
||||
while (head != null) {
|
||||
cache.add(head);
|
||||
head = head.next;
|
||||
}
|
||||
if (n == cache.size()) {
|
||||
return ret.next;
|
||||
}
|
||||
cache.get(cache.size() - n - 1).next = n == 1 ? null : cache.get(cache.size() - n + 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
ListNode() {}
|
||||
ListNode(int val) { this.val = val; }
|
||||
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
package leecode;/*
|
||||
* @lc app=leetcode.cn id=20 lang=java
|
||||
*
|
||||
* [20] 有效的括号
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// @lc code=start
|
||||
class No20 {
|
||||
public boolean isValid(String s) {
|
||||
Map<Character, Character> cache = new HashMap<>();
|
||||
cache.put('(', ')');
|
||||
cache.put('{', '}');
|
||||
cache.put('[', ']');
|
||||
Set<Character> left = new HashSet<>();
|
||||
Set<Character> right = new HashSet<>();
|
||||
left.add('(');
|
||||
left.add('[');
|
||||
left.add('{');
|
||||
right.add(')');
|
||||
right.add(']');
|
||||
right.add('}');
|
||||
Stack<Character> stack = new Stack<>();
|
||||
int index = 0;
|
||||
while (index <= s.length() - 1) {
|
||||
if (left.contains(s.charAt(index))) {
|
||||
stack.push(s.charAt(index));
|
||||
}
|
||||
if (right.contains(s.charAt(index))) {
|
||||
if (stack.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
Character peek = stack.peek();
|
||||
if (s.charAt(index) != cache.get(peek)) {
|
||||
return false;
|
||||
}
|
||||
stack.pop();
|
||||
}
|
||||
index ++;
|
||||
}
|
||||
return stack.isEmpty();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
No20 solution = new No20();
|
||||
System.out.println(solution.isValid("{{}}()"));
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
package leecode;/*
|
||||
* @lc app=leetcode.cn id=22 lang=java
|
||||
*
|
||||
* [22] 括号生成
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
// @lc code=start
|
||||
class No22 {
|
||||
public List<String> generateParenthesis(int n) {
|
||||
Stack<String> stack = new Stack<>();
|
||||
List<String> result = new ArrayList<>();
|
||||
boolean selectLeft = true;
|
||||
StringBuilder create = new StringBuilder();
|
||||
select(stack, result, n-1, n, selectLeft, create.append("("));
|
||||
return result;
|
||||
}
|
||||
|
||||
private void select(Stack<String> stack, List<String> result, int left, int right, boolean selectLeft, StringBuilder create) {
|
||||
if (left == 0 && right == 0) {
|
||||
if (stack.isEmpty() || !stack.pop().equals("(")) {
|
||||
return;
|
||||
}
|
||||
result.add(create.toString());
|
||||
return;
|
||||
}
|
||||
if (left < 0 || right < 0) {
|
||||
return;
|
||||
}
|
||||
if (selectLeft) {
|
||||
stack.push("(");
|
||||
} else {
|
||||
if (stack.isEmpty() || !stack.pop().equals("(")) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
select((Stack<String>) stack.clone(), result, left - 1, right, true, create.append("("));
|
||||
create.delete(create.length() - 1, create.length());
|
||||
select((Stack<String>) stack.clone(), result, left, right-1, false, create.append(")"));
|
||||
create.delete(create.length() - 1, create.length());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
No22 solution = new No22();
|
||||
System.out.println(solution.generateParenthesis(2));
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
package leecode;/*
|
||||
* @lc app=leetcode.cn id=6 lang=java
|
||||
*
|
||||
* [6] Z 字形变换
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class No6 {
|
||||
public String convert(String s, int numRows) {
|
||||
if (numRows == 0 || numRows == 1) return s;
|
||||
int structNum = numRows + numRows - 2;
|
||||
int wholeNum = s.length() / structNum;
|
||||
int lastNum = s.length() % structNum;
|
||||
int lastCell = lastNum / numRows + (lastNum / numRows == 0 ? 1 : lastNum % numRows);
|
||||
int cellRight = wholeNum * (numRows - 1) + lastCell;
|
||||
char[][] resultStruct = new char[numRows][cellRight];
|
||||
int cursor = 1;
|
||||
int structCount = 0;
|
||||
int row = 0, cell = 0;
|
||||
boolean back = false;
|
||||
char[] chars = s.toCharArray();
|
||||
for (char aChar : chars) {
|
||||
if ((cursor - 1) / structNum > structCount) {
|
||||
row = 0;
|
||||
cell ++;
|
||||
structCount = (cursor - 1) / structNum;
|
||||
resultStruct[row][cell] = aChar;
|
||||
row ++;
|
||||
back = false;
|
||||
} else if (row < numRows && !back) {
|
||||
resultStruct[row][cell] = aChar;
|
||||
row ++;
|
||||
} else {
|
||||
if (row == numRows) row --;
|
||||
back = true;
|
||||
cell ++;
|
||||
row --;
|
||||
resultStruct[row][cell] = aChar;
|
||||
}
|
||||
cursor ++;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (char[] chars1 : resultStruct) {
|
||||
for (char c : chars1) {
|
||||
if (c == '\u0000') continue;
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
package leecode;/*
|
||||
* @lc app=leetcode.cn id=65 lang=java
|
||||
*
|
||||
* [65] 有效数字
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class No65 {
|
||||
|
||||
public boolean isNumber(String s) {
|
||||
char[] chars = s.toCharArray();
|
||||
int index = chars.length - 1;
|
||||
return match(chars, index);
|
||||
}
|
||||
|
||||
private boolean match(char[] chars, int index) {
|
||||
String flag = String.valueOf(chars[index]);
|
||||
String dMatch = "[0-9]*";
|
||||
String sMatch = "[eE+-.]*";
|
||||
if (index == 0) {
|
||||
return flag.matches("[+,-]") || flag.matches(dMatch);
|
||||
}
|
||||
if (!flag.matches(dMatch) && !flag.matches(sMatch)) {
|
||||
return false;
|
||||
}
|
||||
if (flag.matches(dMatch)) {
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
package leecode;
|
||||
|
||||
/*
|
||||
* @lc app=leetcode.cn id=7 lang=java
|
||||
*
|
||||
* [7] 整数反转
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class No7 {
|
||||
public int reverse(int x) {
|
||||
int res = 0, y = Math.abs(x);
|
||||
while (y != 0) {
|
||||
if (res < Integer.MIN_VALUE / 10 || res > Integer.MAX_VALUE / 10) {
|
||||
return 0;
|
||||
}
|
||||
res = res * 10 + y % 10;
|
||||
y /= 10;
|
||||
}
|
||||
return x >= 0 ? res : - res;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
package leecode;/*
|
||||
* @lc app=leetcode.cn id=91 lang=java
|
||||
*
|
||||
* [91] 解码方法
|
||||
*/
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
// @lc code=start test
|
||||
class No91 {
|
||||
public int numDecodings(String s) {
|
||||
int[] dp = new int[s.length() + 1];
|
||||
Arrays.fill(dp, -1);
|
||||
char[] chars = s.toCharArray();
|
||||
dp[s.length()] = 1;
|
||||
dp[s.length() - 1] = 1;
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
if (chars[i] == '0') {
|
||||
dp[i] = 0;
|
||||
}
|
||||
}
|
||||
for (int i = s.length() - 2; i >= 0; i--) {
|
||||
if ((Integer.parseInt(String.valueOf(s.charAt(i)) + Integer.parseInt(String.valueOf(s.charAt(i + 1)))) > 26)) {
|
||||
if (dp[i] == -1) {
|
||||
dp[i] = dp[i + 1];
|
||||
}
|
||||
} else {
|
||||
if (dp[i] == -1) {
|
||||
dp[i] = dp[i + 1] + dp[i + 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[0];
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
No91 solution = new No91();
|
||||
System.out.println(solution.numDecodings("0"));
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
package leecode;
|
||||
|
||||
/**
|
||||
* 字符串转整数
|
||||
*/
|
||||
public class No8 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(myAtoi("words and 987"));
|
||||
}
|
||||
|
||||
//有限状态机DFA
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//这个一般解法过不了leecode
|
||||
public static int myAtoi(String str) {
|
||||
if (str.length() == 0){
|
||||
return 0;
|
||||
}
|
||||
int result = 0;
|
||||
boolean isValid = false;
|
||||
Long l = 0L;
|
||||
char symbol = ' ';
|
||||
char[] chars = str.toCharArray();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
if (!isValid){
|
||||
if (chars[i] == '+' || chars[i] == '-') {
|
||||
if ('0' <= chars[i + 1] && chars[i + 1] <= '9'){
|
||||
isValid = true;
|
||||
symbol = chars[i];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ('0' <= chars[i] && chars[i] <= '9'){
|
||||
isValid = true;
|
||||
int value = chars[i] - 48;
|
||||
l = l * 10 + value;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ('0' <= chars[i] && chars[i] <= '9') {
|
||||
int value = chars[i] - 48;
|
||||
if (l * 10 + value >= Integer.MAX_VALUE){
|
||||
l = new Long(Integer.MAX_VALUE);
|
||||
break;
|
||||
}
|
||||
l = l * 10 + value;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (symbol == '-'){
|
||||
l = l - 2 * l;
|
||||
}
|
||||
result = l.intValue();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue