419. Battleships in a Board

Unity2019编译器下使用Ump无错误,打包exe不播放 或 将exe文件在其他电脑下运行不播放 一、unity编译器下使用UMP可以播放视频,打包后查看debug 出现错误 UnloadTime: 1.070900 ms Fallback handler could not load library F:/PCTest/AccessControlSysteam/jfmx_Data/Mono/libdl Fallback handler could not load library F:/PCTest/AccessControlSysteam/jfmx_Data/Mono/libdl.dll Fa. 阅读详情

Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:

  • You receive a valid board, made of only battleships or empty slots.
  • Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) orNx1 (N rows, 1 column), where N can be of any size.
  • At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.

Example:

X..X
...X
...X
In the above board there are 2 battleships.

Invalid Example:

...X
XXXX
...X
This is an invalid board that you will not receive - as battleships will always have a cell separating between them.

Follow up:

Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?


解法1:

规定了舰队的形式只能为一行或者一列若干个X,而且边界不相交,那么舰队要么是单个的X,要么是“条状”的X

可以利用“条状”的X的性质,即处于条状两端的X只有一个邻居是X,每两个这样的端点组成一对,代表一个舰队,另外加上单独的X,就是舰队的总数,空间O(1),one pass


public class Solution {
    public int countBattleships(char[][] board)
	{
		int m=board.length;
		if(m<1)
			return 0;
		int n=board[0].length;
		int count_single=0;
		int count_block=0;
		int[] xoff={1,-1,0,0};
		int[] yoff={0,0,1,-1};
		
		for(int i=0;i<m;i++)
			for(int j=0;j<n;j++)
			{
				char c=board[i][j];
				if(c=='X')
				{
					int cnt=0;
					for(int k=0;k<4;k++)
					{
						int xx=i+xoff[k];
						int yy=j+yoff[k];
						if(xx>=0&&xx<m&&yy>=0&&yy<n)
							if(board[xx][yy]=='X')
								cnt++;
					}
					if(cnt==0)
						count_single++;
					else if(cnt==1)
						count_block++;
				}
			}
		
		return count_single+count_block/2; 
	}
}


-----------------------------------------------------------------------

解法2:

https://discuss.leetcode.com/topic/62970/simple-java-solution

Going over all cells, we can count only those that are the "first" cell of the battleship. First cell will be defined as the most top-left cell. We can check for first cells by only counting cells that do not have an 'X' to the left and do not have an 'X' above them.


    public int countBattleships(char[][] board) {
        int m = board.length;
        if (m==0) return 0;
        int n = board[0].length;
        
        int count=0;
        
        for (int i=0; i<m; i++) {
            for (int j=0; j<n; j++) {
                if (board[i][j] == '.') continue;
                if (i > 0 && board[i-1][j] == 'X') continue;
                if (j > 0 && board[i][j-1] == 'X') continue;
                count++;
            }
        }
        
        return count;
    }

Android ------ 开源的Modnet算法实现抠图和更换背景 Portrait Matting (sight-x.cn) 飞桨PaddlePaddle-源于产业实践的开源深度学习平台 GitHub - PaddlePaddle/PaddleSeg at develop 阅读详情

相关推荐

保姆级教程:Synology NAS共享文件夹从创建到权限设置的完整流程

本文提供了一份详尽的Synology NAS共享文件夹创建与权限设置指南。重点讲解了如何将NAS接入企业AD域,并利用域管理员账户进行集中身份验证和权限管理。文章从基础概念、域环境配置、文件夹创建、精细化权限分配到客户端访问与维护,完整覆盖了构建安全高效企业级文件共享空间的全流程,旨在帮助管理员实现便捷的团队协作与数据安全管理。

u9v0w的博客 556

leetcode 419. Battleships in a Board

1.题目Given an 2D board, count how many battleships are in it. The battleships are represented with ‘X’s, empty slots are represented with ‘.’s. You may assume the following rules:You receive a valid boa

沐沐余风的专栏 384

ubuntu安装FSL

1.下载fslinstaller.py 官网下载地址:https://fsl.fmrib.ox.ac.uk/fsldownloads_registration 或者去我的百度网盘取: 链接:https://pan.baidu.com/s/1d5eHm48oamFdYdKm84nifw 提取码:6ff9 2.将base环境改成python2.0版本 本来想在服务器安装,服务器没有sudo权限,网上很多教程都装不了,于是就自己安装虚拟机再装了一个ubuntu,然后安装了python2环境,再开始装fsl,fsl

qq_36421001的博客 4098

Leetcode题解:419. Battleships in a Board

Leetcode题解:419. Battleships in a Board难度:Medium

Littlecuter的博客 493

Java开发者的福音:Aspose 25.10全系列(Cells/Words/PDF/Slides)最新版破解与集成保姆级教程

本文为Java开发者提供了Aspose 25.10全系列组件(Cells/Words/PDF/Slides)的最新版破解与集成保姆级教程。通过详细的Maven依赖配置、字节码修改技术绕过限制,以及Spring Boot项目中的实战集成指南,帮助开发者高效实现Office文档处理功能,提升开发效率。

weixin_30919429的博客 346

别再只盯着MES了!半导体/面板厂CIM系统全家桶(EAP/YMS/SPC)保姆级入门指南

本文深入解析半导体制造中的CIM系统,涵盖EAP、YMS和SPC等关键子系统的协同工作。通过实战案例和技术细节,展示如何利用CIM系统提升生产效率、良率管理和设备维护,为半导体/面板厂提供全面的实施指南和优化策略。

weixin_33693070的博客 464

【leetcode】419. Battleships in a Board(C++ & Python)

419. Battleships in a Board题目链接419.1 题目描述:Given an 2D board, count how many battleships are in it. The battleships are represented with ‘X’s, empty slots are represented with ‘.’s. You may assume the f

I am what i am 607

FastApi教程

HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于万维网(WWW:World Wide Web )服务器与本地浏览器之间传输超文本的传送协议。HTTP是一个属于应用层的面向对象的协议,由于其简捷、快速的方式,适用于分布式超媒体信息系统。它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展。HTTP协议工作于客户端-服务端架构为上。浏览器作为HTTP客户端通过URL向HTTP服务端即WEB服务器发送所有请求。

小石潭记丶 1562

Leetcode 419. Battleships in a Board解题报告(python)

419. Battleships in a Board Battleships in a Board python solution 题目描述 Given an 2D board, count how many battleships are in it. The battleships are represented with 'X’s, empty slots are represente...

Orientliu96的博客 517

web渗透学习手册

依据多年的渗透测试项目经验及个人理解,对漏洞分类

渗透之路,攻防之间皆学问 1万+

开发simulink 以及stm32半物理仿真碰到的问题,以及处理方法,Error:External Mode Open Protocol Connect command failed(实测有用)

串口实现的,但是在测试的时候发现,这个串口可以正常发送和接收数据,但是一旦把这个串口只让其进行版物理仿真就报错。直接刷写程序是可以的,最后查到问题了。我要实现半物理仿真,就是通过matlab实现可以直接控制stm32板子,实际上是通过xcp on。然后点击simulink上的观测按钮就可以直接和stm32平台进行通讯了。3,还是使用串口3,但是重新需要设置一下com口,因为重新插拔了一下,按照这个设置方法就可以。2,重新加载ioc文件。

pvmsmfchcs的博客 1500

[LeetCode] 419. Battleships in a Board 解题报告

Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with'.'s. You may assume the following rules: You receive a valid b

萌萌小七的专栏 654

一篇文章带你搞定数学建模里的主成分分析(05年水质问题示例应用讲解含代码)

文章目录一、题目分析二、原理公式三、MATLAB实现某一地区的主成分分析四、MATLAB 实现所有地区28个月的综合分析五、注意 一、题目分析 对长江近两年多的水质情况做出定量的综合评价,并分析各地区水质的污染状况 考虑到各指标之间可能存在一定的相关性,选用主成分分析法对长江流域主要城市水质检测报告进行分析,选取主成分,并把主成分得分按照方差贡献率加权求和,得到每个地区的污染综合评价指数,进而可以计算每个月长江流域的污染综合评价指数,对平均综合评价指数进行排序 二、原理公式 根据主成分分析的方法,分析长江沿

南淮北安的博客 2625

【LeetCode】419.Battleships in a Board(Medium)解题报告

【LeetCode】419.Battleships in a Board解题报告(Medium)题目地址:https://leetcode.com/problems/battleships-in-a-board/description/ 题目描述:   Given an 2D board, count how many battleships are in it. The battleship

郝春雨的博客 212

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode 419. Battleships in a Board

Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: You receiv

DennisDing的专栏 513

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

[LeetCode]419. Battleships in a Board

[LeetCode]419. Battleships in a Board题目描述思路题目中的合法输入,是必须有间隔,不能连续,可以理解为一个连通域问题,用广搜求解,但是不满足题目中的空间复杂度O(1)的要求 奇技淫巧:和合法输入有关 能作为一个合法输入的起点,那么他只能是顶点,或者这个点的左边一个点和上边一个点为空,具体代码如下BFS和GFS的解法留坑,明天补代码class Solution

Lcharon的博客 3163

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

LeetCode[419] Battleships in a Board

Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: You receiv

wrsophia_977的博客 981

LeetCode 419. Battleships in a Board 解题报告

LeetCode 419. Battleships in a Board 解题报告

骆小坑的填坑之旅 5586

LeetCode //C - 419. Battleships in a Board

【代码】LeetCode //C - 419. Battleships in a Board。

Made in Code 1095

leetcode 419 Battleships in a Board C++

好久不刷leetcode了,因为找了个还比较满意的实习,今天突然上来发现C++都不会写了。。。还是要保持手感呐。 这道题第一思路就是DFS,然而它给了两个要求,一个是不能改变board,第二是只用O(1)的额外空间。 先说下修改board的吧。 找到一个X之后用DFS把周围的全部变成‘.’就可以了。 符合条件的解法是找到X且上边和右边都没有X,很巧妙。 代码如下,注意边界,不然就cras

wcxdell的专栏 815

[leetcode] 419. Battleships in a Board @ python

原题 Given an 2D board, count how many battleships are in it. The battleships are represented with 'X’s, empty slots are represented with '.'s. You may assume the following rules: You receive a valid bo...

闲庭信步 216

Leetcode 419 Battleships in a Board

Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: You receive a valid

夜色之浓,莫过于黎明前的黑暗。 270
上一篇: 417. Pacific Atlantic Water Flow
下一篇: 420. Strong Password Checker
wdlsjdl2
博客等级 码龄15年 4粉丝 246原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值