单调栈
每周一句:期待三分春意,更期待满分的自己
3.1.商品折扣后的最终价格
-
给你一个数组
prices,其中prices[i]是商店里第i件商品的价格。商店里正在进行促销活动,如果你要买第i件商品,那么你可以得到与prices[j]相等的折扣,其中j是满足j > i且prices[j] <= prices[i]的 最小下标 ,如果没有满足条件的j,你将没有任何折扣。请你返回一个数组,数组中第i个元素是折扣后你购买商品i最终需要支付的价格。 -
示例
示例 1: 输入:prices = [8,4,6,2,3] 输出:[4,2,4,2,3] 解释: 商品 0 的价格为 price[0]=8 ,你将得到 prices[1]=4 的折扣,所以最终价格为 8 - 4 = 4 。 商品 1 的价格为 price[1]=4 ,你将得到 prices[3]=2 的折扣,所以最终价格为 4 - 2 = 2 。 商品 2 的价格为 price[2]=6 ,你将得到 prices[3]=2 的折扣,所以最终价格为 6 - 2 = 4 。 商品 3 和 4 都没有折扣。 示例 2: 输入:prices = [1,2,3,4,5] 输出:[1,2,3,4,5] 解释:在这个例子中,所有商品都没有折扣。 示例 3: 输入:prices = [10,1,1,6] 输出:[9,0,1,6] -
思路
-
新建一个栈作为中介,因为栈内只存递增或递减的数值
-
我们把数组的下标存入栈中,首先将0放入栈上
-
有三种情况:
- 栈中索引大于遍历到的数组:最终价格为栈索引的值 - 遍历的值
- 栈中索引小于遍历到的数组:将索引push到栈中
- 栈中索引等于遍历到的数组:最终价格为栈索引的值 - 遍历的值
-
-
代码实现
class Solution { public int[] finalPrices(int[] prices) { int[] result = new int[prices.length]; Deque<Integer> stack = new LinkedList<>(); stack.push(0); for(int i = 1; i < prices.length; i++){ if(prices[stack.peek()] < prices[i]){ stack.push(i); } while(!stack.isEmpty() && prices[stack.peek()] >= prices[i]){ result[stack.peek()] = prices[stack.peek()] - prices[i]; stack.pop(); } stack.push(i); } while(!stack.isEmpty()){ result[stack.peek()] = prices[stack.peek()]; stack.pop(); } return result; } }
3.3.每日温度
-
给定一个整数数组
temperatures,表示每天的温度,返回一个数组answer,其中answer[i]是指对于第i天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用0来代替。 -
示例:
示例 1: 输入: temperatures = [73,74,75,71,69,72,76,73] 输出: [1,1,4,2,1,1,0,0] 示例 2: 输入: temperatures = [30,40,50,60] 输出: [1,1,1,0] 示例 3: 输入: temperatures = [30,60,90] 输出: [1,1,0] -
思路:
-
新建一个栈作为中介,因为栈内只存递增或递减的数值
-
我们把数组的下标存入栈中,首先将0放入栈上
-
有三种情况:
- 栈中索引大于遍历到的数组:将索引push到栈中
- 栈中索引小于遍历到的数组:最终价格为栈索引 - 遍历的索引
- 栈中索引等于遍历到的数组:最终价格为栈索引 - 遍历的索引
-
-
代码实现:
class Solution { public int[] dailyTemperatures(int[] temperatures) { int[] result = new int[temperatures.length]; Deque<Integer> stack = new LinkedList<>(); stack.push(0); for(int i = 1; i < temperatures.length; i++){ if(temperatures[stack.peek()] > temperatures[i]){ stack.push(i); } while(!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]){ result[stack.peek()] = i - stack.peek(); stack.pop(); } stack.push(i); } while(!stack.isEmpty()){ result[stack.peek()] = 0; stack.pop(); } return result; } }
双指针
4.1有序数组的平方
-
给你一个按 非递减顺序 排序的整数数组
nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。 -
示例:
示例 1: 输入:nums = [-4,-1,0,3,10] 输出:[0,1,9,16,100] 解释:平方后,数组变为 [16,1,0,9,100] 排序后,数组变为 [0,1,9,16,100] 示例 2: 输入:nums = [-7,-3,2,3,11] 输出:[4,9,9,49,121] -
思路:
-
创建一个数组用来接收
-
因为是非递减的数组,负数的平方可能比右边大所以插入新数组的顺序从数组最后开始
-
首先右指针平方和左指针对比
- 右大于左:将右放入数组,数组index--;右--;
- 左大于左:将左放入数组,数组index--;左++;
-
-
代码实现:
class Solution { public int[] sortedSquares(int[] nums) { int[] reslut = new int[nums.length]; int slow = 0; int fast = nums.length - 1; int index = nums.length - 1; while(slow <= fast){ if(nums[slow] * nums[slow] < nums[fast] * nums[fast]){ reslut[index] = nums[fast] * nums[fast]; fast--; index--; }else{ reslut[index] = nums[slow] * nums[slow]; slow++; index--; } } return reslut; } }
4.2.长度最小的子数组
-
给定一个含有
n个正整数的数组和一个正整数target。 找出该数组中满足其总和大于等于target的长度最小的 子数组[numsl, numsl+1, ..., numsr-1, numsr],并返回其长度 。 如果不存在符合条件的子数组,返回0。 -
示例
示例 1: 输入:target = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数组 [4,3] 是该条件下的长度最小的子数组。 示例 2: 输入:target = 4, nums = [1,4,4] 输出:1 示例 3: 输入:target = 11, nums = [1,1,1,1,1,1,1,1] 输出:0 -
思路
- 滑动窗口,用一个for循环for循环的索引为快指针,新建一个慢指针
- 首先保存个sum 首先直接给sum赋值为慢指针的值,并且新建一个返回值为Int最大值
- 如果sum小于target:快指针++
- 如果sum大于等于target:判断返回的值 与 快慢指针相减 取最小值,并且把慢指针++,并且更新sum的值(减去慢指针对应的值)
- 最后返回res的最小值;
-
代码
class Solution { public int minSubArrayLen(int target, int[] nums) { int res = Integer.MAX_VALUE; int i = 0; int sum = 0; for(int j = 0; j < nums.length; j++){ sum += nums[j]; while(sum >= target){ res = Math.min(res, (j - i + 1)); sum -= nums[i++]; } } return res == Integer.MAX_VALUE ? 0 : res; } }
5.1.移除链表元素
-
给你一个链表的头节点
head和一个整数val,请你删除链表中所有满足Node.val == val的节点,并返回 新的头节点 。 -
示例:
示例 1: 输入:head = [1,2,6,3,4,5,6], val = 6 输出:[1,2,3,4,5] 示例 2: 输入:head = [], val = 1 输出:[] 示例 3: 输入:head = [7,7,7,7], val = 7 输出:[] -
思路:
- 创建一个虚拟头节点
- 执行头节点,创建一个指针进行删除操作
-
代码
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummyHead = new ListNode(-1); dummyHead.next = head; ListNode cur = dummyHead; while(cur.next != null){ if(cur.next.val == val){ cur.next = cur.next.next; }else{ cur = cur.next; } } return dummyHead.next; } }
5.2.设计链表
-
你可以选择使用单链表或者双链表,设计并实现自己的链表。
单链表中的节点应该具备两个属性:
val和next。val是当前节点的值,next是指向下一个节点的指针/引用。如果是双向链表,则还需要属性
prev以指示链表中的上一个节点。假设链表中的所有节点下标从 0 开始。实现
MyLinkedList类:MyLinkedList()初始化MyLinkedList对象。int get(int index)获取链表中下标为index的节点的值。如果下标无效,则返回-1。void addAtHead(int val)将一个值为val的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。void addAtTail(int val)将一个值为val的节点追加到链表中作为链表的最后一个元素。void addAtIndex(int index, int val)将一个值为val的节点插入到链表中下标为index的节点之前。如果index等于链表的长度,那么该节点会被追加到链表的末尾。如果index比长度更大,该节点将 不会插入 到链表中。void deleteAtIndex(int index)如果下标有效,则删除链表中下标为index的节点。
-
示例
示例: 输入 ["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"] [[], [1], [3], [1, 2], [1], [1], [1]] 输出 [null, null, null, null, 2, null, 3] 解释 MyLinkedList myLinkedList = new MyLinkedList(); myLinkedList.addAtHead(1); myLinkedList.addAtTail(3); myLinkedList.addAtIndex(1, 2); // 链表变为 1->2->3 myLinkedList.get(1); // 返回 2 myLinkedList.deleteAtIndex(1); // 现在,链表变为 1->3 myLinkedList.get(1); // 返回 3 -
思路:最重要的就是虚拟头节点
-
代码:
class ListNode{ int val; ListNode next; ListNode(){}; ListNode(int val){ this.val = val; } } class MyLinkedList { int size; ListNode head; public MyLinkedList() { size = 0; head = new ListNode(0); } public int get(int index) { if(index < 0 || index > size - 1){ return -1; } ListNode cur = head; for(int i = 0; i <= index; i++){ cur = cur.next; } return cur.val; } public void addAtHead(int val) { ListNode newNode = new ListNode(val); newNode.next = head.next; head.next = newNode; size++; } public void addAtTail(int val) { ListNode tailNode = new ListNode(val); ListNode cur = head; while(cur.next != null){ cur = cur.next; } cur.next = tailNode; size++; } public void addAtIndex(int index, int val) { if (index > size) { return; } if (index < 0) { index = 0; } size++; ListNode cur = head; ListNode newNode = new ListNode(val); for(int i = 0; i < index; i++){ cur = cur.next; } newNode.next = cur.next; cur.next = newNode; } public void deleteAtIndex(int index) { if(index < 0 || index > size - 1){ return; } size--; ListNode cur = head; for(int i = 0; i < index; i++){ cur = cur.next; } cur.next = cur.next.next; } } /** * Your MyLinkedList object will be instantiated and called as such: * MyLinkedList obj = new MyLinkedList(); * int param_1 = obj.get(index); * obj.addAtHead(val); * obj.addAtTail(val); * obj.addAtIndex(index,val); * obj.deleteAtIndex(index); */