题解 | #单调栈#

单调栈

https://www.nowcoder.com/practice/ae25fb47d34144a08a0f8ff67e8e7fb5

using System;
using System.Collections.Generic;


class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param nums int整型一维数组
     * @return int整型二维数组
     */
    public List<List<int>> foundMonotoneStack (List<int> nums) {
        // write code here
        if (nums == null || nums.Count == 0)
            return null;

        List<List<int>> lslsN = new List<List<int>>();
        for (int nIndex = 0; nIndex < nums.Count; nIndex++)
            lslsN.Add(FM(nums, nIndex));
        return lslsN;
    }

    public List<int> FM(List<int> nums, int nCurIndex) {
        // write code here
        List<int> lsN = new List<int>();
        for (int nIndex = nCurIndex - 1; nIndex >= 0; nIndex--) {
            if (nums[nIndex] >= nums[nCurIndex])
                continue;
            lsN.Add(nIndex);
            break;
        }
        if (lsN.Count == 0)
            lsN.Add(-1);
        for (int nIndex = nCurIndex + 1; nIndex < nums.Count; nIndex++) {
            if (nums[nIndex] >= nums[nCurIndex])
                continue;
            lsN.Add(nIndex);
            break;
        }
        if (lsN.Count == 1)
            lsN.Add(-1);
        return lsN;
    }
}

全部评论

相关推荐

01-04 07:53
门头沟学院 C++
心愿便利贴:工作了以后回头再看待这个问题,从客观的视角来讲是因为每个人对自己的要求不同,学习好的人对自己的要求很高,所以觉得考不好就天塌了,认为自己学习好并且值得一份好工作的人也是一样,找不到符合自己预期的工作肯定也会觉得是侮辱,牛客上有很多名校大学生,肯定会存在这种好学生心态啊,“做题区”从来都不是贬义词,这是大部分普通人赖以生存的路径,这个有什么好嘲讽的,有“好学生心态”没有错,但是不要给自己太大的压力了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务