36、有效的数独
0. 题面
请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
- 数字 1-9 在每一行只能出现一次。
- 数字 1-9 在每一列只能出现一次。
- 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
注意: ● 一个有效的数独(部分已被填充)不一定是可解的。 ● 只需要根据以上规则,验证已经填入的数字是否有效即可。 ● 空白格用 '.' 表示。 示例 1:
输入: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"]]
输出:true
示例 2:
输入:board =
[["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"]]
输出:false
解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例 1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
提示: ● board.length == 9 ● board[i].length == 9 ● board[i][j] 是一位数字(1-9)或者 '.'
解法
/**
@param {character[][]} board
@return {boolean}
*/
var isValidSudoku = function (board) {
const set = new Set();
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
const number = board[i][j];
if (number === '.') {
continue;
}
const colStr = `${number}c${i}`;
const rowStr = `${number}r${j}`;
const sectionStr = `${number}s(${Math.floor(i / 3)},${Math.floor(j / 3)})`;
if (set.has(colStr) || set.has(rowStr) || set.has(sectionStr)) {
return false;
}
set.add(colStr);
set.add(rowStr);
set.add(sectionStr);
}
}
return true;
};
思路:遍历整个数组。
因数独游戏特性可知,该数字需要满足:
在该行仅出现一次 --> colStr = ${number}c${i};
在该列仅出现一次 --> rowStr = ${number}r${j};
在该 3*3 区块仅出现一次 --> sectionStr = ${number}s(${Math.floor(i/3)},${Math.floor(j/3)});
创建 set 用于保存上述未出现的字符串,如果重复出现则为无效数独。
42、接雨水
0. 题面
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例 2:
输入: height = [4,2,0,3,2,5]
输出: 9
分析:计算每个柱子上方你能装多少水,相加即可 每个柱子能装多少水,取决于左侧最高柱子和右侧的最高柱子(取两个中矮的那个),类似于木桶原理
解法 1: 暴力解,穷举
function trap(height = []) {
let res = 0;
if (height.length === 0) {
return res;
}
for (let i = 1; i < height.length - 1; i++) {
let l_max = 0;
let r_max = 0;
for (let j = i; j < height.length; j++) {
// 找出右边最高的柱子
r_max = Math.max(r_max, height[j]);
}
for (let j = i; j >= 0; j--) {
// 找出左边最高的柱子
l_max = Math.max(l_max, height[j]);
}
res += Math.min(l_max, r_max) - height[i]; // 取左右最高柱子的小的那个,减去自己的高度
}
return res;
}
时间复杂度:
O(N ^ 2),两层嵌套循环
空间复杂度:O(1),只有一个个的变量
解法 2:优化穷举法
function trap(height = []) {
let res = 0;
const n = height.length;
if (n === 0) {
return res;
}
const l_max = new Array(n);
const r_max = new Array(n);
l_max[0] = height[0];
r_max[n - 1] = height[n - 1];
// 计算 l_max,从左到右
for (let i = 1; i < n; i++) {
l_max[i] = Math.max(l_max[i - 1], height[i]);
}
// 计算 r_max,从右到左
for (let i = n - 2; i >= 0; i--) {
r_max[i] = Math.max(r_max[i + 1], height[i]);
}
for (let i = 1; i < n - 1; i++) {
res += Math.min(l_max[i], r_max[i]) - height[i]; // 取左右最高柱子的小的那个,减去自己的高度
}
return res;
}
这个优化其实和暴力解法思路差不多,就是避免了重复计算
把时间复杂度降为O(N)
但是空间复杂度也提升到了O(N),因为创建了n长度的数组
解法 3:双指针
function trap(height = []) {
let res = 0;
if (height.length === 0) {
return res;
}
const n = height.length;
// 定义两个指针
let left = 0; // 左侧指针从 0 开始往右移动
let right = n - 1; // 左侧指针从 n - 1 开始往左移动
let l_max = height[0];
let r_max = height[n - 1];
while (left <= right) {
l_max = Math.max(l_max, height[left]); // 当前左侧指针的值跟左侧最大值比较,计算出当前左侧的最大值
r_max = Math.max(r_max, height[right]); // 当前右侧指针正的值跟右侧最大值比较,计算出当前右侧的最大值
if (l_max < r_max) {
// 木桶原理,左边柱子矮,所以能接的水以左边为准,然后右边指针不动,左边指针往右前进一位
res += l_max - height[left];
left++;
} else {
// 右边柱子矮,所以能接的水以右边为准,然后左边指针不动,右边指针往左前进一位
res += r_max - height[right];
right--;
}
}
return res;
}
时间复杂度:
O(N)空间复杂度:O(1)