File tree 2 files changed +43
-1
lines changed
longest-substring-without-repeating-characters
2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,6 @@ class Solution {
11
11
subStr.append(it)
12
12
}
13
13
max = max(max, subStr.length)
14
- println (subStr)
15
14
return max
16
15
}
17
16
}
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ // 시간 : O(N), 공간 O(N)
3
+ fun numIslands (grid : Array <CharArray >): Int {
4
+ val moveMap = arrayOf(
5
+ intArrayOf(1 , 0 ),
6
+ intArrayOf(0 , 1 )
7
+ )
8
+ val queue = mutableListOf<IntArray >()
9
+ val visitMap = Array (grid.size) {
10
+ BooleanArray (grid[it].size)
11
+ }
12
+ var count = 0
13
+ // 1. grid를 모두 조회
14
+ for (y in grid.indices) {
15
+ for (x in grid[y].indices) {
16
+ // 2. grid[y][x]가 1이고 방문한적이 없으면 newLand로 인지하고 count를 올린다.
17
+ if (grid[y][x] == ' 1' && ! visitMap[y][x]) {
18
+ count++
19
+ queue.add(intArrayOf(y, x))
20
+ }
21
+ // 3. 인접해있는 land를 조회하기위해 queue를 이용하여 탐색하고, visitMap을 변경해준다.
22
+ while (queue.isNotEmpty()) {
23
+ val newInfo = queue.removeFirst()
24
+ val newY = newInfo[0 ]
25
+ val newX = newInfo[1 ]
26
+ visitMap[newY][newX] = true
27
+ moveMap.forEach {
28
+ val nextY = newY + it[0 ]
29
+ val nextX = newX + it[1 ]
30
+ if (nextY < grid.size
31
+ && nextX < grid[nextY].size
32
+ && grid[nextY][nextX] == ' 1'
33
+ && ! visitMap[nextY][nextX]
34
+ ) {
35
+ queue.add(intArrayOf(nextY, nextX))
36
+ }
37
+ }
38
+ }
39
+ }
40
+ }
41
+ return count
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments