새소식

인기 검색어

프로그래머스/Lv.0

[Swift] 안전지대

  • -
import Foundation

func solution(_ board:[[Int]]) -> Int {

    var results: [[Int]] = Array(repeating: Array(repeating: 0, count: board.count), count: board.count)

    for (i,first) in board.enumerated() {
        for (j,second) in first.enumerated() {
            if board[i][j] == 1 {
                for x in i-1...i+1 {
                    for y in j-1...j+1{
                        if 0 <= x && 0 <= y && x < board.count && y < board.count { results[x][y] = 1 }
                    }
                }
            }
        }
    }

    return board.count * board.count - results.map{ $0.reduce(0,+) }.reduce(0, +)
}

'프로그래머스 > Lv.0' 카테고리의 다른 글

[Swift] 약수 구하기  (0) 2022.12.26
[Swift] 암호해독  (0) 2022.12.26
[Swift] 아이스 아메리카노  (0) 2022.12.26
[Swift] 숫자 찾기  (0) 2022.12.26
[Swift] 숫자 비교하기  (0) 2022.12.26
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.