小white刷题记——LeetCodeHot100_36

该文章提供了一个使用哈希结构解决LeetCode热门问题36的算法,即检查给定的数独是否有效。通过遍历数独的行、列和小九宫格,统计每个位置的数字出现次数,如果发现有数字超过一次,则返回false,否则返回true。
摘要由CSDN通过智能技术生成

LeetCodeHot100_36——《有效的数独》

读题思路:

        思路其实就是在分别建立行、列、小九宫格三类哈希结构,遍历的过程中判断里面出现的次数,如果有数字出现的次数大于1的话,那就返回false,否则就返回true;

具体代码:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool isValidSudoku(vector<vector<char>>& board) {
	int rows[9][9]; //定义行,用来统计行中出现的数字的次数
	int columns[9][9];//定义列,用来统计列中出现的数字的次数
	int nineSquares[3][3][9];//定义九宫格,用来统计小九宫格中出现的数字的次数
	memset(rows, 0, sizeof(rows));
	memset(columns, 0, sizeof(columns));
	memset(nineSquares, 0, sizeof(nineSquares));
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			char c = board[i][j];
			if (c!='.') //如果遍历过程中出现了数字的话
			{
				int index = c - '0' - 1;
				rows[i][index]++;    //rows[i][index]表示的是第i行这个index这个数字出现的次数
				columns[j][index]++;//columns[j][index]表示的是第i行这个index这个数字出现的次数
				nineSquares[i / 3][j / 3][index]++; //小九宫格中index出现的次数
				if (rows[i][index] > 1 || columns[j][index] > 1 || nineSquares[i / 3][j / 3][index] > 1) {
					return false;
				}
			}
		}
	}
	return true;
}
int main() {
	vector<vector<char>> board = { 
 {'8','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'}};
	cout << isValidSudoku(board);
	return 0;
}

“ # 设置按钮的背景颜色 self.m_button1.SetBackgroundColour('#0a74f7') self.m_button1.SetForegroundColour('white') self.m_button2.SetBackgroundColour('#0a74f7') self.m_button2.SetForegroundColour('white') self.m_button3.SetBackgroundColour('#0a74f7') self.m_button3.SetForegroundColour('white') self.m_button4.SetBackgroundColour('#238E23') self.m_button4.SetForegroundColour('white') self.m_button5.SetBackgroundColour('#238E23') self.m_button5.SetForegroundColour('white') self.m_button6.SetBackgroundColour('#238E23') self.m_button6.SetForegroundColour('white') self.m_button7.SetBackgroundColour('#6F4242') self.m_button7.SetForegroundColour('white') self.m_button8.SetBackgroundColour('#6F4242') self.m_button8.SetForegroundColour('white') self.m_button9.SetBackgroundColour('#6F4242') self.m_button9.SetForegroundColour('white') self.m_button10.SetBackgroundColour('#8E6B23') self.m_button10.SetForegroundColour('white') self.m_button11.SetBackgroundColour('#8E6B23') self.m_button11.SetForegroundColour('white') self.m_button12.SetBackgroundColour('#8E6B23') self.m_button12.SetForegroundColour('white') self.m_button13.SetBackgroundColour('#8E6B23') self.m_button13.SetForegroundColour('white') self.m_button14.SetBackgroundColour('#545454') self.m_button14.SetForegroundColour('white') self.m_button15.SetBackgroundColour('#545454') self.m_button15.SetForegroundColour('white') self.m_button16.SetBackgroundColour('#545454') self.m_button16.SetForegroundColour('white') self.m_panel1.SetBackgroundColour('white') # 设置面板的背景颜色”逐行解释代码
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值