那个题二分比较麻烦,我觉得当做二叉搜索树来做也不错,若当前结点不是顶峰,转移到四周较大的位置即可,我的代码过了 ```C++ class Solution { vector<int> direction = {-1, 0, 1, 0, -1}; public: vector<int> findPeakGrid(vector<vector<int>>&; mat) { // log的时间复杂度,二分? // 当做一个二叉搜索树来做,直接向大元素的方向移动 int i = 0, j = 0; int m = mat.size(), n = mat[0].size(); while(i < m &;&; j < n) { if(check(mat, i, j, m, n)) { return {i, j}; } for(int t = 0; t < 4; ++t) { int x = i + direction[t]; int y = j + direction[t + 1]; if(x >= 0 &;&; x < m &;&; y >= 0 &;&; y < n &;&; mat[x][y] > mat[i][j]) { i = x; j = y; break; } } } return {}; } ``` 还有一点我下面发一次吧,字数超了
点赞

相关推荐

牛客网
牛客企业服务