题目一.杨辉三角
118. 杨辉三角https://leetcode.cn/problems/pascals-triangle/
给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。在「杨辉三角」中,每个数是它左上方和右上方的数的和。
示例 :
输入: numRows = 5
输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
1.思路分析:
本题是十分常见的输出杨辉三角前指定行数的问题,可采用的数据结构非常多:常见的如队列等。在此讨论矩阵法:在处理杨辉三角时,有一个小窍门:可以将杨辉三角的每一行左对齐,将其看做是一个下三角方阵,将每一个除第一列外的矩阵元素都视为其正上方元素与正上方左侧元素之和,即:row[i][j]=row[i-1][j]+row[i-1][j-1];
下三角矩阵对角线上方总是0,而杨辉三角对角线上元素必为1,这样扩展的好处在于统一了代码结构,方便编程设计,减少边界条件,这是很重要的一类编程思维。
2.AC代码:
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** generate(int numRows, int* returnSize, int** returnColumnSizes){
*returnSize=numRows;
*returnColumnSizes=(int**)malloc(numRows*sizeof(int));
for(int i=0;i<numRows;i++){
(*returnColumnSizes)[i]=i+1;
}
int** row=(int**)malloc(numRows*sizeof(int*));
for(int i=0;i<numRows;i++) row[i]=(int*)malloc(numRows*sizeof(int));
for(int k=0;k<numRows;k++){
for(int i=0;i<numRows;i++) row[k][i]=0;
}
for(int i=0;i<numRows;i++) row[i][0]=1;
for(int i=1;i<numRows;i++){
for(int j=(*returnColumnSizes)[i]-1;j>0;j--){
row[i][j]=row[i-1][j]+row[i-1][j-1];
}
}
return row;
}
题目二.有效的数独
36. 有效的数独https://leetcode.cn/problems/valid-sudoku/
请你判断一个 9 x 9
的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
- 数字
1-9
在每一行只能出现一次。 - 数字
1-9
在每一列只能出现一次。 - 数字
1-9
在每一个以粗实线分隔的3x3
宫内只能出现一次。(请参考示例图)
注意:
- 一个有效的数独(部分已被填充)不一定是可解的。
- 只需要根据以上规则,验证已经填入的数字是否有效即可。
- 空白格用
'.'
表示。
示例 1:
输入:board = [["5","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"]] 输出:true示例 2:
输入: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"]] 输出:false 解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
1.思路分析:
本题若按题目要求分别多次遍历,属于暴力解决法,实现并不困难:
需要设计一个长度为9辅助数组flag[]用于作为遍历标志位,用于标志数字1-9出现次数,初始时候必须全置为0,当按一种方式遍历结束时,遍历一遍flag数组,若出现某个元素值大于1,说明该数出现次数超过一次,退出循环并返回false;否则全部置0,开始下一种遍历。
2.AC代码:
bool isValidSudoku(char** board, int boardSize, int* boardColSize){
int* flag=(int*)malloc(9*sizeof(int));
for(int i=0;i<9;i++) flag[i]=0;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j]=='1') flag[0]++;
if(board[i][j]=='2') flag[1]++;
if(board[i][j]=='3') flag[2]++;
if(board[i][j]=='4') flag[3]++;
if(board[i][j]=='5') flag[4]++;
if(board[i][j]=='6') flag[5]++;
if(board[i][j]=='7') flag[6]++;
if(board[i][j]=='8') flag[7]++;
if(board[i][j]=='9') flag[8]++;
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
}
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[j][i]=='1') flag[0]++;
if(board[j][i]=='2') flag[1]++;
if(board[j][i]=='3') flag[2]++;
if(board[j][i]=='4') flag[3]++;
if(board[j][i]=='5') flag[4]++;
if(board[j][i]=='6') flag[5]++;
if(board[j][i]=='7') flag[6]++;
if(board[j][i]=='8') flag[7]++;
if(board[j][i]=='9') flag[8]++;
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
}
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
if(board[i][j]=='1') flag[0]++;
if(board[i][j]=='2') flag[1]++;
if(board[i][j]=='3') flag[2]++;
if(board[i][j]=='4') flag[3]++;
if(board[i][j]=='5') flag[4]++;
if(board[i][j]=='6') flag[5]++;
if(board[i][j]=='7') flag[6]++;
if(board[i][j]=='8') flag[7]++;
if(board[i][j]=='9') flag[8]++;
}
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
for(int i=0;i<=2;i++){
for(int j=3;j<=5;j++){
if(board[i][j]=='1') flag[0]++;
if(board[i][j]=='2') flag[1]++;
if(board[i][j]=='3') flag[2]++;
if(board[i][j]=='4') flag[3]++;
if(board[i][j]=='5') flag[4]++;
if(board[i][j]=='6') flag[5]++;
if(board[i][j]=='7') flag[6]++;
if(board[i][j]=='8') flag[7]++;
if(board[i][j]=='9') flag[8]++;
}
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
for(int i=0;i<=2;i++){
for(int j=6;j<=8;j++){
if(board[i][j]=='1') flag[0]++;
if(board[i][j]=='2') flag[1]++;
if(board[i][j]=='3') flag[2]++;
if(board[i][j]=='4') flag[3]++;
if(board[i][j]=='5') flag[4]++;
if(board[i][j]=='6') flag[5]++;
if(board[i][j]=='7') flag[6]++;
if(board[i][j]=='8') flag[7]++;
if(board[i][j]=='9') flag[8]++;
}
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
for(int i=3;i<=5;i++){
for(int j=0;j<=2;j++){
if(board[i][j]=='1') flag[0]++;
if(board[i][j]=='2') flag[1]++;
if(board[i][j]=='3') flag[2]++;
if(board[i][j]=='4') flag[3]++;
if(board[i][j]=='5') flag[4]++;
if(board[i][j]=='6') flag[5]++;
if(board[i][j]=='7') flag[6]++;
if(board[i][j]=='8') flag[7]++;
if(board[i][j]=='9') flag[8]++;
}
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
for(int i=3;i<=5;i++){
for(int j=3;j<=5;j++){
if(board[i][j]=='1') flag[0]++;
if(board[i][j]=='2') flag[1]++;
if(board[i][j]=='3') flag[2]++;
if(board[i][j]=='4') flag[3]++;
if(board[i][j]=='5') flag[4]++;
if(board[i][j]=='6') flag[5]++;
if(board[i][j]=='7') flag[6]++;
if(board[i][j]=='8') flag[7]++;
if(board[i][j]=='9') flag[8]++;
}
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
for(int i=3;i<=5;i++){
for(int j=6;j<=8;j++){
if(board[i][j]=='1') flag[0]++;
if(board[i][j]=='2') flag[1]++;
if(board[i][j]=='3') flag[2]++;
if(board[i][j]=='4') flag[3]++;
if(board[i][j]=='5') flag[4]++;
if(board[i][j]=='6') flag[5]++;
if(board[i][j]=='7') flag[6]++;
if(board[i][j]=='8') flag[7]++;
if(board[i][j]=='9') flag[8]++;
}
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
for(int i=6;i<=8;i++){
for(int j=0;j<=2;j++){
if(board[i][j]=='1') flag[0]++;
if(board[i][j]=='2') flag[1]++;
if(board[i][j]=='3') flag[2]++;
if(board[i][j]=='4') flag[3]++;
if(board[i][j]=='5') flag[4]++;
if(board[i][j]=='6') flag[5]++;
if(board[i][j]=='7') flag[6]++;
if(board[i][j]=='8') flag[7]++;
if(board[i][j]=='9') flag[8]++;
}
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
for(int i=6;i<=8;i++){
for(int j=3;j<=5;j++){
if(board[i][j]=='1') flag[0]++;
if(board[i][j]=='2') flag[1]++;
if(board[i][j]=='3') flag[2]++;
if(board[i][j]=='4') flag[3]++;
if(board[i][j]=='5') flag[4]++;
if(board[i][j]=='6') flag[5]++;
if(board[i][j]=='7') flag[6]++;
if(board[i][j]=='8') flag[7]++;
if(board[i][j]=='9') flag[8]++;
}
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
for(int i=6;i<=8;i++){
for(int j=6;j<=8;j++){
if(board[i][j]=='1') flag[0]++;
if(board[i][j]=='2') flag[1]++;
if(board[i][j]=='3') flag[2]++;
if(board[i][j]=='4') flag[3]++;
if(board[i][j]=='5') flag[4]++;
if(board[i][j]=='6') flag[5]++;
if(board[i][j]=='7') flag[6]++;
if(board[i][j]=='8') flag[7]++;
if(board[i][j]=='9') flag[8]++;
}
}
for(int k=0;k<9;k++) {
if(flag[k]>1) return false;
else flag[k]=0;
}
return true;
}