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

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

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

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

  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. javascript最新深度克隆对象方法

    javascript最新深度克隆对象方法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  2. [ActionScript 3.0] Away3D 旋转效果

    package { import away3d.containers.View3D; import away3d.entities.Mesh; import away3d.events.MouseEv ...

  3. Codeforces 626E Simple Skewness 「数学」「二分」

    题意: 给你一堆无序数,寻找它的一个子堆,使得子堆的平均数减中位数最大. 数字的个数n<=2e5 0<=xi<=1e6. 思路: 首先可以证明这堆数一定是奇数个,证明方法是尝试在奇数 ...

  4. poj 1273 (nyoj 323) Drainage Ditches : 最大流

    点击打开链接 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49648   Accepte ...

  5. dhtmlxGrid分页查询,条件查询实例

    使用jquery的ajax get将页面条件请求到后台,取得数据库数据,分页查询,返回前台grid中. 引入所需文件: <script>window.dhx_globalImgPath = ...

  6. 翻译:Angular 2 - TypeScript 5 分钟快速入门

    原文地址:https://angular.io/docs/ts/latest/quickstart.html Angular 2 终于发布了 beta 版.这意味着正式版应该很快就要发布了. 让我们使 ...

  7. 一名合格QA的基本素养

    测试不是无脑的重复劳动.测试与开发一样,先用脑,再动手. 拒绝搬砖,关注技术: 把握需求,极致体验: 把控进度,高效沟通:   测试技术 如果你只会加班,行万里路,也只是个邮差 将程序测"好 ...

  8. C# 版dll 程序集合并工具

    C# 版dll 程序集合并工具 最近要开发一个控件给同事用,开发中会引用一些第三方DLL,这样交给用户很不方便,希望的效果是直接交付一个DLL文件.网上找了一些资料. 1.       使用 Cost ...

  9. unity区分点击在3D物体还是2D UI上

    当场景中的3D物体需要响应点击,但同时有UI显示时,存在判断点击是在3D物体上还是UI上的问题,办法如下: 1. 射线检测所有2D 3D物体,有2D物体被检测到时表明当前有UI.但无论Physics2 ...

  10. PagedList.MVC分页

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...