zoukankan      html  css  js  c++  java
  • LeetCode——Valid Sudoku

    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.

    原题链接:https://oj.leetcode.com/problems/valid-sudoku/

    依照数独的规则。一行、一列、对角线、9个小格中不出现同一个数字。

    import java.util.ArrayList;
    import java.util.List;
    
    
    public class ValidSudoku {
    	public boolean isValidSudoku(char[][] board) {
    		for(int i=0;i<9;i++){
    			List<Character> list = new ArrayList<Character>();
    			for(int j=0;j<9;j++)
    				list.add(board[i][j]);
    			if(!isValid(list))
    				return false;
    		}
    		for(int i=0;i<9;i++){
    			List<Character> list = new ArrayList<Character>();
    			for(int j=0;j<9;j++)
    				list.add(board[j][i]);
    			if(!isValid(list))
    				return false;
    		}
    		for(int i=0;i<3;i++){
    			for(int j=0;j<3;j++){
    				List<Character> list = new ArrayList<Character>();
    				for(int k=0;k<3;k++){
    					for(int l=0;l<3;l++){
    						list.add(board[i*3+k][j*3+l]);
    					}
    				}
    				if(!isValid(list))
    					return false;
    			}
    		}
    		return true;
    	}
    	private boolean isValid(List<Character> list){
    		for(Character ch : list){
    			if(ch != '.')
    				if(list.indexOf(ch) != list.lastIndexOf(ch))
    					return false;
    		}
    		return true;
    	}
    }
    


  • 相关阅读:
    Java8新特性简介
    责任链模式
    Bean的生命周期
    APP中https证书有效性验证引发安全问题(例Fiddler可抓https包)
    程序员成长指南
    Go 代码性能优化小技巧
    Go slice 扩容机制分析
    一次 Go 程序 out of memory 排查及反思
    curl 常用操作总结
    Go benchmark 详解
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5055386.html
Copyright © 2011-2022 走看看