首页 > 其他分享 >[LeetCode] 531. Lonely Pixel I

[LeetCode] 531. Lonely Pixel I

时间:2022-10-04 14:00:15浏览次数:72  
标签:picture Lonely int pixels ++ length black 531 LeetCode

Given an m x n picture consisting of black 'B' and white 'W' pixels, return the number of black lonely pixels.

A black lonely pixel is a character 'B' that located at a specific position where the same row and same column don't have any other black pixels.

Example 1:

Input: picture = [["W","W","B"],["W","B","W"],["B","W","W"]]
Output: 3
Explanation: All the three 'B's are black lonely pixels.

Example 2:

Input: picture = [["B","B","B"],["B","B","W"],["B","B","B"]]
Output: 0

Constraints:

  • m == picture.length
  • n == picture[i].length
  • 1 <= m, n <= 500
  • picture[i][j] is 'W' or 'B'.

孤独像素。

这道题不难,我直接介绍思路。

首先我用两个数组分别记录 input 矩阵中每一行和每一列出现了多少个字母 B。之后我再次遍历 input 矩阵,当我再次遇到字母 B 的时候,我检查一下当前这个 B 是不是他所在行和所在列上唯一的一个 B。

时间O(mn)

空间O(m + n)

Java实现

 1 class Solution {
 2     public int findLonelyPixel(char[][] picture) {
 3         int m = picture.length;
 4         int n = picture[0].length;
 5         int[] rows = new int[m];
 6         int[] cols = new int[n];
 7         for (int i = 0; i < picture.length; i++) {
 8             for (int j = 0; j < picture[0].length; j++) {
 9                 if (picture[i][j] == 'B') {
10                     rows[i]++;
11                     cols[j]++;
12                 }
13             }
14         }
15         
16         int count = 0;
17         for (int i = 0; i < m; i++) {
18             for (int j = 0; j < n; j++) {
19                 if (picture[i][j] == 'B' && rows[i] == 1 && cols[j] == 1) {
20                     count++;
21                 }
22             }
23         }
24         return count;
25     }
26 }

 

LeetCode 题目总结

标签:picture,Lonely,int,pixels,++,length,black,531,LeetCode
From: https://www.cnblogs.com/cnoodle/p/16753671.html

相关文章

  • leetcode 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公
    一、题目大意给定一个二叉搜索树,找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树T的两个结点p、q,最近公共祖先表示为一个结点......
  • LeetCode 20. 有效的括号(超详细超容易理解的动画解法!!!)
    大家好,我是程序员吴师兄,今天跟大家分享一道和栈一、题目描述给定一个只包括'(',')','{','}','[',']'的字符串s,判断字符串是否有效。有效字符串需满足:左括号必须用相同类......
  • LeetCode92 反转链表
     idea:参考上一道全部反转,所以反转链表部分代码实现,我觉得重点在于集中不同情况的分类讨论。一共四类情况需要考虑,有前有后,有前无后,有后无前,无前无后。 /** * Def......
  • LeetCode238. Product of Array Except Self
    题意给一个数组,计算不包含nums[i]的数组的乘积,方法记录数组中零的数量和索引代码classSolution{public:vector<int>productExceptSelf(vector<int>&......
  • [Oracle] LeetCode 48 Rotate Image 思维
    Youaregivenannxn2Dmatrixrepresentinganimage,rotatetheimageby90degrees(clockwise).Youhavetorotatetheimagein-place,whichmeansyouhave......
  • day13:leetcode:150,239,347
    leetcode150.逆波兰表达式求值本题的思路和之前的括号配对,两数相消的思想一样,利用栈先进后出的特性,三个字符相消到遍历到的是数字时,直接push,当遇到运算符,直接pop出两个......
  • [Oracle] LeetCode 141 Linked List Cycle 判环
    Givenhead,theheadofalinkedlist,determineifthelinkedlisthasacycleinit.Thereisacycleinalinkedlistifthereissomenodeinthelistthat......
  • LeetCode 1367. Linked List in Binary Tree
    原题链接在这里:https://leetcode.com/problems/linked-list-in-binary-tree/题目:Givenabinarytree root anda linkedlistwith head asthefirstnode. Ret......
  • LeetCode 75 突破:环形链表 II
    LeetCode75学习计划适用于想为技术面试做准备但不确定应该聚焦于哪些题目的用户。学习计划中的题目都是经过精心挑选的,Level1和Level2学习计划是为初级用户和中级用户......
  • LeetCode 363. Max Sum of Rectangle No Larger Than K
    原题链接在这里:https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/题目:Givenan mxn matrix matrix andaninteger k,return themaxsum......