Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert javascript to java

const testMatrix = [
  [1, 1, 1, 0, 0],
  [1, 1, 1, 0, 1],
  [0, 1, 0, 0, 1],
  [0, 0, 0, 1, 1]
];

const directions = [
  [-1, 0], //up
  [0, 1], //right
  [1, 0], //down
  [0, -1] //left
]

const numberOfIslands = function(matrix) {
  if(matrix.length === 0) return 0;
  let islandCount = 0;

  for(let row = 0; row < matrix.length; row++) {
    for(let col = 0; col < matrix[0].length; col++) {
      if(matrix[row][col] === 1) {
        islandCount++;
        matrix[row][col] = 0;
        const queue = [];
        queue.push([row, col]);

        while(queue.length) {
          const currentPos = queue.shift();
          const currentRow = currentPos[0];
          const currentCol = currentPos[1];

          for(let i = 0; i < directions.length; i++) {
            const currentDir = directions[i];
            const nextRow = currentRow + currentDir[0];
            const nextCol = currentCol + currentDir[1];

            if(nextRow < 0 || nextRow >= matrix.length || nextCol < 0 || nextCol >= matrix[0].length) continue;

            if(matrix[nextRow][nextCol] === 1) {
              queue.push([nextRow, nextCol]);
              matrix[nextRow][nextCol] = 0;
            }
          }
        }
      }
    }
  }

  return islandCount;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: khai bao bien trong javascript 
Javascript :: VueJS - getting the last element of a split string array 
Javascript :: angular data table push not working 
Javascript :: links 
Javascript :: how to regexp replace with uppercase on specific capture group in js 
Javascript :: get json model from another component in sapui5 
Javascript :: React.createElement pass props 
Javascript :: Reading manifest: Warning processing Description: An unexpected property was found in the WebExtension manifest. 
Javascript :: how to verify json format is valid 
Javascript :: how to check if jquery element is loaded 
Javascript :: TOTAL 
Javascript :: how to restrict page leave in javascript 
Javascript :: object mapper pretty write as string 
Javascript :: npm i resulting in many ERESOLVE issues 
Javascript :: for await range javascript 
Javascript :: discord.js send dm to specific user 
Javascript :: Parsing an array returned from a function JS 
Javascript :: nodejs s3 read 
Javascript :: Masonry js css 
Javascript :: js remove undefined object 
Javascript :: jabascript for each 
Javascript :: clear input field 
Javascript :: on device size changes react-multi-carousel items not showing 
Javascript :: dynamically define routes separated in different pages for React 
Javascript :: jquery show div class 
Javascript :: class validator validate form data 
Javascript :: a method that will do something to each of the values in the array 
Javascript :: all ways pass data to onather page in javascript 
Javascript :: check if the last character of word is "A" 
Javascript :: send data to another page javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =