36.有效的数独——记录(C++)

88 篇文章 0 订阅
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
         int x=3;
         int y=3;
         int tx=0;
         int ty=0;
         for(int i=0;i<9;i++)
         {
             int a[10];
             memset(a,0,sizeof(a)) ; 
             int b[10];
             memset(b,0,sizeof(b)) ;
             for(int j=0;j<9;j++)
             {
                  if(board[j][i]!='.')
                 {
                     int p=board[j][i]-'0';
                   if(b[p]==0)
                  {
                     b[p]=1;
                  }
                 else
                 {
                     return 0;
                 }  
                 }
                 if(board[i][j]!='.')
                 { 
                     int q=board[i][j]-'0';
                     
                      if(a[q]==0)
                 {
                     a[q]=1;
                 }
                 else
                 {
                     return 0;
                 }
                 }  
                 }
         }
  int c[10];
  memset(c,0,sizeof(c)) ;
while(x<=9)
    { 
         for(tx=x-3;tx<x;++tx)
         {
             for(ty=y-3;ty<y;++ty)
             {
                 if(board[tx][ty]!='.')
                 {
                     if(c[board[tx][ty]-'0']==0)
                     {
                        c[board[tx][ty]-'0']=1;
                     }
                     else
                     {
                         return 0;
                     }
                 }  
             }
         } 
          memset(c,0,sizeof(c))     ;
         if(y<9)
             { 
                 y+=3;     
             }
        else
             {  
                 y=3;
                 x+=3;
             }
     }  
    return 1;
    }
};

又是纯暴力的一天。

不过这一题花了两个小时。很烦。中间debug一度想要放掉。

主要的思路和代码不到一个小时就结束了。

就因为几个点一直没找到所以浪费了好多时间。

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int rows[9][9];
        int columns[9][9];
        int subboxes[3][3][9];
        
        memset(rows,0,sizeof(rows));
        memset(columns,0,sizeof(columns));
        memset(subboxes,0,sizeof(subboxes));
        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]++;
                    columns[j][index]++;
                    subboxes[i / 3][j / 3][index]++;
                    if (rows[i][index] > 1 || columns[j][index] > 1 || subboxes[i / 3][j / 3][index] > 1) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
};

 同样是遍历,答案的代码就好看很多了。

T.T

加油!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值