Valid Sudoku
理解题目的意思后这题不难。扫描一遍数独输入并按照要求进行判断就可以了。提交了两次,第一次用了stl的set,第二次本来想借助位运算的,想想觉得有些操作略显麻烦,因此用整数数组代替。代码如下:
解法一:
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
set<char> rowSet;
set<char> colSets[];
set<char> subSets[];
set<char>::iterator itr;
char c;
for(int i=;i<;i++)
{
rowSet.clear();
for(int j=;j<;j++)
{
c=board[i][j];
if(c=='.')
{
continue;
}
else
{
if(rowSet.find(c)==rowSet.end())
{
rowSet.insert(c);
}
else
return false;
if(colSets[j].find(c)==colSets[j].end())
{
colSets[j].insert(c);
}
else
return false;
int idx=*(i/)+j/;
if(subSets[idx].find(c)==subSets[idx].end())
{
subSets[idx].insert(c);
}
else
return false;
}
}
}
return true;
}
};
解法二:
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
int rowMap[];
int colMaps[][];
int subMaps[][];
char c;
int iv;
int subIdx;
memset(colMaps,,sizeof(colMaps));
memset(subMaps,,sizeof(subMaps));
for(int i=;i<;i++)
{
memset(rowMap,,sizeof(rowMap));
for(int j=;j<;j++)
{
c=board[i][j];
if(c=='.')
continue;
iv=c-''-;
if(rowMap[iv]==)
return false;
rowMap[iv]=;
if(colMaps[j][iv]==)
return false;
colMaps[j][iv]=;
subIdx=(i/)*+j/;
if(subMaps[subIdx][iv]==)
return false;
subMaps[subIdx][iv]=;
}
}
return true;
}
};
本质上来说解法一和解法二没有什么区别。但解法二是使用数组直接寻址,因此理论上来说效率应该更高。在使用解法二的过程中,代码一直不能AC,无奈只能一步一步调试,最终定位到"iv=c-'0'-1"这一行。原来是"iv=c-'0',但数独的输入其实是1-9,因此会越界,结果当然不对。这些细节问题,是自己粗心造成的,太不应该。
Valid Sudoku的更多相关文章
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- 【leetcode】Valid Sudoku
题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- Leetcode Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- leetcode36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 36. Valid Sudoku
============= Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku b ...
- 【LeetCode】36 - Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRu ...
- Java [leetcode 36]Valid Sudoku
题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- leetcode@ [36/37] Valid Sudoku / Sudoku Solver
https://leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puz ...
- [Leetcode][Python]36: Valid Sudoku
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...
随机推荐
- python的win32操作
## _*_ coding:UTF-8 _*___author__ = 'shanl'import win32apiimport win32conimport win32guifrom ctypes ...
- 利用BMFont和NGUI制作字体集
Unity中常常需要制作字体,也算是Unity的基本优势吧!其实质就是BMFont和NGUI制作字体.这里把步骤介绍一下: 1.先下载BMFont这个工具 2.Font Settings 设置:(1 ...
- dynamics_cast<>
#include <iostream> class A { public: A(){} ~A(){} ;} }; class B:public A { public: B(){} ~B() ...
- Tiny Rss简明安装与配置笔记
对于某些自己喜欢的却没有RSS订阅功能的网站,可以通过Feed43定制抓取规则来获取所需信息.但由于其服务器在国外,抓取时常失败,且免费账户抓取频率太低,遂仿造他自行实现了一个并挂到服务器上,然后通过 ...
- mysql查询优化器的提示(hit)
如果对优化器选择的执行计划不满意,可以使用优化器提供的几个提示来控制最终的执行计划,关于每个提示的具体用法,建议直接阅读官方手册,一些提示和版本有直接关系,可以使用的一些提示如下: high_prio ...
- ajax 代码
function ajax(){ var aj=null; if(window.ActiveXObject){ aj = new ActiveXObject("Microsoft.XMLHT ...
- noi 9268 酒鬼
题目链接:http://noi.openjudge.cn/ch0206/9268/ 题意:有N瓶酒,不能连续喝>=3瓶的酒,问能喝的最大的酒量. f[i][j] 前 I 瓶中连续喝了 j 瓶的最 ...
- Flowplayer-Subtitle
SOURCE URL: https://flowplayer.org/docs/subtitles.html Setting up Subtitles are loaded with a <tr ...
- ubuntu 到底是选择32位还是64位?
ubuntu 到底是选择32位还是64位? 2011-06-03 15:16:31 标签:ubuntu linux 休闲 cpu 职场 原文出处:官方wiki原文作者:授权许可: 创作共用协议Att ...
- 使用bootstrap框架的模态框与ckeditor产生冲突,ckeditor的弹出窗不可用时的解决方法
这样可以解决冲突 $.fn.modal.Constructor.prototype.enforceFocus = function () { modal_this = this $(document) ...