当前位置:网站首页>LC:有效的数独 + 旋转图像
LC:有效的数独 + 旋转图像
2022-06-29 23:24:00 【MyDreamingCode】
一、有效的数独
请你判断一个 9 x 9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。
注意:
一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
空白格用 '.' 表示。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var 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"]];
var isValidSudoku = function(board) {
var temp;
// 从行开始进行判断
for(var i=0;i<9;i++){
temp=[];
for(var j=0;j<9;j++){
if(board[i][j]!='.'){
if(temp[board[i][j]-'1'])
return false;
else
temp[board[i][j]-'1']=true;
}
}
}
// 从列开始进行判断
for(var i=0;i<9;i++){
temp=[];
for(var j=0;j<9;j++){
if(board[j][i]!='.'){
if(temp[board[j][i]-'1'])
return false;
else
temp[board[j][i]-'1']=true;
}
}
}
// 从3×3宫格进行判断
for(var i=0;i<9;i=i+3)
for(var j=0;j<9;j=j+3){
temp=[];
for(var m=i;m<i+3;m++)
for(var n=j;n<j+3;n++){
if(board[m][n]!='.'){
if(temp[board[m][n]-'1'])
return false;
else
temp[board[m][n]-'1']=true;
}
}
}
return true;
}
console.log(isValidSudoku(board)); //true
</script>
</body>
</html>二、旋转图像
给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。
你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
思路:先上下置换再对角线调换元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var matrix = [[1,2,3],[4,5,6],[7,8,9]];
var temp;
// 上下置换
for(var i=0;i<matrix.length/2 ;i++)
for(var j=0;j<matrix.length;j++){
temp=matrix[i][j];
matrix[i][j]=matrix[matrix.length-i-1][j];
matrix[matrix.length-i-1][j]=temp;
}
// 对角线元素对换
for(var i=0;i<matrix.length;i++)
for(var j=0;j<i;j++){
temp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
console.log(matrix); //[[7,4,1],[8,5,2],[9,6,3]]
</script>
</body>
</html>边栏推荐
- 软件测试 接口测试 Postman测试工具 接口测试的流程 执行接口测试 接口关联 环境变量和全局变量 内置动态参数以及自动有的动态参数
- Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
- 数据库-玩转数据-Pgsql 使用UUID做主键
- Constexpr function
- How to solve the problem that the computer time is not automatically updated after proofreading
- 25 interview questions about Apache
- Head pressing Amway good-looking and practical dispensing machine SolidWorks model material here
- 开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- Procurement intelligence is about to break out, and the "3+2" system of Alipay helps enterprises build core competitive advantages
- RRDtool 画MRTG Log数据
猜你喜欢

采购数智化爆发在即,支出宝“3+2“体系助力企业打造核心竞争优势

微博系统中”微博评论“的高性能高可用计算架构

Node data collection and remote flooding transmission of label information

简单理解B树和B+树

FPGA开发(1)——串口通信

剑指 Offer 14- I. 剪绳子

Incluxdb time series database system

nrm详解

Qdomdocument and qdomnode are used in QT to read XML

Wireshark data analysis and forensics information pacapng
随机推荐
Head on Amway! Good looking and practical motor SolidWorks model material see here
Leetcode 1385. 两个数组间的距离值
大学里遗憾的事,希望你无怨也无悔
Remember the process of checking online MySQL deadlock. You should not only know curd, but also know the principle of locking
穿越过后,她说多元宇宙真的存在
GWD: rotating target detection based on Gaussian Wasserstein distance | ICML 2021
Halcon实用:焊点检出设计思路
InfluxDB时序数据库系统
语音信号处理(三):语音信号分析【连续的“模拟信号”--采样、量化、编码-->离散的“数字信号”】
Fund information disclosure
Solr basic operation 1
Some of my favorite websites
疫情下我离职一年,收入增长了10倍
On binary tree
Mysql database: the difference between drop, truncate and delete
Go zero micro Service Practice Series (VII. How to optimize such a high demand)
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
Discussion on distributed unique ID generation scheme
Under the epidemic, I left my job for a year, and my income increased 10 times
Solr基础操作1