博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode419. 甲板上的战舰 | Battleships in a Board
阅读量:5238 次
发布时间:2019-06-14

本文共 3368 字,大约阅读时间需要 11 分钟。

原文地址: 

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 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) or Nx1 (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:

...XXXXX...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?


给定一个二维的甲板, 请计算其中有多少艘战舰。 战舰用 'X'表示,空位用 '.'表示。 你需要遵守以下规则:

  • 给你一个有效的甲板,仅由战舰或者空位组成。
  • 战舰只能水平或者垂直放置。换句话说,战舰只能由 1xN (1 行, N 列)组成,或者 Nx1 (N 行, 1 列)组成,其中N可以是任意大小。
  • 两艘战舰之间至少有一个水平或垂直的空位分隔 - 即没有相邻的战舰。

示例 :

X..X...X...X

在上面的甲板中有2艘战舰。

无效样例 :

...XXXXX...X

你不会收到这样的无效甲板 - 因为战舰之间至少会有一个空位将它们分开。

进阶:

你可以用一次扫描算法,只使用O(1)额外空间,并且不修改甲板的值来解决这个问题吗?


20ms

1 class Solution { 2     func countBattleships(_ board: [[Character]]) -> Int { 3          4         var battle = 0 5          6         for r in 0..
0 {14 cellLeft = String(board[r][c-1])15 }16 if r > 0{17 cellUp = String(board[r-1][c])18 }19 20 if cellUp == "." && cellLeft == "."{21 battle += 122 }23 }24 25 }26 }27 28 return battle29 }30 }

28ms

1 class Solution { 2     func countBattleships(_ board: [[Character]]) -> Int { 3         guard !board.isEmpty && !board[0].isEmpty else { 4             return 0 5         } 6  7         var board = board 8         let xChar = Character("X") 9         let dotChar = Character(".")10         var result = 011         for row in 0..

152ms

1 class Solution { 2     func countBattleships(_ board: [[Character]]) -> Int { 3         let row = board.count 4         guard row > 0 else { 5             return 0 6         } 7         let column = board[0].count 8         var num = 0 9         10         for i in 0..

172ms

1 class Solution { 2     func countBattleships(_ board: [[Character]]) -> Int { 3         var board = board 4         if board.isEmpty || board[0].isEmpty {
return 0} 5 var m:Int = board.count 6 var n:Int = board[0].count 7 var res:Int = 0 8 var visited:[[Bool]] = [[Bool]](repeating:[Bool](repeating:false,count:n),count:m) 9 for i in 0..
= m || j < 0 || j >= n || visited[i][j] || board[i][j] == "."33 {34 return35 }36 vertical |= i37 horizontal |= j38 visited[i][j] = true39 dfs(&board, &visited, &vertical, &horizontal, i - 1, j)40 dfs(&board, &visited, &vertical, &horizontal, i + 1, j)41 dfs(&board, &visited, &vertical, &horizontal, i, j - 1)42 dfs(&board, &visited, &vertical, &horizontal, i, j + 1)43 }44 }

 

转载于:https://www.cnblogs.com/strengthen/p/10333099.html

你可能感兴趣的文章
jQuery 1.7 发布了
查看>>
Python(软件目录结构规范)
查看>>
Windows多线程入门のCreateThread与_beginthreadex本质区别(转)
查看>>
Nginx配置文件(nginx.conf)配置详解1
查看>>
linux php编译安装
查看>>
name phone email正则表达式
查看>>
721. Accounts Merge
查看>>
「Unity」委托 将方法作为参数传递
查看>>
重置GNOME-TERMINAL
查看>>
redis哨兵集群、docker入门
查看>>
hihoCoder 1233 : Boxes(盒子)
查看>>
oracle中anyData数据类型的使用实例
查看>>
C++对vector里面的元素排序及取任意重叠区间
查看>>
软件测试——性能测试总结
查看>>
12.4站立会议
查看>>
Java Concurrentmodificationexception异常原因和解决方法
查看>>
客户端访问浏览器的流程
查看>>
codeforces水题100道 第二十二题 Codeforces Beta Round #89 (Div. 2) A. String Task (strings)
查看>>
c++||template
查看>>
[BZOJ 5323][Jxoi2018]游戏
查看>>