File tree Expand file tree Collapse file tree
src/main/java/com/chen/algorithm/znn Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -108,6 +108,20 @@ stack/queue
108108239. 滑动窗口最大值 *** ???
109109496. 下一个更大元素 I ***
110110
111+ linkedlist
112+ 2. 两数相加 ***
113+ 21. 合并两个有序链表 ***
114+ 19. 删除链表的倒数第N个节点 ***
115+ 83. 删除排序链表中的重复元素 ***
116+ 24. 两两交换链表中的节点 ***
117+ 206. 反转链表 *******
118+ 92. 反转链表 II
119+ 25 K 个一组翻转链表 ???
120+ 160 相交链表 ***
121+ 141 环形链表 ***
122+ 142 环形链表 II ***
123+ //148. 排序链表
124+
111125tree
11212694. 二叉树的中序遍历
113127144. 二叉树的前序遍历 *****
@@ -166,7 +180,7 @@ dynamic
16618070. 爬楼梯 ***
167181120. 三角形最小路径和 ***
16818264. 最小路径和 ***
169- 121、122、123、188、309、714 股票买卖系列 ***
183+ 121、( 122) 、123、188、309、714 股票买卖系列 ***
17018453. 最大子序和 ***
171185152. 乘积最大子数组***
172186300. 最长上升子序列 ***
Original file line number Diff line number Diff line change 55import java .util .Stack ;
66
77/**
8+ * https://leetcode-cn.com/problems/basic-calculator-ii/solution/chai-jie-fu-za-wen-ti-shi-xian-yi-ge-wan-zheng-ji-/
89 * 227. 基本计算器 II
910 * 实现一个基本的计算器来计算一个简单的字符串表达式的值。
1011 * 字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 。 整数除法仅保留整数部分。
@@ -39,15 +40,15 @@ public int calculate(String s) {
3940 }
4041
4142 Stack <Integer > stack = new Stack <>();
42- char sign = '+' ;
43- int num = 0 ;
43+ char sign = '+' ; // 记录 num 前的符号,初始化为 +
44+ int num = 0 ; // 记录算式中的数字
4445 for (int i = 0 ; i < s .length (); i ++) {
4546 char c = s .charAt (i );
4647 if (Character .isDigit (c )) {
4748 num = num * 10 + (c - '0' );
4849 }
4950
50- if ((!Character .isDigit (c ) && c != ' ' ) || i == s .length () - 1 ) {
51+ if ((!Character .isDigit (c ) && c != ' ' ) || i == s .length () - 1 ) { //不只是遇到新的符号会触发入栈,当i走到了算式的尽头(i == s.size() - 1),也应该将前面的数字入栈,方便后续计算最终结果。
5152 int pre ;
5253 if (sign == '+' ) {
5354 stack .push (num );
@@ -77,4 +78,5 @@ public void test() {
7778 System .out .println (calculate ("60+20-30+5/2 " ));
7879 }
7980
81+
8082}
Original file line number Diff line number Diff line change 77import java .util .LinkedList ;
88
99/**
10+ * https://leetcode-cn.com/problems/sliding-window-maximum/solution/shuang-xiang-dui-lie-jie-jue-hua-dong-chuang-kou-2/
1011 * 239. 滑动窗口最大值
1112 * 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
1213 * 返回滑动窗口中的最大值。
@@ -40,9 +41,7 @@ public class Solution {
4041 * @return
4142 */
4243 public int [] maxSlidingWindow (int [] nums , int k ) {
43- int n = nums .length ;
44- if (n * k == 0 ) return new int [0 ];
45- if (k == 1 ) return nums ;
44+ if (nums == null || nums .length < 2 ) return nums ;
4645
4746 int [] res = new int [nums .length - k + 1 ];
4847 // 递减队列
@@ -70,7 +69,7 @@ public int[] maxSlidingWindow(int[] nums, int k) {
7069 }
7170
7271 /**
73- * 采用双端队列存储数组下标
72+ * 采用双端队列存储数组下标 (推荐)
7473 * https://leetcode-cn.com/problems/sliding-window-maximum/solution/shuang-xiang-dui-lie-jie-jue-hua-dong-chuang-kou-2/
7574 *
7675 * @param nums
You can’t perform that action at this time.
0 commit comments