36. Valid Sudoku (M)

【LabVIEW面向对象编程01】简介 记得刚接触labview那会儿,领导让我在测试设备上写一个简单的日志记录程序,自己反反复复查阅资料才简单的写了一个实在是撇脚的程序,后来领导趁着工作之余给我大致讲解了几个函数的用法,然后简单的改了一下,程序就像突然有了灵魂一样,简单好用,从那时起就对labview产生了兴趣,一发不可收拾,那时经常一个人熬夜学习,现在已然没有那种动力了,哎,年轻就是好。接下来我们为学生类创建一个对象,新建一个VI,打开程序框图,直接将我们项目中的类拖到程序框图,它就成为一个实实在在的对象,我们将其命名为studentA。 阅读详情

Valid Sudoku (M)

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

在这里插入图片描述
A partially filled sudoku which is valid.

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

Example 1:

Input:
[
  ["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"]
]
Output: true

Example 2:

Input:
[
  ["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"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being 
    modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • Only the filled cells need to be validated according to the mentioned rules.
  • The given board contain only digits 1-9 and the character '.'.
  • The given board size is always 9x9.

题意

判断一个数独中已经填入的数字是否有效。

思路

直接对每一个数字判断是否有效,可以直接实现,也可以借助hash实现。


代码实现 - 无hash

class Solution {
    public boolean isValidSudoku(char[][] board) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c = board[i][j];
                if (c != '.' && !isValid(board, i, j, c)) {
                    return false;
                }
            }
        }

        return true;
    }
    
    private boolean isValid(char[][] board, int i, int j, char c) {
        for (int k = 0; k < 9; k++) {
            if (k != j && c == board[i][k] || k != i && c == board[k][j]) {
                return false;
            }
        }
		
        // x、y的初始值为对应九宫格左上顶点的坐标
        for (int x = 3 * (i / 3); x < 3 * (i / 3) + 3; x++) {
            for (int y = 3 * (j / 3); y < 3 * (j / 3) + 3; y++) {
                if (c == board[x][y] && x != i && y != j) {
                    return false;
                }
            }
        }

        return true;
    }
}

代码实现 - hash

class Solution {
    public boolean isValidSudoku(char[][] board) {
        List<Set<Character>> row = new ArrayList<>();
        List<Set<Character>> col = new ArrayList<>();
        List<Set<Character>> nine = new ArrayList<>();

        for (int i = 0; i < 9; i++) {
            row.add(new HashSet<>());
            col.add(new HashSet<>());
            nine.add(new HashSet<>());
        }

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c = board[i][j];
                if (c != '.') {
                    if (!row.get(i).add(c)) {
                        return false;
                    }
                    if (!col.get(j).add(c)) {
                        return false;
                    }
                    // 九宫格序号的计算公式为 3 * (i / 3) + j / 3
                    if (!nine.get(3 * (i / 3) + j / 3).add(c)) {
                        return false;
                    }
                }
            }
        }

        return true;
    }
}
基于Simulink的电机驱动逆变器仿真 本文完成了电机驱动逆变器的Simulink多层级仿真平台搭建,实现了:✅ 构建了从理想到器件级的逆变器模型体系✅ 实现了Si/SiC损耗精确计算与结温预测✅ 建模了母线寄生参数、死区效应、栅极驱动等关键非理想特性✅ 支持故障注入与保护策略验证✅ 集成了热管理、EMI、寿命预测与碳足迹评估。 阅读详情

相关推荐

MATLAB AppDesigner 中TextArea保留原有信息并换行显示提示信息

App Designer 中的文本区域TextArea组件,一般可以用来作为matlab app设计中的信息显示。 app.TextArea.Value = 'Hello Matlab'; Value可以是字符向量、字符向量元胞数组、字符串数组或一维分类数组。这样直接给TextArea赋值,每次赋值都会消除先前的信息,然后显示新的信息。如果想要在app实现保留原有信息、多行信息的输出,在MathWorks官方帮助中心给出了一种方法: cellArrayText{1} = sprintf('%s\n

我是麻瓜喔的博客 1万+

LeetCode:M-36. Valid Sudoku

Link 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 p

iyangdi的博客 331

[Windows] 发票识别工具。支持xml、pdf、ofd文件。

发票识别是财务自动化中的关键环节,支持多种格式(XML、PDF、OFD)的识别工具能显著提升工作效率。以下介绍基于Windows平台开发此类工具的技术方案。发票识别是财务自动化中的关键环节,支持多种格式(XML、PDF、OFD)的识别工具能显著提升工作效率。以下介绍基于Windows平台开发此类工具的技术方案。

pZC_dqux的博客 666

有效的数独 python_[LeetCode in Python] 36 (M) valid sudoku 有效的数独

题目判断一个9x9的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。数字1-9在每一行只能出现一次。数字1-9在每一列只能出现一次。数字1-9在每一个以粗实线分隔的3x3宫内只能出现一次。上图是一个部分填充的有效的数独。数独部分空格内已填入了数字,空白格用'.'表示。示例1:输入:[["5","3",".",".","7",".",".",".","."],["6",".","....

weixin_39844901的博客 69

Obsidian与GPT协作:数学公式从KaTeX到MathJax的无缝转换实践

本文针对Obsidian用户从GPT等AI助手复制数学公式时遇到的KaTeX与MathJax格式不兼容问题,提供了高效的解决方案。重点介绍了KaTeX to MathJax插件的核心作用,它能自动将AI生成的KaTeX格式公式(如\(...\))无缝转换为Obsidian默认支持的MathJax格式($...$),实现复制粘贴后公式即时渲染,极大提升了数学笔记与AI协作的效率和流畅度。

weixin_29209825的博客 136

RTK_ROS_导航(1): GNSS里程计

1. RTK 配置 4G CORS + 4G 网络 + 户外有信号,不能实现RTK,就恢复出厂设置 输出信息包含(一般需要三个同时打开): GAPPA:包含位置信息 GPVTG:包含速度信息 GPHDT:包含定向的朝向信息 2. ROS驱动 驱动安装sudo apt install ros-noetic-nmea-navsat-driver 运行,需要修改该功能包的启动端口,会自动解析上面的3个RTK语句信息roslaunch nmea_navsat_driver nmea_serial_d

旋风小短腿 4265

[leetcode 36] Valid Sudoku

Question: 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 pa

清空 448

解决gradle下载慢的问题

allprojects块的repositories用于多项目构建,为所有项目提供共同所需依赖包。而子项目可以配置自己的repositories以获取自己独需的依赖包。buildScript块的repositories主要是为了Gradle脚本自身的执行,获取脚本依赖插件。根级别的repositories主要是为了当前项目提供所需依赖包。慢的问题在于下载时链接的是官网,而官网下载速度太慢;1、解决gradle不同版本下载慢的问题。但是这个镜像站的版本不全,低版本可以用;有公司做的国内镜像站;

weixin_42551921的博客 2050

[leetcode] 36. Valid Sudoku 解题报告

题目链接:https://leetcode.com/problems/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 fil

小榕流光的专栏 971

Android 13.0 app进程保活白名单功能实现

在13.0的系统rom产品开发中,在某些重要的app即使进入后台,产品需求要求也不想被系统杀掉进程,需要app长时间保活,就是app进程保活白名单功能的实现, 所以需要在系统杀进程的时候不杀掉白名单的进程,接下来就看怎么样来实现这些功能

安卓兼职framework和app工程师的博客 3142

2022年全国职业院校技能大赛网络系统管理赛项模块A:网络构建(样题5)-网络部分解析-附详细代码

2022年全国职业院校技能大赛网络系统管理赛项模块A:网络构建(样题5)-网络部分解析-附详细代码

bfq05234214的博客 415

LeetCode 36 Valid Sudoku

Problem: 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 partiall...

dilian4850的博客 155

扣子(Coze)搭建智能体完整实战指南 —— 从零基础入门到写小说及其他需求扩展开发

随着大语言模型进入应用爆发期,我们正在经历从“寻找提示词”向“构建智能体”的范式转移。扣子(Coze)作为字节跳动推出的一站式AI智能体开发与运营平台,极大地降低了AI应用开发的门槛,让非技术人员也能快速搭建具备感知、决策、行动能力的智能体。 本文将手把手带你从零开始,在扣子平台上搭建一个功能完备的**小说创作智能体**,并基于此展示如何将同一套架构扩展到其他需求场景。无论你是写作爱好者、内容创作者,还是希望用AI赋能日常工作的职场人士,这套完整的技术方案都将帮助你快速上手。

huanghm88的专栏 637

leetcode36. 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 partia

ProgrammingLearner的博客 384

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

【Leetcode】36. Valid Sudoku

36. Valid Sudoku Total Accepted: 85441Total Submissions: 266223Difficulty: Easy Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could

renxingzhadan的专栏 318

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

36.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 ‘.’.判断一个数独板是否是有效的,数独板存在空白格,用’.’表示。#i

不很正派的专栏 489

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Leetcode 36 - 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 ‘.’. class Solution { public: b

蝎心忘情的博客 472

LeetCode 36. 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 fille

SparkSnail 253

Leetcode 36. 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 '.'. 数独的规则是同行,同列,或者是每一个小

Hanwenwangad的专栏 331

Leetcode 36.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 fille

huakaimo的博客 263

LeetCode第36题——valid sudoku

问题描述 Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must co...

apollo_steve的博客 231

【LeetCode 36】 Valid Sudoku

题目描述 Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must co...

iCode_girl的博客 161

Leetcode36 Valid Sudoku

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 ‘.’. No

大气人生 450

[java] 36. Valid Sudoku

同步发表于 http://algorithm.books.mafengshe.com/answer/36.html36. Valid Sudoku题目描述:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where

灵魂画师牧码 888

【LeetCode】36. Valid Sudoku

Valid SudokuDetermine 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 ...

momo_mo520的博客 142

leetcode36 - Valid Sudoku

题目: Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of the ni

i_actor的博客 149
上一篇: 33. Search in Rotated Sorted Array (M)
下一篇: 32. Longest Valid Parentheses (H)
绿色小光头
博客等级 码龄10年 10粉丝 301原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值