Conflicts:
	LeetCode/src/main/java/com/code/leet/entiy/Node.java
This commit is contained in:
huangge1199@hotmail.com 2021-04-05 21:17:21 +08:00
commit b928c97241
50 changed files with 3204 additions and 3 deletions

View File

@ -7,6 +7,8 @@ public class Node {
public int val; public int val;
public Node next; public Node next;
public Node random; public Node random;
public Node prev;
public Node child;
public Node(int val) { public Node(int val) {
this.val = val; this.val = val;

View File

@ -1,6 +1,9 @@
package com.code.leet.entiy; package com.code.leet.entiy;
import java.util.ArrayList;
import java.util.List;
/** /**
* @Author: hyy * @Author: hyy
* @Date: 2020-02-13 18:25 * @Date: 2020-02-13 18:25
@ -22,4 +25,46 @@ public class TreeNode {
this.left = left; this.left = left;
this.right = right; this.right = right;
} }
public TreeNode(List<Integer> list) {
if (list.size() == 0) {
return;
}
List<TreeNode> treeNodeList = new ArrayList<>();
int index = 0;
for (int i = 0; i < list.size(); i++) {
if (treeNodeList.size() == 0) {
this.val = list.get(i);
treeNodeList.add(this);
index = 0;
} else {
TreeNode root = treeNodeList.get(index);
treeNodeList.remove(index);
TreeNode left;
if (list.get(i) != null && root != null) {
left = new TreeNode(list.get(i));
root.left = left;
treeNodeList.add(index, left);
} else {
treeNodeList.add(index, null);
}
i++;
index++;
TreeNode right;
if (i < list.size()) {
if (list.get(i) != null && root != null) {
right = new TreeNode(list.get(i));
root.right = right;
treeNodeList.add(index, right);
}else{
treeNodeList.add(index, null);
}
index++;
}
if (index == treeNodeList.size()*2) {
index = 0;
}
}
}
}
} }

View File

@ -0,0 +1,68 @@
package com.code.leet.study.t2021.t20210305;
import com.code.leet.entiy.ListNode;
import com.code.leet.entiy.TreeNode;
/**
* 给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表
* <p>
* 如果在二叉树中存在一条一直向下的路径且每个点的数值恰好一一对应以 head 为首的链表中每个节点的值那么请你返回 True 否则返回 False
* <p>
* 一直向下的路径的意思是从树中某个节点开始一直连续向下的路径
* <p>
*  
* <p>
* 示例 1
* <p>
* <p>
* <p>
* 输入head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
* 输出true
* 解释树中蓝色的节点构成了与链表对应的子路径
* 示例 2
* <p>
* <p>
* <p>
* 输入head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
* 输出true
* 示例 3
* <p>
* 输入head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
* 输出false
* 解释二叉树中不存在一一对应链表的路径
*  
* <p>
* 提示
* <p>
* 二叉树和链表中的每个节点的值都满足 1 <= node.val <= 100 
* 链表包含的节点数目在 1  100 之间
* 二叉树包含的节点数目在 1  2500 之间
* <p>
* 来源力扣LeetCode
* 链接https://leetcode-cn.com/problems/linked-list-in-binary-tree
* 著作权归领扣网络所有商业转载请联系官方授权非商业转载请注明出处
*/
public class IsSubPath {
/**
* 1367. 二叉树中的列表
*/
public boolean isSubPath(ListNode head, TreeNode root) {
if (root == null) {
return false;
}
return isSubPath(head, root.left) || isSubPath(head, root.right) || dfs(head, root);
}
private boolean dfs(ListNode head, TreeNode root) {
if (head == null) {
return true;
}
if (root == null) {
return false;
}
if (root.val != head.val) {
return false;
}
return dfs(head.next, root.left) || dfs(head.next, root.right);
}
}

View File

@ -0,0 +1,70 @@
package com.code.leet.study.t2021.t20210305;
import com.code.leet.entiy.ListNode;
/**
* 给你两个链表 list1  list2 它们包含的元素分别为 n 个和 m
* <p>
* 请你将 list1 中第 a 个节点到第 b 个节点删除并将list2 接在被删除节点的位置
* <p>
* 下图中蓝色边和节点展示了操作后的结果
* <p>
* <p>
* 请你返回结果链表的头指针
* <p>
*  
* <p>
* 示例 1
* <p>
* <p>
* <p>
* 输入list1 = [0,1,2,3,4,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
* 输出[0,1,2,1000000,1000001,1000002,5]
* 解释我们删除 list1 中第三和第四个节点并将 list2 接在该位置上图中蓝色的边和节点为答案链表
* 示例 2
* <p>
* <p>
* 输入list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
* 输出[0,1,1000000,1000001,1000002,1000003,1000004,6]
* 解释上图中蓝色的边和节点为答案链表
*  
* <p>
* 提示
* <p>
* 3 <= list1.length <= 104
* 1 <= a <= b < list1.length - 1
* 1 <= list2.length <= 104
* <p>
* 来源力扣LeetCode
* 链接https://leetcode-cn.com/problems/merge-in-between-linked-lists
* 著作权归领扣网络所有商业转载请联系官方授权非商业转载请注明出处
*/
public class MergeInBetween {
/**
* 1669. 合并两个链表
*/
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
ListNode temp = list1;
int num = 0;
while (temp != null) {
ListNode trans = new ListNode(0);
if (num == a - 1) {
trans.next = temp.next;
temp.next = list2;
temp = trans;
}
if (num == b + 1) {
ListNode temp2 = list2;
while (temp2!=null&&temp2.next!=null){
temp2 = temp2.next;
}
assert temp2 != null;
temp2.next = temp;
break;
}
num++;
temp = temp.next;
}
return list1;
}
}

View File

@ -0,0 +1,64 @@
package com.code.leet.study.t2021.t20210305;
import com.code.leet.entiy.ListNode;
import java.util.ArrayList;
import java.util.List;
/**
* 给出一个以头节点 head 作为第一个节点的链表链表中的节点分别编号为node_1, node_2, node_3, ...
* <p>
* 每个节点都可能有下一个更大值next larger value对于 node_i如果其 next_larger(node_i)  node_j.val那么就有 j > i   node_j.val > node_i.val j 是可能的选项中最小的那个如果不存在这样的 j那么下一个更大值为 0 
* <p>
* 返回整数答案数组 answer其中 answer[i] = next_larger(node_{i+1}) 
* <p>
* 注意在下面的示例中诸如 [2,1,5] 这样的输入不是输出是链表的序列化表示其头节点的值为 2第二个节点值为 1第三个节点值为 5
* <p>
*  
* <p>
* 示例 1
* <p>
* 输入[2,1,5]
* 输出[5,5,0]
* 示例 2
* <p>
* 输入[2,7,4,3,5]
* 输出[7,0,5,5,0]
* 示例 3
* <p>
* 输入[1,7,5,1,9,2,5,1]
* 输出[7,9,9,9,0,5,0,0]
*  
* <p>
* 提示
* <p>
* 对于链表中的每个节点1 <= node.val <= 10^9
* 给定列表的长度在 [0, 10000] 范围内
* <p>
* 来源力扣LeetCode
* 链接https://leetcode-cn.com/problems/next-greater-node-in-linked-list
* 著作权归领扣网络所有商业转载请联系官方授权非商业转载请注明出处
*/
public class NextLargerNodes {
/**
* 1019. 链表中的下一个更大节点
*/
public int[] nextLargerNodes(ListNode head) {
List<Integer> list = new ArrayList<>();
while (head != null) {
list.add(head.val);
head = head.next;
}
int size = list.size();
int[] result = new int[size];
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (list.get(j) > list.get(i)) {
result[i] = list.get(j);
break;
}
}
}
return result;
}
}

View File

@ -0,0 +1,70 @@
package com.code.leet.study.t2021.t20210305;
import com.code.leet.entiy.ListNode;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* 给定链表头结点 head该链表上的每个结点都有一个 唯一的整型值
* <p>
* 同时给定列表 G该列表是上述链表中整型值的一个子集
* <p>
* 返回列表 G 中组件的个数这里对组件的定义为链表中一段最长连续结点的值该值必须在列表 G 构成的集合
* <p>
*  
* <p>
* 示例 1
* <p>
* 输入:
* head: 0->1->2->3
* G = [0, 1, 3]
* 输出: 2
* 解释:
* 链表中,0 1 是相连接的 G 中不包含 2所以 [0, 1] G 的一个组件同理 [3] 也是一个组件故返回 2
* 示例 2
* <p>
* 输入:
* head: 0->1->2->3->4
* G = [0, 3, 1, 4]
* 输出: 2
* 解释:
* 链表中0 1 是相连接的3 4 是相连接的所以 [0, 1] [3, 4] 是两个组件故返回 2
*  
* <p>
* 提示
* <p>
* 如果 N 是给定链表 head 的长度1 <= N <= 10000
* 链表中每个结点的值所在范围为 [0, N - 1]
* 1 <= G.length <= 10000
* G 是链表中所有结点的值的一个子集.
* <p>
* 来源力扣LeetCode
* 链接https://leetcode-cn.com/problems/linked-list-components
* 著作权归领扣网络所有商业转载请联系官方授权非商业转载请注明出处
*/
public class NumComponents {
/**
* 817. 链表组件
*/
public int numComponents(ListNode head, int[] G) {
int num = 0;
boolean bl = false;
List<Integer> list = Arrays.stream(G).boxed().collect(Collectors.toList());
while (head != null) {
if (list.contains(head.val)) {
bl = true;
} else {
if (bl) {
num++;
}
bl = false;
}
head = head.next;
}
num = bl ? num + 1 : num;
return num;
}
}

View File

@ -0,0 +1,71 @@
package com.code.leet.study.t2021.t20210305;
import com.code.leet.entiy.ListNode;
import java.util.HashMap;
import java.util.Map;
/**
* 给你一个链表的头节点 head请你编写代码反复删去链表中由 总和 值为 0 的连续节点组成的序列直到不存在这样的序列为止
* <p>
* 删除完毕后请你返回最终结果链表的头节点
* <p>
* <p>
* <p>
* 你可以返回任何满足题目要求的答案
* <p>
* 注意下面示例中的所有序列都是对 ListNode 对象序列化的表示
* <p>
* 示例 1
* <p>
* 输入head = [1,2,-3,3,1]
* 输出[3,1]
* 提示答案 [1,2,1] 也是正确的
* 示例 2
* <p>
* 输入head = [1,2,3,-3,4]
* 输出[1,2,4]
* 示例 3
* <p>
* 输入head = [1,2,3,-3,-2]
* 输出[1]
* <p>
* <p>
* 提示
* <p>
* 给你的链表中可能有 1 1000 个节点
* 对于链表中的每个节点节点的值-1000 <= node.val <= 1000.
*/
public class RemoveZeroSumSublists {
/**
* 1171. 从链表中删去总和值为零的连续节点
*/
public ListNode removeZeroSumSublists(ListNode head) {
Map<Integer, ListNode> map = new HashMap<>();
int sum = 0;
ListNode temp = head;
while (temp != null) {
sum += temp.val;
if (sum == 0 || map.containsKey(sum)) {
if (sum == 0) {
head = temp.next;
} else {
map.get(sum).next = temp.next;
}
if (head != null) {
map = new HashMap<>();
temp = head;
sum = head.val;
} else {
return head;
}
}
while (temp.next != null && temp.next.val == 0) {
temp.next = temp.next.next;
}
map.put(sum, temp);
temp = temp.next;
}
return head;
}
}

View File

@ -0,0 +1,90 @@
package leetcode.editor.cn;
//给定一个整数数组 asteroids表示在同一行的行星
//
// 对于数组中的每一个元素其绝对值表示行星的大小正负表示行星的移动方向正表示向右移动负表示向左移动每一颗行星以相同的速度移动
//
// 找出碰撞后剩下的所有行星碰撞规则两个行星相互碰撞较小的行星会爆炸如果两颗行星大小相同则两颗行星都会爆炸两颗移动方向相同的行星永远不会发生碰撞
//
//
//
//
// 示例 1
//
//
//输入asteroids = [5,10,-5]
//输出[5,10]
//解释10 -5 碰撞后只剩下 10 5 10 永远不会发生碰撞
//
// 示例 2
//
//
//输入asteroids = [8,-8]
//输出[]
//解释8 -8 碰撞后两者都发生爆炸
//
// 示例 3
//
//
//输入asteroids = [10,2,-5]
//输出[10]
//解释2 -5 发生碰撞后剩下 -5 10 -5 发生碰撞后剩下 10
//
// 示例 4
//
//
//输入asteroids = [-2,-1,1,2]
//输出[-2,-1,1,2]
//解释-2 -1 向左移动 1 2 向右移动 由于移动方向相同的行星不会发生碰撞所以最终没有行星发生碰撞
//
//
//
// 提示
//
//
// 2 <= asteroids.length <= 104
// -1000 <= asteroids[i] <= 1000
// asteroids[i] != 0
//
// Related Topics
// 👍 135 👎 0
import java.util.Stack;
public class AsteroidCollision{
public static void main(String[] args) {
Solution solution = new AsteroidCollision().new Solution();
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();
for (int num : asteroids) {
boolean isIn = true;
while (!stack.isEmpty() && num < 0 && stack.peek() > 0) {
if (num + stack.peek() < 0) {
stack.pop();
} else if (num + stack.peek() > 0) {
isIn = false;
break;
} else {
isIn = false;
stack.pop();
break;
}
}
if (isIn) {
stack.push(num);
}
}
int size = stack.size();
asteroids = new int[size];
for (int i = size - 1; i >= 0; i--) {
asteroids[i] = stack.pop();
}
return asteroids;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,102 @@
//给定 S T 两个字符串当它们分别被输入到空白的文本编辑器后判断二者是否相等并返回结果 # 代表退格字符
//
// 注意如果对空文本输入退格字符文本继续为空
//
//
//
// 示例 1
//
//
//输入S = "ab#c", T = "ad#c"
//输出true
//解释S T 都会变成 ac
//
//
// 示例 2
//
//
//输入S = "ab##", T = "c#d#"
//输出true
//解释S T 都会变成
//
//
// 示例 3
//
//
//输入S = "a##c", T = "#a#c"
//输出true
//解释S T 都会变成 c
//
//
// 示例 4
//
//
//输入S = "a#c", T = "b"
//输出false
//解释S 会变成 c T 仍然是 b
//
//
//
// 提示
//
//
// 1 <= S.length <= 200
// 1 <= T.length <= 200
// S T 只含有小写字母以及字符 '#'
//
//
//
//
// 进阶
//
//
// 你可以用 O(N) 的时间复杂度和 O(1) 的空间复杂度解决该问题吗
//
//
//
// Related Topics 双指针
// 👍 276 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//844:比较含退格的字符串
public class BackspaceStringCompare {
public static void main(String[] args) {
//测试代码
Solution solution = new BackspaceStringCompare().new Solution();
solution.backspaceCompare("a##c", "#a#c");
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean backspaceCompare(String S, String T) {
S = getResult(S);
T = getResult(T);
return S.equals(T);
}
private String getResult(String str) {
Stack<Character> stack = new Stack<>();
int length = str.length();
for (char ch : str.toCharArray()) {
if (ch == '#') {
if(!stack.isEmpty()) {
stack.pop();
}
} else {
stack.push(ch);
}
}
str = "";
while (!stack.isEmpty()) {
str += stack.pop().toString();
}
return str;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,57 @@
<p>给定 <code>S</code><code>T</code> 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果。 <code>#</code> 代表退格字符。</p>
<p><strong>注意:</strong>如果对空文本输入退格字符,文本继续为空。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>S = "ab#c", T = "ad#c"
<strong>输出:</strong>true
<strong>解释:</strong>S 和 T 都会变成 “ac”。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>S = "ab##", T = "c#d#"
<strong>输出:</strong>true
<strong>解释:</strong>S 和 T 都会变成 “”。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>S = "a##c", T = "#a#c"
<strong>输出:</strong>true
<strong>解释:</strong>S 和 T 都会变成 “c”。
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>S = "a#c", T = "b"
<strong>输出:</strong>false
<strong>解释:</strong>S 会变成 “c”但 T 仍然是 “b”。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= S.length <= 200</code></li>
<li><code>1 <= T.length <= 200</code></li>
<li><code>S</code><code>T</code> 只含有小写字母以及字符 <code>'#'</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>你可以用 <code>O(N)</code> 的时间复杂度和 <code>O(1)</code> 的空间复杂度解决该问题吗?</li>
</ul>
<p> </p>
<div><div>Related Topics</div><div><li></li><li>双指针</li></div></div>\n<div><li>👍 276</li><li>👎 0</li></div>

View File

@ -0,0 +1,85 @@
//定义栈的数据结构请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中调用 minpush pop 的时间复杂度都是 O(1)
//
//
//
// 示例:
//
// MinStack minStack = new MinStack();
//minStack.push(-2);
//minStack.push(0);
//minStack.push(-3);
//minStack.min(); --> 返回 -3.
//minStack.pop();
//minStack.top(); --> 返回 0.
//minStack.min(); --> 返回 -2.
//
//
//
//
// 提示
//
//
// 各函数的调用总次数不超过 20000
//
//
//
//
// 注意本题与主站 155 题相同https://leetcode-cn.com/problems/min-stack/
// Related Topics 设计
// 👍 114 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//面试题30:包含min函数的栈
public class BaoHanMinhanShuDeZhanLcof{
public static void main(String[] args) {
//测试代码
// Solution solution = new BaoHanMinhanShuDeZhanLcof().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MinStack {
Stack<Integer> stack;
Stack<Integer> min;
public MinStack() {
stack = new Stack<>();
min = new Stack<>();
}
public void push(int val) {
if (stack.isEmpty()) {
min.push(val);
} else {
min.push(Math.min(min.peek(), val));
}
stack.push(val);
}
public void pop() {
stack.pop();
min.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return min.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.min();
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,28 @@
<p>定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。</p>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre>MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); --&gt; 返回 -3.
minStack.pop();
minStack.top(); --&gt; 返回 0.
minStack.min(); --&gt; 返回 -2.
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li>各函数的调用总次数不超过 20000 次</li>
</ol>
<p>&nbsp;</p>
<p>注意:本题与主站 155 题相同:<a href="https://leetcode-cn.com/problems/min-stack/">https://leetcode-cn.com/problems/min-stack/</a></p>
<div><div>Related Topics</div><div><li></li><li>设计</li></div></div>\n<div><li>👍 114</li><li>👎 0</li></div>

View File

@ -0,0 +1,111 @@
//你现在是一场采用特殊赛制棒球比赛的记录员这场比赛由若干回合组成过去几回合的得分可能会影响以后几回合的得分
//
// 比赛开始时记录是空白的你会得到一个记录操作的字符串列表 ops其中 ops[i] 是你需要记录的第 i 项操作ops 遵循下述规则
//
//
// 整数 x - 表示本回合新获得分数 x
// "+" - 表示本回合新获得的得分是前两次得分的总和题目数据保证记录此操作时前面总是存在两个有效的分数
// "D" - 表示本回合新获得的得分是前一次得分的两倍题目数据保证记录此操作时前面总是存在一个有效的分数
// "C" - 表示前一次得分无效将其从记录中移除题目数据保证记录此操作时前面总是存在一个有效的分数
//
//
// 请你返回记录中所有得分的总和
//
//
//
// 示例 1
//
//
//输入ops = ["5","2","C","D","+"]
//输出30
//解释
//"5" - 记录加 5 记录现在是 [5]
//"2" - 记录加 2 记录现在是 [5, 2]
//"C" - 使前一次得分的记录无效并将其移除记录现在是 [5].
//"D" - 记录加 2 * 5 = 10 记录现在是 [5, 10].
//"+" - 记录加 5 + 10 = 15 记录现在是 [5, 10, 15].
//所有得分的总和 5 + 10 + 15 = 30
//
//
// 示例 2
//
//
//输入ops = ["5","-2","4","C","D","9","+","+"]
//输出27
//解释
//"5" - 记录加 5 记录现在是 [5]
//"-2" - 记录加 -2 记录现在是 [5, -2]
//"4" - 记录加 4 记录现在是 [5, -2, 4]
//"C" - 使前一次得分的记录无效并将其移除记录现在是 [5, -2]
//"D" - 记录加 2 * -2 = -4 记录现在是 [5, -2, -4]
//"9" - 记录加 9 记录现在是 [5, -2, -4, 9]
//"+" - 记录加 -4 + 9 = 5 记录现在是 [5, -2, -4, 9, 5]
//"+" - 记录加 9 + 5 = 14 记录现在是 [5, -2, -4, 9, 5, 14]
//所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27
//
//
// 示例 3
//
//
//输入ops = ["1"]
//输出1
//
//
//
//
// 提示
//
//
// 1 <= ops.length <= 1000
// ops[i] "C""D""+"或者一个表示整数的字符串整数范围是 [-3 * 104, 3 * 104]
// 对于 "+" 操作题目数据保证记录此操作时前面总是存在两个有效的分数
// 对于 "C" "D" 操作题目数据保证记录此操作时前面总是存在一个有效的分数
//
// Related Topics
// 👍 169 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//682:棒球比赛
public class BaseballGame {
public static void main(String[] args) {
//测试代码
Solution solution = new BaseballGame().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int calPoints(String[] ops) {
int sum = 0;
Stack<Integer> stack = new Stack<>();
for (String str : ops) {
switch (str) {
case "C":
stack.pop();
break;
case "D":
stack.push(stack.peek() * 2);
break;
case "+":
int temp = stack.pop();
int current = temp + stack.peek();
stack.push(temp);
stack.push(current);
break;
default:
stack.push(Integer.valueOf(str));
break;
}
}
while (!stack.isEmpty()) {
sum += stack.pop();
}
return sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,64 @@
<p>你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。</p>
<p>比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 <code>ops</code>,其中 <code>ops[i]</code> 是你需要记录的第 <code>i</code> 项操作,<code>ops</code> 遵循下述规则:</p>
<ol>
<li>整数 <code>x</code> - 表示本回合新获得分数 <code>x</code></li>
<li><code>"+"</code> - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。</li>
<li><code>"D"</code> - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。</li>
<li><code>"C"</code> - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。</li>
</ol>
<p>请你返回记录中所有得分的总和。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>ops = ["5","2","C","D","+"]
<strong>输出:</strong>30
<strong>解释:</strong>
"5" - 记录加 5 ,记录现在是 [5]
"2" - 记录加 2 ,记录现在是 [5, 2]
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5].
"D" - 记录加 2 * 5 = 10 ,记录现在是 [5, 10].
"+" - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15].
所有得分的总和 5 + 10 + 15 = 30
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>ops = ["5","-2","4","C","D","9","+","+"]
<strong>输出:</strong>27
<strong>解释:</strong>
"5" - 记录加 5 ,记录现在是 [5]
"-2" - 记录加 -2 ,记录现在是 [5, -2]
"4" - 记录加 4 ,记录现在是 [5, -2, 4]
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2]
"D" - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4]
"9" - 记录加 9 ,记录现在是 [5, -2, -4, 9]
"+" - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5]
"+" - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14]
所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>ops = ["1"]
<strong>输出:</strong>1
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= ops.length <= 1000</code></li>
<li><code>ops[i]</code><code>"C"</code><code>"D"</code><code>"+"</code>,或者一个表示整数的字符串。整数范围是 <code>[-3 * 10<sup>4</sup>, 3 * 10<sup>4</sup>]</code></li>
<li>对于 <code>"+"</code> 操作,题目数据保证记录此操作时前面总是存在两个有效的分数</li>
<li>对于 <code>"C"</code><code>"D"</code> 操作,题目数据保证记录此操作时前面总是存在一个有效的分数</li>
</ul>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 169</li><li>👎 0</li></div>

View File

@ -0,0 +1,105 @@
//给定一个二叉树的根节点 root 返回它的 中序 遍历
//
//
//
// 示例 1
//
//
//输入root = [1,null,2,3]
//输出[1,3,2]
//
//
// 示例 2
//
//
//输入root = []
//输出[]
//
//
// 示例 3
//
//
//输入root = [1]
//输出[1]
//
//
// 示例 4
//
//
//输入root = [1,2]
//输出[2,1]
//
//
// 示例 5
//
//
//输入root = [1,null,2]
//输出[1,2]
//
//
//
//
// 提示
//
//
// 树中节点数目在范围 [0, 100]
// -100 <= Node.val <= 100
//
//
//
//
// 进阶: 递归算法很简单你可以通过迭代算法完成吗
// Related Topics 哈希表
// 👍 902 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TreeNode;
import java.util.ArrayList;
import java.util.List;
//94:二叉树的中序遍历
public class BinaryTreeInorderTraversal {
public static void main(String[] args) {
//测试代码
Solution solution = new BinaryTreeInorderTraversal().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
inorder(root, list);
return list;
}
private void inorder(TreeNode root, List<Integer> list) {
if (root == null) {
return;
}
inorder(root.left, list);
list.add(root.val);
inorder(root.right, list);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,52 @@
<p>给定一个二叉树的根节点 <code>root</code> ,返回它的 <strong>中序</strong> 遍历。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg" style="width: 202px; height: 324px;" />
<pre>
<strong>输入:</strong>root = [1,null,2,3]
<strong>输出:</strong>[1,3,2]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>root = []
<strong>输出:</strong>[]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>root = [1]
<strong>输出:</strong>[1]
</pre>
<p><strong>示例 4</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg" style="width: 202px; height: 202px;" />
<pre>
<strong>输入:</strong>root = [1,2]
<strong>输出:</strong>[2,1]
</pre>
<p><strong>示例 5</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg" style="width: 202px; height: 202px;" />
<pre>
<strong>输入:</strong>root = [1,null,2]
<strong>输出:</strong>[1,2]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点数目在范围 <code>[0, 100]</code></li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong> 递归算法很简单,你可以通过迭代算法完成吗?</p>
<div><div>Related Topics</div><div><li></li><li></li><li>哈希表</li></div></div>\n<div><li>👍 902</li><li>👎 0</li></div>

View File

@ -0,0 +1,97 @@
//给定一个二叉树返回其节点值的锯齿形层序遍历即先从左往右再从右往左进行下一层遍历以此类推层与层之间交替进行
//
// 例如
//给定二叉树 [3,9,20,null,null,15,7],
//
//
// 3
// / \
// 9 20
// / \
// 15 7
//
//
// 返回锯齿形层序遍历如下
//
//
//[
// [3],
// [20,9],
// [15,7]
//]
//
// Related Topics 广度优先搜索
// 👍 421 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TreeNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
//103:二叉树的锯齿形层序遍历
public class BinaryTreeZigzagLevelOrderTraversal {
public static void main(String[] args) {
//测试代码
Solution solution = new BinaryTreeZigzagLevelOrderTraversal().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
stack1.push(root);
TreeNode temp;
List<Integer> list;
while (!stack1.isEmpty() || !stack2.isEmpty()) {
list = new ArrayList<>();
while (!stack1.isEmpty()) {
temp = stack1.pop();
list.add(temp.val);
if (temp.left != null) {
stack2.push(temp.left);
}
if (temp.right != null) {
stack2.push(temp.right);
}
result.add(list);
}
list = new ArrayList<>();
while (!stack2.isEmpty()) {
temp = stack2.pop();
list.add(temp.val);
if (temp.right != null) {
stack1.push(temp.right);
}
if (temp.left != null) {
stack1.push(temp.left);
}
result.add(list);
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,23 @@
<p>给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。</p>
<p>例如:<br />
给定二叉树 <code>[3,9,20,null,null,15,7]</code>,</p>
<pre>
3
/ \
9 20
/ \
15 7
</pre>
<p>返回锯齿形层序遍历如下:</p>
<pre>
[
[3],
[20,9],
[15,7]
]
</pre>
<div><div>Related Topics</div><div><li></li><li></li><li>广度优先搜索</li></div></div>\n<div><li>👍 421</li><li>👎 0</li></div>

View File

@ -0,0 +1,105 @@
//给你一个目标数组 target 和一个整数 n每次迭代需要从 list = {1,2,3..., n} 中依序读取一个数字
//
// 请使用下述操作来构建目标数组 target
//
//
// Push list 中读取一个新元素 并将其推入数组中
// Pop删除数组中的最后一个元素
// 如果目标数组构建完成就停止读取更多元素
//
//
// 题目数据保证目标数组严格递增并且只包含 1 n 之间的数字
//
// 请返回构建目标数组所用的操作序列
//
// 题目数据保证答案是唯一的
//
//
//
// 示例 1
//
//
//输入target = [1,3], n = 3
//输出["Push","Push","Pop","Push"]
//解释
//读取 1 并自动推入数组 -> [1]
//读取 2 并自动推入数组然后删除它 -> [1]
//读取 3 并自动推入数组 -> [1,3]
//
//
// 示例 2
//
//
//输入target = [1,2,3], n = 3
//输出["Push","Push","Push"]
//
//
// 示例 3
//
//
//输入target = [1,2], n = 4
//输出["Push","Push"]
//解释只需要读取前 2 个数字就可以停止
//
//
// 示例 4
//
//
//输入target = [2,3,4], n = 4
//输出["Push","Pop","Push","Push","Push"]
//
//
//
//
// 提示
//
//
// 1 <= target.length <= 100
// 1 <= target[i] <= 100
// 1 <= n <= 100
// target 是严格递增的
//
// Related Topics
// 👍 18 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
//1441:用栈操作构建数组
public class BuildAnArrayWithStackOperations {
public static void main(String[] args) {
//测试代码
Solution solution = new BuildAnArrayWithStackOperations().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<String> buildArray(int[] target, int n) {
List<String> list = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
int size = target.length;
for (int i = size - 1; i >= 0; i--) {
stack.push(target[i]);
}
for (int i = 1; i <= n; i++) {
if (!stack.isEmpty() && stack.peek() == i) {
list.add("Push");
stack.pop();
if(stack.isEmpty()){
break;
}
} else {
list.add("Push");
list.add("Pop");
}
}
return list;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,62 @@
<p>给你一个目标数组 <code>target</code> 和一个整数 <code>n</code>。每次迭代,需要从  <code>list = {1,2,3..., n}</code> 中依序读取一个数字。</p>
<p>请使用下述操作来构建目标数组 <code>target</code> </p>
<ul>
<li><strong>Push</strong>:从 <code>list</code> 中读取一个新元素, 并将其推入数组中。</li>
<li><strong>Pop</strong>:删除数组中的最后一个元素。</li>
<li>如果目标数组构建完成,就停止读取更多元素。</li>
</ul>
<p>题目数据保证目标数组严格递增,并且只包含 <code>1</code><code>n</code> 之间的数字。</p>
<p>请返回构建目标数组所用的操作序列。</p>
<p>题目数据保证答案是唯一的。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>target = [1,3], n = 3
<strong>输出:</strong>["Push","Push","Pop","Push"]
<strong>解释:
</strong>读取 1 并自动推入数组 -> [1]
读取 2 并自动推入数组,然后删除它 -> [1]
读取 3 并自动推入数组 -> [1,3]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>target = [1,2,3], n = 3
<strong>输出:</strong>["Push","Push","Push"]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>target = [1,2], n = 4
<strong>输出:</strong>["Push","Push"]
<strong>解释:</strong>只需要读取前 2 个数字就可以停止。
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>target = [2,3,4], n = 4
<strong>输出:</strong>["Push","Pop","Push","Push","Push"]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= target.length <= 100</code></li>
<li><code>1 <= target[i] <= 100</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>target</code> 是严格递增的</li>
</ul>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 18</li><li>👎 0</li></div>

View File

@ -0,0 +1,73 @@
//通常正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积例如factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 *
// 3 * 2 * 1
//
// 相反我们设计了一个笨阶乘 clumsy在整数的递减序列中我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符乘法(*)除法(/)加法(+)
//和减法(-)
//
// 例如clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1然而这些运算仍然使用通常的算术运算顺序
//们在任何加减步骤之前执行所有的乘法和除法步骤并且按从左到右处理乘法和除法步骤
//
// 另外我们使用的除法是地板除法floor division所以 10 * 9 / 8 等于 11这保证结果是一个整数
//
// 实现上面定义的笨函数给定一个整数 N它返回 N 的笨阶乘
//
//
//
// 示例 1
//
// 输入4
//输出7
//解释7 = 4 * 3 / 2 + 1
//
//
// 示例 2
//
// 输入10
//输出12
//解释12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
//
//
//
//
// 提示
//
//
// 1 <= N <= 10000
// -2^31 <= answer <= 2^31 - 1 答案保证符合 32 位整数
//
// Related Topics 数学
// 👍 79 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//1006:笨阶乘
public class ClumsyFactorial {
public static void main(String[] args) {
//测试代码
Solution solution = new ClumsyFactorial().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int clumsy(int N) {
Stack<Integer> stack = new Stack<>();
for (int i = 1; i <= N; i++) {
stack.push(i);
}
int sum = 0;
while (!stack.isEmpty()) {
int temp = stack.pop();
temp = stack.isEmpty() ? temp : temp * stack.pop();
temp = stack.isEmpty() ? temp : temp / stack.pop();
sum = sum == 0 ? temp : sum - temp;
sum = stack.isEmpty() ? sum : sum + stack.pop();
}
return sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,35 @@
<p>通常,正整数 <code>n</code> 的阶乘是所有小于或等于 <code>n</code> 的正整数的乘积。例如,<code>factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1</code></p>
<p>相反,我们设计了一个笨阶乘 <code>clumsy</code>:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)。</p>
<p>例如,<code>clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1</code>。然而,这些运算仍然使用通常的算术运算顺序:我们在任何加、减步骤之前执行所有的乘法和除法步骤,并且按从左到右处理乘法和除法步骤。</p>
<p>另外,我们使用的除法是地板除法(<em>floor division</em>),所以&nbsp;<code>10 * 9 / 8</code>&nbsp;等于&nbsp;<code>11</code>。这保证结果是一个整数。</p>
<p>实现上面定义的笨函数:给定一个整数 <code>N</code>,它返回 <code>N</code> 的笨阶乘。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>4
<strong>输出:</strong>7
<strong>解释:</strong>7 = 4 * 3 / 2 + 1
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>10
<strong>输出:</strong>12
<strong>解释:</strong>12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li><code>1 &lt;= N &lt;= 10000</code></li>
<li><code>-2^31 &lt;= answer &lt;= 2^31 - 1</code>&nbsp; (答案保证符合 32 位整数。)</li>
</ol>
<div><div>Related Topics</div><div><li>数学</li></div></div>\n<div><li>👍 79</li><li>👎 0</li></div>

View File

@ -0,0 +1,84 @@
//每当用户执行变更文件夹操作时LeetCode 文件系统都会保存一条日志记录
//
// 下面给出对变更操作的说明
//
//
// "../" 移动到当前文件夹的父文件夹如果已经在主文件夹下 继续停留在当前文件夹
// "./" 继续停留在当前文件夹
// "x/" 移动到名为 x 的子文件夹中题目数据 保证总是存在文件夹 x
//
//
// 给你一个字符串列表 logs 其中 logs[i] 是用户在 ith 步执行的操作
//
// 文件系统启动时位于主文件夹然后执行 logs 中的操作
//
// 执行完所有变更文件夹操作后请你找出 返回主文件夹所需的最小步数
//
//
//
// 示例 1
//
//
//
// 输入logs = ["d1/","d2/","../","d21/","./"]
//输出2
//解释执行 "../" 操作变更文件夹 2 即可回到主文件夹
//
//
// 示例 2
//
//
//
// 输入logs = ["d1/","d2/","./","d3/","../","d31/"]
//输出3
//
//
// 示例 3
//
// 输入logs = ["d1/","../","../","../"]
//输出0
//
//
//
//
// 提示
//
//
// 1 <= logs.length <= 103
// 2 <= logs[i].length <= 10
// logs[i] 包含小写英文字母数字'.' '/'
// logs[i] 符合语句中描述的格式
// 文件夹名称由小写英文字母和数字组成
//
// Related Topics
// 👍 12 👎 0
package leetcode.editor.cn;
//1598:文件夹操作日志搜集器
public class CrawlerLogFolder {
public static void main(String[] args) {
//测试代码
Solution solution = new CrawlerLogFolder().new Solution();
solution.minOperations(new String[]{"d1/", "d2/", "../", "d21/", "./"});
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minOperations(String[] logs) {
int step = 0;
int size = logs.length;
for (String str : logs) {
if (str.equals("../")) {
step = step == 0 ? 0 : step - 1;
} else if (!str.equals("./")) {
step++;
}
}
return step;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,53 @@
<p>每当用户执行变更文件夹操作时LeetCode 文件系统都会保存一条日志记录。</p>
<p>下面给出对变更操作的说明:</p>
<ul>
<li><code>&quot;../&quot;</code> :移动到当前文件夹的父文件夹。如果已经在主文件夹下,则 <strong>继续停留在当前文件夹</strong></li>
<li><code>&quot;./&quot;</code> :继续停留在当前文件夹<strong></strong></li>
<li><code>&quot;x/&quot;</code> :移动到名为 <code>x</code> 的子文件夹中。题目数据 <strong>保证总是存在文件夹 <code>x</code></strong></li>
</ul>
<p>给你一个字符串列表 <code>logs</code> ,其中 <code>logs[i]</code> 是用户在 <code>i<sup>th</sup></code> 步执行的操作。</p>
<p>文件系统启动时位于主文件夹,然后执行 <code>logs</code> 中的操作。</p>
<p>执行完所有变更文件夹操作后,请你找出 <strong>返回主文件夹所需的最小步数</strong></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/26/sample_11_1957.png" style="height: 151px; width: 775px;"></p>
<pre><strong>输入:</strong>logs = [&quot;d1/&quot;,&quot;d2/&quot;,&quot;../&quot;,&quot;d21/&quot;,&quot;./&quot;]
<strong>输出:</strong>2
<strong>解释:</strong>执行 &quot;../&quot; 操作变更文件夹 2 次,即可回到主文件夹
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/26/sample_22_1957.png" style="height: 270px; width: 600px;"></p>
<pre><strong>输入:</strong>logs = [&quot;d1/&quot;,&quot;d2/&quot;,&quot;./&quot;,&quot;d3/&quot;,&quot;../&quot;,&quot;d31/&quot;]
<strong>输出:</strong>3
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>logs = [&quot;d1/&quot;,&quot;../&quot;,&quot;../&quot;,&quot;../&quot;]
<strong>输出:</strong>0
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= logs.length &lt;= 10<sup>3</sup></code></li>
<li><code>2 &lt;= logs[i].length &lt;= 10</code></li>
<li><code>logs[i]</code> 包含小写英文字母,数字,<code>&#39;.&#39;</code><code>&#39;/&#39;</code></li>
<li><code>logs[i]</code> 符合语句中描述的格式</li>
<li>文件夹名称由小写英文字母和数字组成</li>
</ul>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 12</li><li>👎 0</li></div>

View File

@ -68,7 +68,7 @@ public class FuZaLianBiaoDeFuZhiLcof {
List<Integer> var = Arrays.asList(7, 13, 11, 10, 1); List<Integer> var = Arrays.asList(7, 13, 11, 10, 1);
List<Integer> random = Arrays.asList(null, 0, 4, 2, 0); List<Integer> random = Arrays.asList(null, 0, 4, 2, 0);
Node head = new Node(0); Node head = new Node(0);
head = head.setHead(var, random); // head = head.setHead(var, random);
solution.copyRandomList(head); solution.copyRandomList(head);
} }

View File

@ -0,0 +1,135 @@
//请你仅使用两个栈实现先入先出队列队列应当支持一般队列支持的所有操作pushpoppeekempty
//
// 实现 MyQueue
//
//
// void push(int x) 将元素 x 推到队列的末尾
// int pop() 从队列的开头移除并返回元素
// int peek() 返回队列开头的元素
// boolean empty() 如果队列为空返回 true 否则返回 false
//
//
//
//
// 说明
//
//
// 你只能使用标准的栈操作 也就是只有 push to top, peek/pop from top, size, is empty 操作是合法的
//
// 你所使用的语言也许不支持栈你可以使用 list 或者 deque双端队列来模拟一个栈只要是标准的栈操作即可
//
//
//
//
// 进阶
//
//
// 你能否实现每个操作均摊时间复杂度为 O(1) 的队列换句话说执行 n 个操作的总时间复杂度为 O(n) 即使其中一个操作可能花费较长时间
//
//
//
//
// 示例
//
//
//输入
//["MyQueue", "push", "push", "peek", "pop", "empty"]
//[[], [1], [2], [], [], []]
//输出
//[null, null, null, 1, 1, false]
//
//解释
//MyQueue myQueue = new MyQueue();
//myQueue.push(1); // queue is: [1]
//myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
//myQueue.peek(); // return 1
//myQueue.pop(); // return 1, queue is [2]
//myQueue.empty(); // return false
//
//
//
//
//
//
//
// 提示
//
//
// 1 <= x <= 9
// 最多调用 100 pushpoppeek empty
// 假设所有操作都是有效的 例如一个空的队列不会调用 pop 或者 peek 操作
//
// Related Topics 设计
// 👍 377 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//232:用栈实现队列
public class ImplementQueueUsingStacks {
public static void main(String[] args) {
//测试代码
// Solution solution = new ImplementQueueUsingStacks().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
/**
* Initialize your data structure here.
*/
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
/**
* Push element x to the back of queue.
*/
public void push(int x) {
while (!stack1.empty()){
stack2.push(stack1.pop());
}
stack1.push(x);
while (!stack2.isEmpty()){
stack1.push(stack2.pop());
}
}
/**
* Removes the element from in front of queue and returns that element.
*/
public int pop() {
return stack1.pop();
}
/**
* Get the front element.
*/
public int peek() {
return stack1.peek();
}
/**
* Returns whether the queue is empty.
*/
public boolean empty() {
return stack1.empty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,61 @@
<p>请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(<code>push</code><code>pop</code><code>peek</code><code>empty</code></p>
<p>实现 <code>MyQueue</code> 类:</p>
<ul>
<li><code>void push(int x)</code> 将元素 x 推到队列的末尾</li>
<li><code>int pop()</code> 从队列的开头移除并返回元素</li>
<li><code>int peek()</code> 返回队列开头的元素</li>
<li><code>boolean empty()</code> 如果队列为空,返回 <code>true</code> ;否则,返回 <code>false</code></li>
</ul>
<p> </p>
<p><strong>说明:</strong></p>
<ul>
<li>你只能使用标准的栈操作 —— 也就是只有 <code>push to top</code>, <code>peek/pop from top</code>, <code>size</code>, 和 <code>is empty</code> 操作是合法的。</li>
<li>你所使用的语言也许不支持栈。你可以使用 list 或者 deque双端队列来模拟一个栈只要是标准的栈操作即可。</li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>你能否实现每个操作均摊时间复杂度为 <code>O(1)</code> 的队列?换句话说,执行 <code>n</code> 个操作的总时间复杂度为 <code>O(n)</code> ,即使其中一个操作可能花费较长时间。</li>
</ul>
<p> </p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong>
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
<strong>输出:</strong>
[null, null, null, 1, 1, false]
<strong>解释:</strong>
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
</pre>
<ul>
</ul>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= x <= 9</code></li>
<li>最多调用 <code>100</code><code>push</code><code>pop</code><code>peek</code><code>empty</code></li>
<li>假设所有操作都是有效的 (例如,一个空的队列不会调用 <code>pop</code> 或者 <code>peek</code> 操作)</li>
</ul>
<div><div>Related Topics</div><div><li></li><li>设计</li></div></div>\n<div><li>👍 377</li><li>👎 0</li></div>

View File

@ -0,0 +1,78 @@
//实现一个MyQueue类该类用两个栈来实现一个队列 示例 MyQueue queue = new MyQueue(); queue.push(1);
//queue.push(2); queue.peek(); // 返回 1 queue.pop(); // 返回 1 queue.empty(); //
// false 说明 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size is empty
// 操作是合法的 你所使用的语言也许不支持栈你可以使用 list 或者 deque双端队列来模拟一个栈只要是标准的栈操作即可 假设所有操作都是有效的
//例如一个空的队列不会调用 pop 或者 peek 操作 Related Topics
// 👍 35 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//面试题 03.04:化栈为队
public class ImplementQueueUsingStacksLcci {
public static void main(String[] args) {
//测试代码
// Solution solution = new ImplementQueueUsingStacksLcci().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
/**
* Initialize your data structure here.
*/
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
/**
* Push element x to the back of queue.
*/
public void push(int x) {
while (!stack1.empty()) {
stack2.push(stack1.pop());
}
stack1.push(x);
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
}
/**
* Removes the element from in front of queue and returns that element.
*/
public int pop() {
return stack1.pop();
}
/**
* Get the front element.
*/
public int peek() {
return stack1.peek();
}
/**
* Returns whether the queue is empty.
*/
public boolean empty() {
return stack1.empty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1 @@
<p>实现一个MyQueue类该类用两个栈来实现一个队列。</p><br><p><strong>示例:</strong><pre>MyQueue queue = new MyQueue();<br><br>queue.push(1);<br>queue.push(2);<br>queue.peek(); // 返回 1<br>queue.pop(); // 返回 1<br>queue.empty(); // 返回 false</pre></p><br><p><strong>说明:</strong><br><ul><li>你只能使用标准的栈操作 -- 也就是只有 <code>push to top</code>, <code>peek/pop from top</code>, <code>size</code><code>is empty</code> 操作是合法的。</li><li>你所使用的语言也许不支持栈。你可以使用 list 或者 deque双端队列来模拟一个栈只要是标准的栈操作即可。</li><li>假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。</li></ul></p><div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 35</li><li>👎 0</li></div>

View File

@ -0,0 +1,148 @@
//请你仅使用两个队列实现一个后入先出LIFO的栈并支持普通队列的全部四种操作pushtoppop empty
//
// 实现 MyStack
//
//
// void push(int x) 将元素 x 压入栈顶
// int pop() 移除并返回栈顶元素
// int top() 返回栈顶元素
// boolean empty() 如果栈是空的返回 true 否则返回 false
//
//
//
//
// 注意
//
//
// 你只能使用队列的基本操作 也就是 push to backpeek/pop from frontsize is empty 这些操作
// 你所使用的语言也许不支持队列 你可以使用 list 列表或者 deque双端队列来模拟一个队列 , 只要是标准的队列操作即可
//
//
//
//
// 示例
//
//
//输入
//["MyStack", "push", "push", "top", "pop", "empty"]
//[[], [1], [2], [], [], []]
//输出
//[null, null, null, 2, 2, false]
//
//解释
//MyStack myStack = new MyStack();
//myStack.push(1);
//myStack.push(2);
//myStack.top(); // 返回 2
//myStack.pop(); // 返回 2
//myStack.empty(); // 返回 False
//
//
//
//
// 提示
//
//
// 1 <= x <= 9
// 最多调用100 pushpoptop empty
// 每次调用 pop top 都保证栈不为空
//
//
//
//
// 进阶你能否实现每种操作的均摊时间复杂度为 O(1) 的栈换句话说执行 n 个操作的总时间复杂度 O(n) 尽管其中某个操作可能需要比其他操作更长的
//时间你可以使用两个以上的队列
// Related Topics 设计
// 👍 305 👎 0
package leetcode.editor.cn;
import java.util.LinkedList;
import java.util.Queue;
//225:用队列实现栈
public class ImplementStackUsingQueues {
public static void main(String[] args) {
//测试代码
// Solution solution = new ImplementStackUsingQueues().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
int flag = 1;
/**
* Initialize your data structure here.
*/
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
/**
* Push element x onto stack.
*/
public void push(int x) {
if (flag == 1) {
queue2.offer(x);
while (!queue1.isEmpty()) {
queue2.offer(queue1.poll());
}
flag = 2;
} else {
queue1.offer(x);
while (!queue2.isEmpty()) {
queue1.offer(queue2.poll());
}
flag = 1;
}
}
/**
* Removes the element on top of the stack and returns that element.
*/
public int pop() {
if (flag == 1) {
return queue1.poll();
} else {
return queue2.poll();
}
}
/**
* Get the top element.
*/
public int top() {
if (flag == 1) {
return queue1.peek();
} else {
return queue2.peek();
}
}
/**
* Returns whether the stack is empty.
*/
public boolean empty() {
if (flag == 1) {
return queue1.isEmpty();
} else {
return queue2.isEmpty();
}
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,54 @@
<p>请你仅使用两个队列实现一个后入先出LIFO的栈并支持普通队列的全部四种操作<code>push</code><code>top</code><code>pop</code><code>empty</code>)。</p>
<p>实现 <code>MyStack</code> 类:</p>
<ul>
<li><code>void push(int x)</code> 将元素 x 压入栈顶。</li>
<li><code>int pop()</code> 移除并返回栈顶元素。</li>
<li><code>int top()</code> 返回栈顶元素。</li>
<li><code>boolean empty()</code> 如果栈是空的,返回 <code>true</code> ;否则,返回 <code>false</code></li>
</ul>
<p> </p>
<p><strong>注意:</strong></p>
<ul>
<li>你只能使用队列的基本操作 —— 也就是 <code>push to back</code><code>peek/pop from front</code><code>size</code> 和 <code>is empty</code> 这些操作。</li>
<li>你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque双端队列来模拟一个队列 , 只要是标准的队列操作即可。</li>
</ul>
<p> </p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong>
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
<strong>输出:</strong>
[null, null, null, 2, 2, false]
<strong>解释:</strong>
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= x <= 9</code></li>
<li>最多调用<code>100</code><code>push</code><code>pop</code><code>top</code><code>empty</code></li>
<li>每次调用 <code>pop</code><code>top</code> 都保证栈不为空</li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你能否实现每种操作的均摊时间复杂度为 <code>O(1)</code> 的栈?换句话说,执行 <code>n</code> 个操作的总时间复杂度 <code>O(n)</code> ,尽管其中某个操作可能需要比其他操作更长的时间。你可以使用两个以上的队列。</p>
<div><div>Related Topics</div><div><li></li><li>设计</li></div></div>\n<div><li>👍 305</li><li>👎 0</li></div>

View File

@ -0,0 +1,86 @@
//给你一个由大小写英文字母组成的字符串 s
//
// 一个整理好的字符串中两个相邻字符 s[i] s[i+1]其中 0<= i <= s.length-2 要满足如下条件:
//
//
// s[i] 是小写字符 s[i+1] 不可以是相同的大写字符
// s[i] 是大写字符 s[i+1] 不可以是相同的小写字符
//
//
// 请你将字符串整理好每次你都可以从字符串中选出满足上述条件的 两个相邻 字符并删除直到字符串整理好为止
//
// 请返回整理好的 字符串 题目保证在给出的约束条件下测试样例对应的答案是唯一的
//
// 注意空字符串也属于整理好的字符串尽管其中没有任何字符
//
//
//
// 示例 1
//
//
//输入s = "leEeetcode"
//输出"leetcode"
//解释无论你第一次选的是 i = 1 还是 i = 2都会使 "leEeetcode" 缩减为 "leetcode"
//
//
// 示例 2
//
//
//输入s = "abBAcC"
//输出""
//解释存在多种不同情况但所有的情况都会导致相同的结果例如
//"abBAcC" --> "aAcC" --> "cC" --> ""
//"abBAcC" --> "abBA" --> "aA" --> ""
//
//
// 示例 3
//
//
//输入s = "s"
//输出"s"
//
//
//
//
// 提示
//
//
// 1 <= s.length <= 100
// s 只包含小写和大写英文字母
//
// Related Topics 字符串
// 👍 21 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//1544:整理字符串
public class MakeTheStringGreat {
public static void main(String[] args) {
//测试代码
Solution solution = new MakeTheStringGreat().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String makeGood(String s) {
Stack<Character> stack = new Stack<>();
for (char ch : s.toCharArray()) {
if (!stack.isEmpty() && (stack.peek() - ch == 32 || stack.peek() - ch == -32)) {
stack.pop();
} else {
stack.push(ch);
}
}
s = "";
while (!stack.isEmpty()) {
s = stack.pop() + s;
}
return s;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,51 @@
<p>给你一个由大小写英文字母组成的字符串 <code>s</code></p>
<p>一个整理好的字符串中,两个相邻字符 <code>s[i]</code><code>s[i+1]</code>,其中 <code>0<= i <= s.length-2</code> ,要满足如下条件:</p>
<ul>
<li><code>s[i]</code> 是小写字符,则 <code>s[i+1]</code> 不可以是相同的大写字符。</li>
<li><code>s[i]</code> 是大写字符,则 <code>s[i+1]</code> 不可以是相同的小写字符。</li>
</ul>
<p>请你将字符串整理好,每次你都可以从字符串中选出满足上述条件的 <strong>两个相邻</strong> 字符并删除,直到字符串整理好为止。</p>
<p>请返回整理好的 <strong>字符串</strong> 。题目保证在给出的约束条件下,测试样例对应的答案是唯一的。</p>
<p><strong>注意:</strong>空字符串也属于整理好的字符串,尽管其中没有任何字符。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "leEeetcode"
<strong>输出:</strong>"leetcode"
<strong>解释:</strong>无论你第一次选的是 i = 1 还是 i = 2都会使 "leEeetcode" 缩减为 "leetcode" 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = "abBAcC"
<strong>输出:</strong>""
<strong>解释:</strong>存在多种不同情况,但所有的情况都会导致相同的结果。例如:
"abBAcC" --> "aAcC" --> "cC" --> ""
"abBAcC" --> "abBA" --> "aA" --> ""
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>s = "s"
<strong>输出:</strong>"s"
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> 只包含小写和大写英文字母</li>
</ul>
<div><div>Related Topics</div><div><li></li><li>字符串</li></div></div>\n<div><li>👍 21</li><li>👎 0</li></div>

View File

@ -0,0 +1,139 @@
//设计一个支持 push pop top 操作并能在常数时间内检索到最小元素的栈
//
//
// push(x) 将元素 x 推入栈中
// pop() 删除栈顶的元素
// top() 获取栈顶元素
// getMin() 检索栈中的最小元素
//
//
//
//
// 示例:
//
// 输入
//["MinStack","push","push","push","getMin","pop","top","getMin"]
//[[],[-2],[0],[-3],[],[],[],[]]
//
//输出
//[null,null,null,null,-3,null,0,-2]
//
//解释
//MinStack minStack = new MinStack();
//minStack.push(-2);
//minStack.push(0);
//minStack.push(-3);
//minStack.getMin(); --> 返回 -3.
//minStack.pop();
//minStack.top(); --> 返回 0.
//minStack.getMin(); --> 返回 -2.
//
//
//
//
// 提示
//
//
// poptop getMin 操作总是在 非空栈 上调用
//
// Related Topics 设计
// 👍 856 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//155:最小栈
public class MinStack {
public static void main(String[] args) {
//测试代码
// Solution solution = new MinStack1().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MinStack1 {
// Stack<Integer> stack;
// Stack<Integer> min;
// public MinStack() {
// stack = new Stack<>();
// min = new Stack<>();
// }
// public void push(int val) {
// if (stack.isEmpty()) {
// min.push(val);
// } else {
// min.push(Math.min(min.peek(),val));
// }
// stack.push(val);
// }
// public void pop() {
// stack.pop();
// min.pop();
// }
// public int top() {
// return stack.peek();
// }
// public int getMin() {
// return min.peek();
// }
class Data{
int num;
int min;
public Data(int num, int min) {
this.num = num;
this.min = min;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
}
Stack<Data> stack;
public MinStack1() {
stack = new Stack<>();
}
public void push(int val) {
if (stack.isEmpty()) {
stack.push(new Data(val,val));
} else {
stack.push(new Data(val,Math.min(val,stack.peek().min)));
}
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek().num;
}
public int getMin() {
return stack.peek().min;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,39 @@
<p>设计一个支持 <code>push</code> <code>pop</code> <code>top</code> 操作,并能在常数时间内检索到最小元素的栈。</p>
<ul>
<li><code>push(x)</code> &mdash;&mdash; 将元素 x 推入栈中。</li>
<li><code>pop()</code>&nbsp;&mdash;&mdash; 删除栈顶的元素。</li>
<li><code>top()</code>&nbsp;&mdash;&mdash; 获取栈顶元素。</li>
<li><code>getMin()</code> &mdash;&mdash; 检索栈中的最小元素。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>
[&quot;MinStack&quot;,&quot;push&quot;,&quot;push&quot;,&quot;push&quot;,&quot;getMin&quot;,&quot;pop&quot;,&quot;top&quot;,&quot;getMin&quot;]
[[],[-2],[0],[-3],[],[],[],[]]
<strong>输出:</strong>
[null,null,null,null,-3,null,0,-2]
<strong>解释:</strong>
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --&gt; 返回 -3.
minStack.pop();
minStack.top(); --&gt; 返回 0.
minStack.getMin(); --&gt; 返回 -2.
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>pop</code><code>top</code><code>getMin</code> 操作总是在 <strong>非空栈</strong> 上调用。</li>
</ul>
<div><div>Related Topics</div><div><li></li><li>设计</li></div></div>\n<div><li>👍 856</li><li>👎 0</li></div>

View File

@ -0,0 +1,84 @@
//请设计一个栈除了常规栈支持的pop与push函数以外还支持min函数该函数返回栈元素中的最小值执行pushpop和min操作的时间复杂度必须为O(
//1) 示例 MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0);
// minStack.push(-3); minStack.getMin(); --> 返回 -3. minStack.pop(); minStack.top
//(); --> 返回 0. minStack.getMin(); --> 返回 -2. Related Topics
// 👍 41 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//面试题 03.02:栈的最小值
public class MinStackLcci {
public static void main(String[] args) {
//测试代码
// Solution solution = new MinStackLcci().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MinStack {
class Data {
int num;
int min;
public Data(int num, int min) {
this.num = num;
this.min = min;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
}
Stack<Data> stack;
public MinStack() {
stack = new Stack<>();
}
public void push(int val) {
if (stack.isEmpty()) {
stack.push(new Data(val,val));
} else {
stack.push(new Data(val, Math.min(val, stack.peek().min)));
}
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek().num;
}
public int getMin() {
return stack.peek().min;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1 @@
<p>请设计一个栈除了常规栈支持的pop与push函数以外还支持min函数该函数返回栈元素中的最小值。执行push、pop和min操作的时间复杂度必须为O(1)。</p><br><p><strong>示例:</strong><pre>MinStack minStack = new MinStack();<br>minStack.push(-2);<br>minStack.push(0);<br>minStack.push(-3);<br>minStack.getMin(); --> 返回 -3.<br>minStack.pop();<br>minStack.top(); --> 返回 0.<br>minStack.getMin(); --> 返回 -2.</pre></p><div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 41</li><li>👎 0</li></div>

View File

@ -0,0 +1,92 @@
//给你两个 没有重复元素 的数组 nums1 nums2 其中nums1 nums2 的子集
//
// 请你找出 nums1 中每个元素在 nums2 中的下一个比其大的值
//
// nums1 中数字 x 的下一个更大元素是指 x nums2 中对应位置的右边的第一个比 x 大的元素如果不存在对应位置输出 -1
//
//
//
// 示例 1:
//
//
//输入: nums1 = [4,1,2], nums2 = [1,3,4,2].
//输出: [-1,3,-1]
//解释:
// 对于 num1 中的数字 4 你无法在第二个数组中找到下一个更大的数字因此输出 -1
// 对于 num1 中的数字 1 第二个数组中数字1右边的下一个较大数字是 3
// 对于 num1 中的数字 2 第二个数组中没有下一个更大的数字因此输出 -1
//
// 示例 2:
//
//
//输入: nums1 = [2,4], nums2 = [1,2,3,4].
//输出: [3,-1]
//解释:
//  对于 num1 中的数字 2 第二个数组中的下一个较大数字是 3
// 对于 num1 中的数字 4 第二个数组中没有下一个更大的数字因此输出 -1
//
//
//
//
// 提示
//
//
// 1 <= nums1.length <= nums2.length <= 1000
// 0 <= nums1[i], nums2[i] <= 104
// nums1和nums2中所有整数 互不相同
// nums1 中的所有整数同样出现在 nums2
//
//
//
//
// 进阶你可以设计一个时间复杂度为 O(nums1.length + nums2.length) 的解决方案吗
// Related Topics
// 👍 397 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.Stack;
//496:下一个更大元素 I
public class NextGreaterElementI {
public static void main(String[] args) {
//测试代码
Solution solution = new NextGreaterElementI().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int size = nums1.length;
int[] result = new int[size];
Stack<Integer> stack = new Stack<>();
for (int i = size - 1; i >= 0; i--) {
result[i] = -2;
stack.push(nums1[i]);
}
int length = nums2.length;
int index = 0;
while (!stack.isEmpty()) {
int i;
for (i = 0; i < length; i++) {
if (result[index] == -2 && nums2[i] == stack.peek()) {
result[index] = -1;
} else if (result[index] == -1 && nums2[i] > stack.peek()) {
result[index] = nums2[i];
stack.pop();
break;
}
}
if (i == length && stack.peek() != result[index]) {
stack.pop();
}
index++;
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,43 @@
<p>给你两个<strong> 没有重复元素</strong> 的数组 <code>nums1</code> 和 <code>nums2</code> ,其中<code>nums1</code> 是 <code>nums2</code> 的子集。</p>
<p>请你找出 <code>nums1</code> 中每个元素在 <code>nums2</code> 中的下一个比其大的值。</p>
<p><code>nums1</code> 中数字 <code>x</code> 的下一个更大元素是指 <code>x</code> 在 <code>nums2</code> 中对应位置的右边的第一个比 <code>x</code><strong> </strong>大的元素。如果不存在,对应位置输出 <code>-1</code></p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2].
<strong>输出:</strong> [-1,3,-1]
<strong>解释:</strong>
对于 num1 中的数字 4 ,你无法在第二个数组中找到下一个更大的数字,因此输出 -1 。
对于 num1 中的数字 1 第二个数组中数字1右边的下一个较大数字是 3 。
对于 num1 中的数字 2 ,第二个数组中没有下一个更大的数字,因此输出 -1 。</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> nums1 = [2,4], nums2 = [1,2,3,4].
<strong>输出:</strong> [3,-1]
<strong>解释:</strong>
  对于 num1 中的数字 2 ,第二个数组中的下一个较大数字是 3 。
对于 num1 中的数字 4 ,第二个数组中没有下一个更大的数字,因此输出 -1 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
<li><code>nums1</code><code>nums2</code>中所有整数 <strong>互不相同</strong></li>
<li><code>nums1</code> 中的所有整数同样出现在 <code>nums2</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你可以设计一个时间复杂度为 <code>O(nums1.length + nums2.length)</code> 的解决方案吗?</p>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 397</li><li>👎 0</li></div>

View File

@ -0,0 +1,62 @@
//给出由小写字母组成的字符串 S重复项删除操作会选择两个相邻且相同的字母并删除它们
//
// S 上反复执行重复项删除操作直到无法继续删除
//
// 在完成所有重复项删除操作后返回最终的字符串答案保证唯一
//
//
//
// 示例
//
// 输入"abbaca"
//输出"ca"
//解释
//例如 "abbaca" 我们可以删除 "bb" 由于两字母相邻且相同这是此时唯一可以执行删除操作的重复项之后我们得到字符串 "aaca"其中又
//只有 "aa" 可以执行重复项删除操作所以最后的字符串为 "ca"
//
//
//
//
// 提示
//
//
// 1 <= S.length <= 20000
// S 仅由小写英文字母组成
//
// Related Topics
// 👍 247 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//1047:删除字符串中的所有相邻重复项
public class RemoveAllAdjacentDuplicatesInString {
public static void main(String[] args) {
//测试代码
Solution solution = new RemoveAllAdjacentDuplicatesInString().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String removeDuplicates(String S) {
Stack<Character> stack = new Stack<>();
for (char ch : S.toCharArray()) {
if (!stack.isEmpty() && stack.peek() == ch) {
stack.pop();
} else {
stack.push(ch);
}
}
StringBuilder SBuilder = new StringBuilder();
while (!stack.isEmpty()) {
SBuilder.insert(0, stack.pop());
}
S = SBuilder.toString();
return S;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,25 @@
<p>给出由小写字母组成的字符串&nbsp;<code>S</code><strong>重复项删除操作</strong>会选择两个相邻且相同的字母,并删除它们。</p>
<p>在 S 上反复执行重复项删除操作,直到无法继续删除。</p>
<p>在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。</p>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>&quot;abbaca&quot;
<strong>输出:</strong>&quot;ca&quot;
<strong>解释:</strong>
例如,在 &quot;abbaca&quot; 中,我们可以删除 &quot;bb&quot; 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 &quot;aaca&quot;,其中又只有 &quot;aa&quot; 可以执行重复项删除操作,所以最后的字符串为 &quot;ca&quot;
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li><code>1 &lt;= S.length &lt;= 20000</code></li>
<li><code>S</code> 仅由小写英文字母组成。</li>
</ol>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 247</li><li>👎 0</li></div>

View File

@ -0,0 +1,79 @@
//有效括号字符串为空 ("")"(" + A + ")" A + B其中 A B 都是有效的括号字符串+ 代表字符串的连接例如"""()"
//"(())()" "(()(()))" 都是有效的括号字符串
//
// 如果有效字符串 S 非空且不存在将其拆分为 S = A+B 的方法我们称其为原语primitive其中 A B 都是非空有效括号字符串
//
// 给出一个非空有效字符串 S考虑将其进行原语化分解使得S = P_1 + P_2 + ... + P_k其中 P_i 是有效括号字符串原语
//
// S 进行原语化分解删除分解中每个原语字符串的最外层括号返回 S
//
//
//
// 示例 1
//
// 输入"(()())(())"
//输出"()()()"
//解释
//输入字符串为 "(()())(())"原语化分解得到 "(()())" + "(())"
//删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"
//
// 示例 2
//
// 输入"(()())(())(()(()))"
//输出"()()()()(())"
//解释
//输入字符串为 "(()())(())(()(()))"原语化分解得到 "(()())" + "(())" + "(()(()))"
//删除每个部分中的最外层括号后得到 "()()" + "()" + "()(())" = "()()()()(())"
//
//
// 示例 3
//
// 输入"()()"
//输出""
//解释
//输入字符串为 "()()"原语化分解得到 "()" + "()"
//删除每个部分中的最外层括号后得到 "" + "" = ""
//
//
//
//
// 提示
//
//
// S.length <= 10000
// S[i] "(" ")"
// S 是一个有效括号字符串
//
// Related Topics
// 👍 163 👎 0
package leetcode.editor.cn;
//1021:删除最外层的括号
public class RemoveOutermostParentheses {
public static void main(String[] args) {
//测试代码
Solution solution = new RemoveOutermostParentheses().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String removeOuterParentheses(String S) {
int count = 0;
String result = "";
for (char ch : S.toCharArray()) {
if (ch == '(') {
count++;
result = count > 1 ? result + ch : result;
}else{
count--;
result = count > 0 ? result + ch : result;
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,46 @@
<p>有效括号字符串为空&nbsp;<code>(&quot;&quot;)</code><code>&quot;(&quot; + A + &quot;)&quot;</code>&nbsp;&nbsp;<code>A + B</code>,其中&nbsp;<code>A</code>&nbsp;<code>B</code>&nbsp;都是有效的括号字符串,<code>+</code>&nbsp;代表字符串的连接。例如,<code>&quot;&quot;</code><code>&quot;()&quot;</code><code>&quot;(())()&quot;</code>&nbsp;&nbsp;<code>&quot;(()(()))&quot;</code>&nbsp;都是有效的括号字符串。</p>
<p>如果有效字符串&nbsp;<code>S</code>&nbsp;非空,且不存在将其拆分为&nbsp;<code>S = A+B</code>&nbsp;的方法,我们称其为<strong>原语primitive</strong>,其中&nbsp;<code>A</code>&nbsp;<code>B</code>&nbsp;都是非空有效括号字符串。</p>
<p>给出一个非空有效字符串&nbsp;<code>S</code>,考虑将其进行原语化分解,使得:<code>S = P_1 + P_2 + ... + P_k</code>,其中&nbsp;<code>P_i</code>&nbsp;是有效括号字符串原语。</p>
<p>&nbsp;<code>S</code>&nbsp;进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 <code>S</code>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>&quot;(()())(())&quot;
<strong>输出:</strong>&quot;()()()&quot;
<strong>解释:
</strong>输入字符串为 &quot;(()())(())&quot;,原语化分解得到 &quot;(()())&quot; + &quot;(())&quot;
删除每个部分中的最外层括号后得到 &quot;()()&quot; + &quot;()&quot; = &quot;()()()&quot;</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>&quot;(()())(())(()(()))&quot;
<strong>输出:</strong>&quot;()()()()(())&quot;
<strong>解释:</strong>
输入字符串为 &quot;(()())(())(()(()))&quot;,原语化分解得到 &quot;(()())&quot; + &quot;(())&quot; + &quot;(()(()))&quot;
删除每个部分中的最外层括号后得到 &quot;()()&quot; + &quot;()&quot; + &quot;()(())&quot; = &quot;()()()()(())&quot;
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>&quot;()()&quot;
<strong>输出:</strong>&quot;&quot;
<strong>解释:</strong>
输入字符串为 &quot;()()&quot;,原语化分解得到 &quot;()&quot; + &quot;()&quot;
删除每个部分中的最外层括号后得到 &quot;&quot; + &quot;&quot; = &quot;&quot;
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li><code>S.length &lt;= 10000</code></li>
<li><code>S[i]</code>&nbsp;<code>&quot;(&quot;</code>&nbsp;<code>&quot;)&quot;</code></li>
<li><code>S</code> 是一个有效括号字符串</li>
</ol>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 163</li><li>👎 0</li></div>

View File

@ -0,0 +1,104 @@
//给你一个字符串 path 表示指向某一文件或目录的 Unix 风格 绝对路径 '/' 开头请你将其转化为更加简洁的规范路径
//
// Unix 风格的文件系统中一个点.表示当前目录本身此外两个点 .. 表示将目录切换到上一级指向父目录两者都可以是复杂相对路径的组成
//部分任意多个连续的斜杠'//'都被视为单个斜杠 '/' 对于此问题任何其他格式的点例如'...'均被视为文件/目录名称
//
// 请注意返回的 规范路径 必须遵循下述格式
//
//
// 始终以斜杠 '/' 开头
// 两个目录名之间必须只有一个斜杠 '/'
// 最后一个目录名如果存在不能 '/' 结尾
// 此外路径仅包含从根目录到目标文件或目录的路径上的目录不含 '.' '..'
//
//
// 返回简化后得到的 规范路径
//
//
//
// 示例 1
//
//
//输入path = "/home/"
//输出"/home"
//解释注意最后一个目录名后面没有斜杠
//
// 示例 2
//
//
//输入path = "/../"
//输出"/"
//解释从根目录向上一级是不可行的因为根目录是你可以到达的最高级
//
//
// 示例 3
//
//
//输入path = "/home//foo/"
//输出"/home/foo"
//解释在规范路径中多个连续斜杠需要用一个斜杠替换
//
//
// 示例 4
//
//
//输入path = "/a/./b/../../c/"
//输出"/c"
//
//
//
//
// 提示
//
//
// 1 <= path.length <= 3000
// path 由英文字母数字'.''/' '_' 组成
// path 是一个有效的 Unix 风格绝对路径
//
// Related Topics 字符串
// 👍 260 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//71:简化路径
public class SimplifyPath {
public static void main(String[] args) {
//测试代码
Solution solution = new SimplifyPath().new Solution();
solution.simplifyPath("/home/");
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String simplifyPath(String path) {
Stack<String> stack = new Stack<>();
while (path.contains("//")) {
path = path.replace("//", "/");
}
String[] paths = path.split("/");
for (String str : paths) {
if (str.equals(".")) {
continue;
} else if (str.equals("..")) {
if (!stack.isEmpty()) {
stack.pop();
}
} else {
if (!str.equals("")) {
stack.push(str);
}
}
}
path = "";
while (!stack.isEmpty()) {
path = "/" + stack.pop() + path;
}
return path.equals("") ? "/" : path;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,57 @@
<p>给你一个字符串 <code>path</code> 表示指向某一文件或目录的 Unix 风格 <strong>绝对路径 </strong>(以 <code>'/'</code> 开头),请你将其转化为更加简洁的规范路径。</p>
<p class="MachineTrans-lang-zh-CN">在 Unix 风格的文件系统中,一个点(<code>.</code>)表示当前目录本身;此外,两个点 <code>..</code>) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,<code>'//'</code>)都被视为单个斜杠 <code>'/'</code> 。 对于此问题,任何其他格式的点(例如,<code>'...'</code>)均被视为文件/目录名称。</p>
<p>请注意,返回的 <strong>规范路径</strong> 必须遵循下述格式:</p>
<ul>
<li>始终以斜杠 <code>'/'</code> 开头。</li>
<li>两个目录名之间必须只有一个斜杠 <code>'/'</code></li>
<li>最后一个目录名(如果存在)<strong>不能 </strong><code>'/'</code> 结尾。</li>
<li>此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 <code>'.'</code><code>'..'</code>)。</li>
</ul>
<p>返回简化后得到的 <strong>规范路径</strong></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>path = "/home/"
<strong>输出:</strong>"/home"
<strong>解释:</strong>注意,最后一个目录名后面没有斜杠。 </pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>path = "/../"
<strong>输出:</strong>"/"
<strong>解释:</strong>从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>path = "/home//foo/"
<strong>输出:</strong>"/home/foo"
<strong>解释:</strong>在规范路径中,多个连续斜杠需要用一个斜杠替换。
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>path = "/a/./b/../../c/"
<strong>输出:</strong>"/c"
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= path.length <= 3000</code></li>
<li><code>path</code> 由英文字母,数字,<code>'.'</code><code>'/'</code><code>'_'</code> 组成。</li>
<li><code>path</code> 是一个有效的 Unix 风格绝对路径。</li>
</ul>
<div><div>Related Topics</div><div><li></li><li>字符串</li></div></div>\n<div><li>👍 260</li><li>👎 0</li></div>

View File

@ -0,0 +1,98 @@
//给定一个只包括 '('')''{''}''['']' 的字符串 s 判断字符串是否有效
//
// 有效字符串需满足
//
//
// 左括号必须用相同类型的右括号闭合
// 左括号必须以正确的顺序闭合
//
//
//
//
// 示例 1
//
//
//输入s = "()"
//输出true
//
//
// 示例 2
//
//
//输入s = "()[]{}"
//输出true
//
//
// 示例 3
//
//
//输入s = "(]"
//输出false
//
//
// 示例 4
//
//
//输入s = "([)]"
//输出false
//
//
// 示例 5
//
//
//输入s = "{[]}"
//输出true
//
//
//
// 提示
//
//
// 1 <= s.length <= 104
// s 仅由括号 '()[]{}' 组成
//
// Related Topics 字符串
// 👍 2289 👎 0
package leetcode.editor.cn;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
//20:有效的括号
public class ValidParentheses {
public static void main(String[] args) {
//测试代码
Solution solution = new ValidParentheses().new Solution();
solution.isValid("()");
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isValid(String s) {
Stack<String> stack = new Stack<>();
int length = s.length();
if (length % 2 == 1) {
return false;
}
Map<String, String> map = new HashMap<String, String>() {
{
put(")", "(");
put("}", "{");
put("]", "[");
}
};
for (int i = 0; i < length; i++) {
String ch = s.substring(i, i + 1);
if (!map.containsKey(ch) || stack.isEmpty() || !map.get(ch).equals(stack.pop())) {
stack.push(ch);
}
}
return stack.isEmpty();
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,75 @@
//用两个栈实现一个队列队列的声明如下请实现它的两个函数 appendTail deleteHead 分别完成在队列尾部插入整数和在队列头部删除整数的
//功能(若队列中没有元素deleteHead 操作返回 -1 )
//
//
//
// 示例 1
//
// 输入
//["CQueue","appendTail","deleteHead","deleteHead"]
//[[],[3],[],[]]
//输出[null,null,3,-1]
//
//
// 示例 2
//
// 输入
//["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
//[[],[],[5],[2],[],[]]
//输出[null,-1,null,null,5,2]
//
//
// 提示
//
//
// 1 <= values <= 10000
// 最多会对 appendTaildeleteHead 进行 10000 次调用
//
// Related Topics 设计
// 👍 212 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//剑指 Offer 09:用两个栈实现队列
public class YongLiangGeZhanShiXianDuiLieLcof {
public static void main(String[] args) {
//测试代码
// Solution solution = new YongLiangGeZhanShiXianDuiLieLcof().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class CQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public CQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.isEmpty() ? -1 : stack2.pop();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,27 @@
<p>用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 <code>appendTail</code><code>deleteHead</code> ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,<code>deleteHead</code>&nbsp;操作返回 -1 )</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>
[&quot;CQueue&quot;,&quot;appendTail&quot;,&quot;deleteHead&quot;,&quot;deleteHead&quot;]
[[],[3],[],[]]
<strong>输出:</strong>[null,null,3,-1]
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>
[&quot;CQueue&quot;,&quot;deleteHead&quot;,&quot;appendTail&quot;,&quot;appendTail&quot;,&quot;deleteHead&quot;,&quot;deleteHead&quot;]
[[],[],[5],[2],[],[]]
<strong>输出:</strong>[null,-1,null,null,5,2]
</pre>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= values &lt;= 10000</code></li>
<li><code>最多会对&nbsp;appendTail、deleteHead 进行&nbsp;10000&nbsp;次调用</code></li>
</ul>
<div><div>Related Topics</div><div><li></li><li>设计</li></div></div>\n<div><li>👍 212</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long