• 1351. 统计有序矩阵中的负数


    地址:https://leetcode-cn.com/problems/count-negative-numbers-in-a-sorted-matrix

    <?php
    /**
    给你一个 m * n 的矩阵 grid,矩阵中的元素无论是按行还是按列,都以非递增顺序排列。 
    
    请你统计并返回 grid 中 负数 的数目。
    
     
    
    示例 1:
    
    输入:grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
    输出:8
    解释:矩阵中共有 8 个负数。
    示例 2:
    
    输入:grid = [[3,2],[1,0]]
    输出:0
    示例 3:
    
    输入:grid = [[1,-1],[-1,-1]]
    输出:3
    示例 4:
    
    输入:grid = [[-1]]
    输出:1
     
    
    提示:
    
    m == grid.length
    n == grid[i].length
    1 <= m, n <= 100
    -100 <= grid[i][j] <= 100
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/count-negative-numbers-in-a-sorted-matrix
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
     */
    
    class Solution {
    
        /**
         * @param Integer[][] $grid
         * @return Integer
         */
        function countNegatives($grid) {
            $m = count($grid);
            $n = count($grid[0]);
            $i = 0;
            $j = $n - 1;
            $count = 0;
            while($i < $m && $j >= -1){
                while($j >= 0 && $grid[$i][$j] < 0) $j--;
                $count += $n - 1 - $j;
                $i++;
            }
            return $count;
        }
    }
    
    $solution = new Solution();
    $grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]];
    $grid =  [[3,2],[1,0]];
    var_dump($solution->countNegatives($grid));
  • 相关阅读:
    Expanding Rods(二分)
    Monthly Expense(二分)
    sdut1269 走迷宫(dfs)
    走迷宫(dfs)
    C Looooops(扩展欧几里得+模线性方程)
    41. First Missing Positive
    40. Combination Sum II
    39. Combination Sum
    37. Sudoku Solver
    36. Valid Sudoku
  • 原文地址:https://www.cnblogs.com/8013-cmf/p/13877435.html
Copyright © 2020-2023  润新知