李恒道
发表于 2024-12-18 18:51:00
https://leetcode.cn/problems/count-submatrices-with-all-ones/submissions/587906655/
这题dp真的很有意思
简单来说将第一个计算只有一行的,第二次计算两行的,第三次三行的
```js
var numSubmat = function (mat) {
let total = 0;
for (let col = 0; col < mat.length; col++) {
for (let index = col; index < mat.length; index++) {
let ans = 0;
for (let indey = 0; indey < mat.length; indey++) {
const num = mat;
if (num == 0) {
ans = 0;
} else {
ans = ans + 1;
}
total += ans;
}
}
for (let index = mat.length - 1; index > 0; index--) {
for (let indey = 0; indey < mat.length; indey++) {
mat &= mat;
}
}
}
return total;
};
```
李恒道
发表于 2024-12-18 19:07:39
https://leetcode.cn/problems/string-to-integer-atoi/submissions/587908846/
没去凹边界极大值情况
讲道理不太喜欢那种模板题
```js
var myAtoi = function (s) {
let result = 0;
let hasPrefix = "";
for (let index = 0; index < s.length; index++) {
const charCode = s.charCodeAt();
const char = s;
if (char == " ") {
if(hasPrefix!==""){
break;
}
continue;
}
if (char == "+" || char == "-") {
if (hasPrefix === "") {
hasPrefix = char == "+" ? 1 : -1;
} else {
break;
}
continue;
}
if (charCode >= 48 && charCode <= 57) {
result = result * 10 + (charCode - 48);
if (hasPrefix === "") {
hasPrefix = 1;
}
} else {
break;
}
if(result>(2**31)&&hasPrefix==-1){
return (2**31)*-1
}
if(result>(2**31-1)&&hasPrefix==1){
return (2**31-1)
}
}
return result * (hasPrefix === "" ? 1 : hasPrefix);
};
```
李恒道
发表于 2024-12-19 17:20:01
https://leetcode.cn/problems/sum-of-subarray-minimums/submissions/588105406/
脑子没想明白
手凹过去了
我草
```js
var sumSubarrayMins = function (arr) {
const left = new Array(arr.length).fill(-1)
const right = new Array(arr.length).fill(-1)
const st = [];
for (let i = 0; i < arr.length; i++) {
const x = arr;
while (st.length && x <= arr]) {
st.pop();
}
if (st.length) {
left = st;
}
st.push(i);
}
st.length = 0;
for (let i = arr.length - 1; i >= 0; i--) {
const x = arr;
while (st.length && x < arr]) {
st.pop();
}
if (st.length) {
right = st;
}
st.push(i);
}
let ans = BigInt(0);
for (let i = 0; i < arr.length; i++) {
let total = 1;
const l = left;
const r = right;
let leftLen=0;
if (l !== -1) {
leftLen += i - l - 1
} else {
leftLen += i
}
let rightLen=0;
if (r !== -1) {
rightLen += r - i - 1
} else {
rightLen += arr.length - i - 1
}
if (leftLen!==0 && rightLen!==0) {
total += leftLen * rightLen
}
total+=leftLen+rightLen
ans +=( BigInt(total) * BigInt(arr))% BigInt(10**9+7)
ans =ans %BigInt(10**9+7)
}
return Number(ans);
}
```
李恒道
发表于 2024-12-19 17:38:28
https://leetcode.cn/problems/surrounded-regions/submissions/588110103/?envType=study-plan-v2&envId=top-interview-150
一波秒
```js
var solve = function (board) {
const dfs = (x, y, target, replace) => {
if (x < 0 || x >= board.length || y < 0 || y >= board.length) {
return
}
if (board == target) {
board = replace
dfs(x + 1, y, target, replace)
dfs(x - 1, y, target, replace)
dfs(x, y + 1, target, replace);
dfs(x, y - 1, target, replace)
}
}
for (let index = 0; index < board.length; index++) {
if (board == "O") {
dfs(0, index, "O", "Z")
}
if (board == "O") {
dfs(board.length - 1, index, "O", "Z")
}
}
for (let index = 0; index < board.length; index++) {
if (board == "O") {
dfs(index, 0, "O", "Z")
}
if (board.length - 1] == "O") {
dfs(index, board.length - 1, "O", "Z")
}
}
for (let index = 0; index < board.length; index++) {
for (let indey = 0; indey < board.length; indey++) {
if (board == "O") {
dfs(index, indey, "O", "X")
}
}
}
for (let index = 0; index < board.length; index++) {
for (let indey = 0; indey < board.length; indey++) {
if (board == "Z") {
dfs(index, indey, "Z", "O")
}
}
}
};
```
李恒道
发表于 2024-12-19 17:52:59
https://leetcode.cn/problems/find-indices-of-stable-mountains/submissions/588113165/
简单题
过
```js
var stableMountains = function(height, threshold) {
let left=height;
let ans=[]
for (let index = 1; index < height.length; index++) {
if(left>threshold){
ans.push(index)
}
left=height
}
return ans
};
```
李恒道
发表于 2024-12-19 18:35:28
https://leetcode.cn/problems/game-of-life/submissions/588119767/?envType=study-plan-v2&envId=top-interview-150
位运算题
秒了
```js
var gameOfLife = function (board) {
const getNear = (x, y) => {
const getPos = (x, y) => {
if (x < 0 || x >= board.length || y < 0 || y > board.length) {
return 0
}
return ((board & 1) == 1) ? 1 : 0
}
return getPos(x + 1, y) + getPos(x - 1, y) + getPos(x, y + 1) + getPos(x, y - 1) +
getPos(x - 1, y - 1) + getPos(x - 1, y + 1) + getPos(x + 1, y - 1) + getPos(x + 1, y + 1)
}
for (let index = 0; index < board.length; index++) {
for (let indey = 0; indey < board.length; indey++) {
let status = board;
const count = getNear(index, indey)
if (status == 1) {
//活细胞
if (count == 2 || count == 3) {
status = status | (1 << 1)
}
} else {
if (count == 3) {
status = status | (1 << 1)
}
}
board = status
}
}
for (let index = 0; index < board.length; index++) {
for (let indey = 0; indey < board.length; indey++) {
board = (board >> 1)
}
}
};
```
李恒道
发表于 2024-12-19 19:02:33
https://leetcode.cn/problems/path-sum/submissions/588123625/?envType=study-plan-v2&envId=top-interview-150
简单题,秒了
```js
var hasPathSum = function (root, targetSum) {
if (root === null) {
return false
}
const dfs = (node, val) => {
val = val - node.val;
if (node.left == null && node.right == null) {
return val == 0
}
return (node.left && dfs(node.left, val)) || (node.right && dfs(node.right, val))||false
}
return dfs(root, targetSum)
};
```
李恒道
发表于 2024-12-20 16:21:03
https://leetcode.cn/problems/minimum-length-of-anagram-concatenation/submissions/588290954/
这题写一半卡住了
边界没搞明白
mark一下
```js
const check = (str, len) => {
let arr1 = new Array(26).fill(0)
for (let index = 0; index < str.length; index += len) {
const arr2 = new Array(26).fill(0)
for (let pos = index; pos < index + len; pos++) {
arr2.charCodeAt() - 97]++
}
const result = arr2.every((val, index) => arr1 == val)
if(index==0||result==true){
arr1=arr2
}else{
return false
}
}
return true
}
var minAnagramLength = function (s) {
for (let index = 1; index < s.length; index++) {
if (s.length % (index) !== 0) {
continue
}
if (check(s, index)) {
return index
}
}
return s.length
};
```
李恒道
发表于 2024-12-20 16:28:53
https://leetcode.cn/problems/single-number-ii/submissions/588293666/?envType=study-plan-v2&envId=top-interview-150
秒了
```js
var singleNumber = function (nums) {
let num = 0
for (let index = 0; index < 32; index++) {
let total = 0;
for (let indey = 0; indey < nums.length; indey++) {
const num = nums;
total += ((num & (1 << index)) == 0) ? 0 : 1
}
num=num|((total%3)<<index)
}
return num
};
```
王一之
发表于 2024-12-20 16:31:59
有排名了吗?