当前位置:网站首页>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>边栏推荐
- 关于二叉树
- Matplotlib plt Hist() parameter explanation
- SYSTEMd debugging
- Design of Distributed Message Oriented Middleware
- 二叉搜索树 230. 二叉搜索树中第K小的元素 1038. 从二叉搜索树到更大和树
- Effective self summary of remote communication | community essay solicitation
- error: C2665: “QMessageBox::critical”: 4 个重载中没有一个可以转换所有参数类型
- High performance and high availability computing architecture of "Weibo comments"
- MetaQ集群安装测试
- Welcome the "top ten" of the Municipal Association for science and technology • pay tribute to Lu Yi, a scientific and technological worker: an explorer guarding the transmission security of the power
猜你喜欢

机器学习:VC维的概念和用途

Procurement intelligence is about to break out, and the "3+2" system of Alipay helps enterprises build core competitive advantages

error: C2665: “QMessageBox::critical”: 4 个重载中没有一个可以转换所有参数类型

Gracefully transform the SMS business module and start the strategic mode!

How to solve the problem that the computer time is not automatically updated after proofreading

Software testing interface testing postman testing tool interface testing process execution interface testing interface associated environment variables and global variables built-in dynamic parameter

Qdomdocument and qdomnode are used in QT to read XML

matplotlib matplotlib中plt.hist()参数解释

Intranet penetration (NC)

Constexpr function
随机推荐
动态代理的实现原理
基于OpenStack的虚拟机在线迁移
Collection! Have you ever used these tools to improve programmer productivity?
Paper writing tool: latex online website
采购数智化爆发在即,支出宝“3+2“体系助力企业打造核心竞争优势
sql刷题595. 大的国家
Matplotlib histogram of Matplotlib visualization plt bar()
How can the local / park do a good job in industrial analysis?
收藏!这些提高程序员生产力的工具你用过吗?
RRDtool 画MRTG Log数据
海外数字身份验证服务商ADVANCE.AI入选EqualOcean《2022品牌出海服务市场研究报告》
matplotlib matplotlib可视化之柱状图plt.bar()
Cacti最大监控数测试
動態代理的實現原理
Uniapp copy contents to clipboard
剑指 Offer 14- I. 剪绳子
Wechat applet: (update) cloud development wechat group contacts
Hematemesis finishing: a rare map of architects!
Wechat applet: big red festive UI guessing lantern riddles is also called guessing character riddles
25 interview questions about Apache