Code Monkey home page Code Monkey logo

Comments (2)

bazinga-web avatar bazinga-web commented on September 16, 2024

解题思路同200题岛屿的数量相同,都是采用沉没法

/**
 * @param {character[][]} board
 * @return {number}
 */
var countBattleships = function(board) {
    let result = 0;
    for (let row = 0; row < board.length; row++) {
        for (let col = 0; col < board[0].length; col++) {
            if (board[row][col] === 'X') {
                result++;
                dfs(row, col);
            }
        }
    }

    function dfs(row, col) {
        if (row < 0 || row >= board.length || col < 0 || col >= board[0].length || board[row][col] === '.') return;
        board[row][col] = '.';
        dfs(row-1, col);
        dfs(row+1, col);
        dfs(row, col-1);
        dfs(row, col+1);
    }

    return result;
};

from like-algorithms.

Ray-56 avatar Ray-56 commented on September 16, 2024

此题与200. 岛屿数量一致

/**
 * @param {character[][]} board
 * @return {number}
 */
var countBattleships = function(board) {
	const xLen = board.length;
	const yLen = board[0].length;
	let ret = 0;
	for (let x = 0; x < xLen; x++) {
		for (let y = 0; y < yLen; y++) {
			if (board[x][y] === 'X') {
				ret++;
				h(board, x, y, xLen, yLen);
			}
		}
	}
	return ret;
};
function h(board, x, y, xLen, yLen) {
	if (x < 0 || y < 0 || x >= xLen || y >= yLen || board[x][y] === '.') return;
	board[x][y] = '.';
	h(board, x - 1, y, xLen, yLen);
	h(board, x + 1, y, xLen, yLen);
	h(board, x, y - 1, xLen, yLen);
	h(board, x, y + 1, xLen, yLen);
}

from like-algorithms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.