注意的点:
以下是代码部分:
//单调栈public int[] dailyTemperatures(int[] temperatures) {Stack s = new Stack<>();int[] result = new int[temperatures.length];//思路:在栈中维持一个递减的序列(从栈底到栈顶)for (int i = 0; i < temperatures.length; i++) {//要一直看栈顶元素是否小于当前入栈元素,所以使用 while 而不是 if//踩坑:temperatures[i] > s.peek() ——> 这里不是>s.peek(),而是>temp[s.peek()]while (!s.isEmpty() && temperatures[i] > temperatures[s.peek()]){//这里要减s.peek,因为是求之间的距离result[s.peek()] = i - s.peek();s.pop();}//同时,还要进行一个入栈操作s.push(i);}return result;}
注意的点:
以下是代码部分:
public class 下一个更大元素I496 {//暴力循环破解public int[] nextGreaterElement(int[] nums1, int[] nums2) {int[] result = new int[nums1.length];for (int i = 0; i < nums1.length; i++) {for (int j = 0; j < nums2.length; j++) {while (j < nums2.length && nums1[i] != nums2[j])j++;if(j == nums2.length)break;//这里迭代寻找第一个大于该值的数while (j < nums2.length && nums1[i] >= nums2[j]){j++;}if(j == nums2.length)break;//如果符合条件,即找到一个大的值,则返回该值if(nums1[i] < nums2[j]) {result[i] = nums2[j];break;}}//判断如果result为0,即没有被赋值,则置为-1if(result[i] == 0)result[i] = -1;}return result;}//单调栈方式//思路:其实就是在nums2中找右边第一个最大值,然后再看nums2中的元素是否在nums1中也存在public int[] nextGreaterElement2(int[] nums1, int[] nums2) {int[] result = new int[nums1.length];//要先全部初始化为-1for (int i = 0; i < result.length; i++) {result[i] = -1;}Map map = new HashMap<>();//这里map的value对应的是下标,这样好在result中进行对应的赋值for (int i = 0; i < result.length; i++) {map.put(nums1[i], i);}Stack s = new Stack<>();//对nums2进行右边最大值的查找for (int i = 0; i < nums2.length; i++) {while (!s.isEmpty() && nums2[s.peek()] < nums2[i]){//这里要进行查找、并赋值if(map.containsKey(nums2[s.peek()]))result[map.get(nums2[s.peek()])] = nums2[i];//弹出栈顶元素s.pop();}//入栈s.push(i);}return result;}
}