Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit72747d5

Browse files
authored
Create 463-island-perimeter.js
solved island-perimeter in JS.
1 parent3cfb9a5 commit72747d5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎javascript/463-island-perimeter.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// problem link https://leetcode.com/problems/island-perimeter
2+
// time complexity. it looks O(n^2) but it's actually constant time O(1). Because the input widht and height won't exeed 100 as stated in the problem.
3+
4+
varislandPerimeter=function(grid){
5+
constrow=grid.length;
6+
constcolumn=grid[0].length;
7+
letperimeter=0;
8+
for(leti=0;i<row;i++){
9+
for(letj=0;j<column;j++){
10+
if(grid[i][j]==1){
11+
perimeter+=4;
12+
13+
if(i>0&&grid[i-1][j]==1){
14+
perimeter-=2;
15+
}
16+
if(j>0&&grid[i][j-1]==1){
17+
perimeter-=2;
18+
}
19+
}
20+
}
21+
}
22+
23+
returnperimeter
24+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp