Leetcode 503. Next Greater Element II JAVA语言

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn‘t exist, output -1 for this number.
Example 1:
Input: [1,2,1]Output: [2,-1,2]Explanation: The first 1‘s next greater number is 2; 
The number 2 can‘t find next greater number; 
The second 1‘s next greater number needs to search circularly, which is also 2.
Note: The length of given array won‘t exceed 10000.

题意:给一个循环数组,求解其next greater number:第一个比其大的数。

public class Solution {
    ////这个思路是直接double数组,把原来的循环简化了。貌似还有用栈的。再看看。
    public int[] nextGreaterElements(int[] nums) {
        if(nums.length==1){
            nums[0]=-1;
            return nums;
        }
        int[] newnums=new int[nums.length*2];
        for(int i=0;i<newnums.length;i++){
            newnums[i]=nums[i%nums.length];
        }
        // for(int i=0;i<nums.length;i++){
        //     newnums[i]=nums[i];
        // }
        // int index=0;
        // for(int i=nums.length;i<newnums.length;i++){
        //     newnums[i]=nums[index++];
        // }
        // for(int i=0;i<nums.length*2;i++){
        //     System.out.println(newnums[i]+"  ");
        // }
        for(int i=0;i<nums.length;i++)
            for(int j=i+1;j<i+nums.length;j++)
            if(newnums[j]>newnums[i]){
                nums[i]=newnums[j];
                break;
            }else{
                nums[i]=-1;
            }
        return nums;
    }
}

PS:听群里大神说把数组直接double一下,然后就可以简化了,【好厉害】。然后就是暴力搜索了。。。。。。。。貌似还有栈!

栈的话,若当前元素小于等于栈顶元素,直接入栈。若大于栈顶元素,即将所有小于该元素的值出站,并作为=他们的next greater number。再来一次循环,只出栈,看看能不能找到比他大的元素。最后看看栈里的元素就是最大值,直接设为-1

时间: 2024-08-25 06:28:15

Leetcode 503. Next Greater Element II JAVA语言的相关文章

[栈] leetcode 503 Next Greater Element II

problem:https://leetcode.com/problems/next-greater-element-ii/ 一道比较简单的单调队列题目.不过由于题目要求是循环的,需要两个pass,第二个pass处理循环生效的next greater,同时需要把下标已经超出范围的队首数据及时pop出来. class Solution { public: vector<int> nextGreaterElements(vector<int>& nums) { deque<

503. Next Greater Element II

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the ar

503. Next Greater Element II 下一个更大元素

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the ar

leetcode496 - Next Greater Element I - easy &amp;&amp; leetcode503 - Next Greater Element II - medium

496. Next Greater Element IYou are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.The Next Greater Number o

leetcode 119 Pascal&#39;s Triangle II ----- java

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 上一道题的延伸版,就是直接求出第k行的数,要求用o(k)的空间复杂度. 也是直接相加就可以了. public class Solution { pub

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

【LeetCode】229. Majority Element II

Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint

Leetcode 122. Best Time to Buy and Sell Stock II JAVA语言

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho

Leetcode 350. Intersection of Two Arrays II JAVA语言

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any or