Leetcode 细节实现

Valid Sudoku

Total Accepted: 13142 Total
Submissions: 47070My Submissions

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

The Sudoku board could be partially filled, where empty cells are filled with the character ‘.‘.

A partially filled sudoku which is valid.

Note:

A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

题意:检查一个给定的数独是否是合理的,该数独可能可有些单元是空白的

思路:

检查每一行,第一列,每一个小方块中是否出现重复数字

可用一个大小为 9的数组表示某个数字是否出现过

复杂度:O(n^2)

bool isExist[9];

bool check(const vector<vector<char> >& board, int i, int j){
	if(board[i][j] != '.') {
		if (isExist[board[i][j] - '1']) return false;
		else return isExist[board[i][j] - '1'] = true;
	}
	return true;
};
bool isValidSudoku(const vector<vector<char> >& board){
	//检查行
	for(int i = 0; i < 9; ++i){
		memset(isExist, false, sizeof(isExist));
		for(int j = 0; j < 9; ++j){
			if(!check(board, i, j)) return false;
		}
	}
	//检查列
	for(int j = 0; j < 9; ++j){
		memset(isExist, false, sizeof(isExist));
		for(int i = 0; i < 9; ++i){
			if(!check(board, i, j)) return false;
		}
	}
	//检查小方块
	for(int k = 0; k < 9; ++k){
		memset(isExist, false, sizeof(isExist));
		for(int i = 0; i < 3; ++i){
			for(int j = 0; j < 3; ++j){
				int ii = k % 3 * 3 + i;
				int jj = k / 3 * 3 + j;
				if(!check(board, ii, jj)) return false;
			}
		}
	}
	return true;
}

时间: 2024-08-27 09:12:27

Leetcode 细节实现的相关文章

Leetcode 细节实现 Set Matrix Zeroes

Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. 题意:给定矩阵,如果矩阵的某个位置为0,则把那一行那一列的所有元素都置为0 思路:用两个bool数组,分

Leetcode 细节实现 Length of Last Word

Length of Last Word Total Accepted: 17518 Total Submissions: 59526My Submissions Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, re

Leetcode 细节实现题 Spiral Matrix

Spiral Matrix Total Accepted: 12721 Total Submissions: 62094My Submissions Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [

Leetcode 细节实现题 Spiral Matrix II

Spiral Matrix II Total Accepted: 12773 Total Submissions: 41526My Submissions Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2,

Leetcode 细节实现 Rotate Image

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Rotate Image Total Accepted: 15609 Total Submissions: 49679My Submissions You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you d

Leetcode 细节实现 Longest Common Prefix

Longest Common Prefix Total Accepted: 17298 Total Submissions: 63704My Submissions Write a function to find the longest common prefix string amongst an array of strings. 题意:在一个字符串数组中找到最长的公共前缀 思路: 扫描数组,直到遇到一个在各个字符串不一样的字符 复杂度:时间O(n1 + n2 + ...) --> 最差的

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1

LeetCode --- 65. Valid Number

题目链接:Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statemen

LeetCode -- Triangle 路径求最小和( 动态规划问题)

人们常说"细节决定成败". 编码工作中,同样需要关注细节. 本文将给出3个小实例来说明编码中关注细节的重要性,同时给出作者对如何注意编码细节的一点见解(说的不对,请指正). 例1 这个问题如此地显而易见,竟然没有被发现. List<int> numList = new List<int>(); numList.Add(3); numList.Add(1); numList.Add(4); numList.Add(2); numList.Add(5); numLi