leetcode T36

package com.david.leetcode;

import java.util.HashMap;

public class T36 {
	class Solution {
	    /**
	 case1:   记住可以利用数组结合循环创建多个同类对象
	    利用的还是官方思路:
	    为每一行,每一列,每一个子3*3的数独分别建立一个HashMap<Integer,Integer>用于统计遍历到的数字的出现情况
	    特别注意子3*3数独的索引与行i,列j的关系为 索引=i/3*3+j/3;
	 char类型数字c如何转换陈对应的int型数值(利用ascii码);c-'0'/c - 48('0'对应的ascii码=48)
	    */
	    HashMap<Integer,Integer>[] rows = new HashMap[9];
	    HashMap<Integer,Integer>[] cols = new HashMap[9];
	    HashMap<Integer,Integer>[] sds = new HashMap[9];
	    public boolean isValidSudoku(char[][] board) {
	        for(int i=0;i<9;i++){
	            rows[i] = new HashMap<Integer,Integer>();
	            cols[i] = new HashMap<Integer,Integer>();
	            sds[i] = new HashMap<Integer,Integer>();
	        }
	        for(int i=0;i<9;i++){
	            for(int j=0;j<9;j++){
	                char temp = board[i][j];
	                if(temp!='.'){
	                    /*
	                    这里不能先进行判断,套先加入后再判断(比如某一行或列或box中只有两个相同数字时,这时明显要return false;但是如果采用先判断在加入的方式则检测不出来这类情况,它会返回true)
	                    */
	                    rows[i].put(temp-48,rows[i].getOrDefault(temp-48,0)+1);
	                    cols[j].put(temp-48,cols[j].getOrDefault(temp-48,0)+1);
	                    sds[(i/3)*3+j/3].put(temp-48,sds[(i/3)*3+j/3].getOrDefault(temp-48,0)+1);
	                    if(rows[i].get(temp-48)>1||cols[j].get(temp-48)>1||sds[(i/3)*3+j/3].get(temp-48)>1) return false;
	                }
	            }
	        }
	        return true;
	    
	    }
	}
/**
 * Case2:采用下面的思路:
	     为行,列,各子box各自创建一个9*9的boolean数组第0个下标用于表示第几个行/列/box,第1个下标用于记录0-9中各个数的出现情况 
		    特别注意子3*3数独的索引与行i,列j的关系为 索引=i/3*3+j/3;
		 char类型数字c如何转换陈对应的int型数值(利用ascii码);c-'0'/c-48('0'对应的ascii码=48)
 * */
	class Solution2 {
	   public boolean isValidSudoku(char[][] board) {
	        // 记录某行,某位数字是否已经被摆放
	        boolean[][] row = new boolean[9][9];
	        // 记录某列,某位数字是否已经被摆放
	        boolean[][] col = new boolean[9][9];
	        // 记录某 3x3 宫格内,某位数字是否已经被摆放
	        boolean[][] block = new boolean[9][9];

	        for (int i = 0; i < 9; i++) {
	            for (int j = 0; j < 9; j++) {
	                if (board[i][j] != '.') {
	                    int num = board[i][j] - '1';
	                    int blockIndex = i / 3 * 3 + j / 3;
	                    if (row[i][num] || col[j][num] || block[blockIndex][num]) {
	                        return false;
	                    } else {
	                        row[i][num] = true;
	                        col[j][num] = true;
	                        block[blockIndex][num] = true;
	                    }
	                }
	            }
	        }
	        return true;
	    }
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值