Upgrade to Pro — share decks privately, control downloads, hide ads and more …

swift warmup 10/4

Johnlin
October 04, 2016

swift warmup 10/4

valid-sudoku

Johnlin

October 04, 2016
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. • Determine if a Sudoku is valid, according to: Sudoku

    Puzzles - The Rules. • ࠜᎦᏐᘐنଇိ൑ᏗҰݸᏐᘐੋෆੋ༗Ꮘతɻ
  2. • The Sudoku board could be partially filled, where empty

    cells are filled with the character '.'. • Ꮠᘐత൫໘༗Մೳੋෆ׬੔తɼۭനత஍ํ။ੋ .(ᴍ ᥒ)
  3. ୊໨ࠎՍ class Solution { func isValidSudoku(_ board: [[Character]]) -> Bool

    { } } let board = [
 “.87654321",
 "2........",
 "3........",
 "4........",
 "5........",
 "6........",
 "7........",
 "8........",
 "9........"]
  4. ᒾҰҰℷഉཫ༗ᔒ༗ॏෳ for row in board { var set = Set<Character>()

    for char in row { guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } }
  5. ᒾҰҰ௚ߦཫ༗ᔒ༗ॏෳ for col in 0..<9 { var set = Set<Character>()

    for row in 0..<9 { let char = board[row][col] guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } }
  6. ᒾҰეմத༗ᔒ༗ॏෳ for blockRow in 0..<3 { for blockCol in 0..<3

    { var set = Set<Character>() for row in 0..<3 { for col in 0..<3 { let char = board[blockRow*3 + row] [blockCol*3 + col] guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } } }
  7. class Solution { func isValidSudoku(_ board: [[Character]]) -> Bool {

    for row in board { var set = Set<Character>() for char in row { guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } for col in 0..<9 { var set = Set<Character>() for row in 0..<9 { let char = board[row][col] guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } for blockRow in 0..<3 { for blockCol in 0..<3 { var set = Set<Character>() for row in 0..<3 { for col in 0..<3 { let char = board[blockRow*3 + row][blockCol*3 + col] guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } } } return true } } Runtime: 119 ms (50%)
  8. class Solution { func isValidSudoku(_ board: [[Character]]) -> Bool {

    for row in board { var set = Set<Character>() for char in row { guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } for col in 0..<9 { var set = Set<Character>() for row in 0..<9 { let char = board[row][col] guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } for blockRow in 0..<3 { for blockCol in 0..<3 { var set = Set<Character>() for row in 0..<3 { for col in 0..<3 { let char = board[blockRow*3 + row][blockCol*3 + col] guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } } } return true } }
  9. ೺௨༻త෦෼፮ग़ိ class Checker { var set = Set<Character>() func check(_

    c:Character) -> Bool { guard c != "." else { return true } guard !set.contains(c) else { return false } set.insert(c) return true } }
  10. class Solution { func isValidSudoku(_ board: [[Character]]) -> Bool {

    for row in board { let checker = Checker() for char in row { guard checker.check(char) else { return false } } } for col in 0..<9 { let checker = Checker() for row in 0..<9 { let char = board[row][col] guard checker.check(char) else { return false } } } for blockRow in 0..<3 { for blockCol in 0..<3 { let checker = Checker() for row in 0..<3 { for col in 0..<3 { let char = board[blockRow*3 + row][blockCol*3 + col] guard checker.check(char) else { return false } } } } } return true } } Runtime: 135 ms(40%)
  11. Ճշ Checker class Checker { var set = Array(repeating: false,

    count: 9) let base = Int("1".unicodeScalars.first!.value) func check(_ c:Character) -> Bool { guard c != "." else { return true } let idx:Int = Int(String(c).unicodeScalars.first!.value) - base guard set[idx] != true else { return false } set[idx] = true return true } } Runtime: 95 ms(80%)
  12. Q&A