中间被启程日本的面试弄的没有静下心来复习算法。这样不好,基本功是硬道理。逐步恢复刷题。

题目:给一个数独(九宫格)中的一些数字,判断该数独是否有效。

即按照数独的规则,判断其行、列、小九格中是否有重复的数字。如有,即判断无效。

直接给代码吧,长期没刷题,代码质量有所下降。

  public boolean isValidSudoku(char[][] board) {
int[] help = new int[10]; //横排检测
for(int i = 0 ; i < 9 ; i++){
for(int j = 0 ; j < 9 ; j++){
if(board[i][j] != '.') help[board[i][j] - '0']++;
}
for(int k = 1 ; k <= 9 ; k++){
if(help[k] > 1) return false;
else help[k] = 0;
}
} //竖排检测
for(int row = 0 ; row < 9 ; row++){
for(int col = 0 ; col < 9 ; col++){
if(board[col][row] != '.') help[board[col][row] - '0']++;
}
for(int k = 1 ; k <= 9 ; k++){
if(help[k] > 1) return false;
else help[k] = 0;
}
} //小九宫格检测
int rowStart = 0,rowEnd = 3, colStart = 0 ,colEnd = 3;
while(colStart < 9){
while(rowStart < 9){
for(int row = rowStart ; row < rowEnd ; row++){
for(int col = colStart ; col < colEnd ; col++){
if(board[row][col] != '.') help[board[row][col] - '0']++;
}
}
for(int k = 1 ; k <= 9 ; k++){
if(help[k] > 1) return false;
else help[k] = 0;
}
rowStart += 3;
rowEnd += 3;
}
colStart += 3;
colEnd += 3;
rowStart = 0;
rowEnd = 3;
}
return true;
}

[leetcode]_Valid Sudoku的更多相关文章

  1. leetcode先刷_Valid Sudoku

    我没有看到这个问题,这使其在现货需求数独,害怕一直没敢做.后来我发现原来的标题就是这么简单.推断现在只有数字全不符合的就可以了棋盘上的形势的要求. 是不是正确的三个周期..人是不能满意地看到每一行.每 ...

  2. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  3. LeetCode——Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  4. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  5. Leetcode Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  6. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  7. 【leetcode】Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  8. Java for LeetCode 037 Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  9. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

随机推荐

  1. DEDE织梦,文章页里的幻灯调用,能调用全部栏目的吗

    arclist 是必须要有 typeid 设置的,如果你没设置,默认是取的当前栏目的 typeid,而首页取到的是 top,所以你强制指定typeid=top就可以了.---------------- ...

  2. mysql 错误1054

    问题,当查询数据时,输入字符串是数字时,可以查询,但当输入字母字符串时却不能查询,总是提示错误1054 解决:将字符串打上单引号 字段对应的值如果为字符或字符串类型的时候,应用英文单引号括起来,你用的 ...

  3. Qt编译安装后中文无法显示问题

    闲的蛋疼,把Ubuntu删了,再装10.04的时候,QT编译后运行自己的程序已经不能显示中文了,只能显示英文,字体贼丑... 想了各种办法,都没解决.. 最后:终于搞定: apt-get instal ...

  4. Spring配置事务 http://www.cnblogs.com/leiOOlei/p/3725911.html

    http://www.cnblogs.com/leiOOlei/p/3725911.html JNDI方式配置数据源: <?xml version="1.0" encodin ...

  5. 如何隐藏storyboard中的top bar

    在navigation bar中是没有设置它的隐藏和显示的属性的 那么究竟在什么地方呢,当在它自身的属性中没有找到的情况下,那么就只能去包含它的容器中去寻找了,结果果然找到了.在view的第四个选择器 ...

  6. cocos2d-x 3.x丨搭建Android环境下的开发环境

    所需要的一些工具软件: 1.JDK  官网下载地址:http://www.oracle.com/ttechnetwork/java/javase/downloads/index.html 2.Andr ...

  7. 长期内部推荐SAP职位,包括Java ABAP 咨询顾问,Developer,架构师等。

    长期内部推荐SAP职位,包括Java ABAP 咨询顾问,Developer,架构师等. 有需要请发简历到邮箱    LoB Position LocationAcquisitions Hybris ...

  8. tornado框架之路三之ajax

    一.ajax 1.传统的Web应用 一个简单操作需要重新加载全局数据 2.AJAX AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是 ...

  9. 【练习】如何显示本地主机上的MySQL客户机版本

    在终端窗口以oracle用户身份登录 [root@enmo ~]# su - oracle [oracle@enmo ~]$ mysql -V mysql Ver , for Linux (x86_6 ...

  10. SQL常用日期转换

    0   或   100   (*)     默认值   mon   dd   yyyy   hh:miAM(或   PM)       1   101   美国   mm/dd/yyyy       ...