my swift solution beat 95% O(n2)


  • 0
    W
    class Solution {
        func isValidSudoku(_ board: [[Character]]) -> Bool {
            var dic = [Character:[CGPoint]]()
            for i in 0...board.count - 1 {
                let lineArr = board[i]
            
                for j in 0...lineArr.count - 1 {
                    let ch = lineArr[j]
                    if ch == "." {continue}
                    var arr = dic[ch]
                    if arr != nil {
                        for point in arr! {
                            if Int(point.x) == i || Int(point.y) == j {
                                return false
                            }
                            if Int(point.x) / 3 == i / 3 && Int(point.y) / 3 == j / 3 {
                                return false
                            }
                        }
                    }else {
                        arr = [CGPoint]()
                    }
                    arr?.append(CGPoint.init(x: CGFloat(i), y: CGFloat(j)))
                    dic[ch] = arr!
                }   
            }
            return true
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.