Skip to content

Commit a32a4b6

Browse files
committed
0917
1 parent 5b5dc94 commit a32a4b6

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* function NestedInteger() {
5+
*
6+
* Return true if this NestedInteger holds a single integer, rather than a nested list.
7+
* @return {boolean}
8+
* this.isInteger = function() {
9+
* ...
10+
* };
11+
*
12+
* Return the single integer that this NestedInteger holds, if it holds a single integer
13+
* Return null if this NestedInteger holds a nested list
14+
* @return {integer}
15+
* this.getInteger = function() {
16+
* ...
17+
* };
18+
*
19+
* Set this NestedInteger to hold a single integer equal to value.
20+
* @return {void}
21+
* this.setInteger = function(value) {
22+
* ...
23+
* };
24+
*
25+
* Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
26+
* @return {void}
27+
* this.add = function(elem) {
28+
* ...
29+
* };
30+
*
31+
* Return the nested list that this NestedInteger holds, if it holds a nested list
32+
* Return null if this NestedInteger holds a single integer
33+
* @return {NestedInteger[]}
34+
* this.getList = function() {
35+
* ...
36+
* };
37+
* };
38+
*/
39+
/**
40+
* @param {string} s
41+
* @return {NestedInteger}
42+
*/
43+
var deserialize = function (s) {
44+
const nested = NestedInteger(s);
45+
return nested.getInteger();
46+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
var islandPerimeter = function(grid) {
6+
7+
const ROW=grid.length;
8+
const COL=grid[0].length;
9+
10+
//BFS
11+
function doBFS(startR,startC){
12+
const DIR=[[-1,0],[0,1],[1,0],[0,-1]]
13+
const visited=Array.from({length:ROW},()=>Array(COL));
14+
let count=0;
15+
16+
visited[startR][startC]=true;
17+
18+
const queue=[[startR,startC]];
19+
let head=0;
20+
21+
while(head<queue.length){
22+
const [curR,curC]=queue[head++];
23+
24+
for(let i=0;i<4;i++){
25+
const [nextR,nextC]=[curR+DIR[i][0],curC+DIR[i][1]];
26+
if(nextR<0 || nextR>=ROW || nextC<0 || nextC>=COL
27+
|| grid[nextR][nextC]===0){
28+
count++;
29+
continue;
30+
}
31+
if(visited[nextR][nextC]) continue;
32+
visited[nextR][nextC]=true;
33+
queue.push([nextR,nextC]);
34+
}
35+
}
36+
return count;
37+
38+
}
39+
40+
41+
// 실행부
42+
for(let i=0;i<ROW;i++){
43+
for(let j=0;j<COL;j++){
44+
if(grid[i][j]===1) return doBFS(i,j);
45+
}
46+
}
47+
48+
49+
};

0 commit comments

Comments
 (0)