C# Solution (Next Greater Element)


  • 0
    A
     public int[] NextGreaterElement(int[] findNums, int[] nums) {
            int[] nextGreater = new int[findNums.Length];
    
                for(int i=0; i<findNums.Length;i++)
                {
                    bool greaterNoFound = false;
                    int matchIndex = 0;
                    for (int j=0; j<nums.Length; j++)
                    {
                        if (findNums[i] == nums[j])
                        {
                            matchIndex = j;
                            break;
                        }
                    } 
                    for(int j= matchIndex; j< nums.Length; j++)
                    {
                        if(findNums[i] < nums[j])
                        {
                            nextGreater[i] = nums[j];
                            greaterNoFound = true;
                            break;
                        }
                    }                
                    if (!greaterNoFound)
                    {
                        nextGreater[i] = -1;
                    }                
                }
                return nextGreater;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.