Utopi
a

有效的数独

To be clear , I am unable to do so...

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Example

Input: [ ["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ] Output: true

160 ms

class Solution { func isValidSudoku(_ board: [[Character]]) -> Bool { var rowNums = Array(repeating: 0, count: 9) var colNums = Array(repeating: 0, count: 9) var boxNums = Array(repeating: 0, count: 9) for i in 0..<9 { for j in 0..<9 { if !board[i][j].isNumber { continue } let num = board[i][j].wholeNumberValue ?? 0 if rowNums[i] & 1 << num != 0 { return false } else { rowNums[i] |= 1 << num } if colNums[j] & 1 << num != 0{ return false } else { colNums[j] |= 1 << num } let boxIndex = i / 3 * 3 + j / 3 if boxNums[boxIndex] & 1 << num != 0 { return false } else { boxNums[boxIndex] |= 1 << num } } } return true } }

Swift Continue 语句

Swift continue语句告诉一个循环体立刻停止本次循环迭代,重新开始下次循环迭代。

位运算符

位运算符用来对二进制位进行操作,~ , & , | , ^ 分别为取反,按位与与,按位与或,按位与异或运算,如下表实例:

如果指定 A = 60; 及 B = 13; 两个变量对应的二进制为:

A = 0011 1100 B = 0000 1101

Swift 正则表达式 NSRegularExpression

枚举类型

typedef NS_OPTIONS(NSUInteger, NSRegularExpressionOptions) { NSRegularExpressionCaseInsensitive = 1 << 0, // 不区分大小写的 NSRegularExpressionAllowCommentsAndWhitespace = 1 << 1, // 忽略空格和# (注释符) NSRegularExpressionIgnoreMetacharacters = 1 << 2, // 整体化 NSRegularExpressionDotMatchesLineSeparators = 1 << 3, // 匹配任何字符,包括行分隔符 NSRegularExpressionAnchorsMatchLines = 1 << 4, // 允许^和$在匹配的开始和结束行 NSRegularExpressionUseUnixLineSeparators = 1 << 5, // (查找范围为整个的话无效) NSRegularExpressionUseUnicodeWordBoundaries = 1 << 6 // (查找范围为整个的话无效) }; typedef NS_OPTIONS(NSUInteger, NSMatchingOptions) { NSMatchingReportProgress = 1 << 0, //找到最长的匹配字符串后调用block回调 NSMatchingReportCompletion = 1 << 1, //找到任何一个匹配串后都回调一次block NSMatchingAnchored = 1 << 2, //从匹配范围的开始处进行匹配 NSMatchingWithTransparentBounds = 1 << 3, //允许匹配的范围超出设置的范围 NSMatchingWithoutAnchoringBounds = 1 << 4 //禁止^和$自动匹配行还是和结束 };

哦我的天哪,太多了,今天就到这里为止吧!