李恒道
发表于 2024-9-30 02:50:58
https://leetcode.cn/problems/power-of-two/submissions/569154641/?envType=study-plan-v2&envId=primers-list
基础题
```go
func isPowerOfTwo(n int) bool {
if(n==0){
return false
}
if(n==1){
return true
}
if (n<0){
return false
}
for {
var quotient = n / 2;//商
var remainder = n % 2;//余数
if remainder == 1{
return false
};
if (quotient == 1 && remainder == 0) {
return true
};
n = n / 2;
}
}
```
李恒道
发表于 2024-9-30 02:51:24
https://leetcode.cn/submissions/detail/569148085/
岛屿下沉问题,dfs过
```js
var maxAreaOfIsland = function (grid) {
const dfs = (x, y) => {
if (x >= grid.length || x < 0 || y >= grid.length || y < 0) {
return 0
}
if (grid == 0) {
return 0
}
grid = 0
return dfs(x + 1, y) + dfs(x - 1, y) + dfs(x, y + 1) + dfs(x, y - 1) + 1
}
let maxResult = 0
for (let index = 0; index < grid.length; index++) {
for (let indey = 0; indey < grid.length; indey++) {
const char = grid
if (char == 1) {
const result = dfs(index, indey)
maxResult = Math.max(maxResult, result)
}
}
}
return maxResult
};
```
李恒道
发表于 2024-9-30 02:52:51
https://leetcode.cn/problems/power-of-three/description/?envType=study-plan-v2&envId=primers-list
秒了
```go
func isPowerOfThree(n int) bool {
if(n==0){
return false
}
if(n==1){
return true
}
if (n<0){
return false
}
for {
var quotient = n / 3;//商
var remainder = n % 3;//余数
if (remainder == 1||remainder == 2){
return false
};
if (quotient == 1 && remainder == 0) {
return true
};
n = n / 3;
}
}
```
李恒道
发表于 2024-9-30 03:05:02
https://leetcode.cn/problems/count-number-of-texts/description/
dfs,可以改dp
```js
var countTexts = function (pressedKeys) {
const cache=new Map()
const dfs = (pos) => {
if(cache.has(pos)){
return cache.get(pos)
}
if (pos >= pressedKeys.length) {
return 1;
}
const char = pressedKeys;
let result = 0;
let maxLimt = char == "7" || char == "9" ? 4 : 3;
let times=1;
const oldPos=pos
while (pressedKeys == char&×<=maxLimt) {
result += dfs(pos + 1);
pos++;
times++;
}
result=result% (1000000000 + 7)
cache.set(oldPos,result)
return result
};
return dfs(0);
};
```
李恒道
发表于 2024-9-30 03:10:41
https://leetcode.cn/problems/k-radius-subarray-averages/submissions/569155028/
魔改硬凹过去的
```js
var getAverages = function (nums, k) {
let pos = 0;
let result = [];
let avergeNum = 0;
k = k * 2 + 1;
if(nums.length<k){
return new Array(nums.length).fill(-1)
}
for (let index = 0; index < nums.length; index++) {
const num = nums;
avergeNum += num;
pos++;
if (pos <= k - 1) {
continue;
}
result.push(Math.floor(avergeNum / k));
avergeNum -= nums;
}
while (result.length < nums.length) {
if (nums.length == 1) {
result.push(-1);
break;
}
result.push(-1);
result.unshift(-1);
}
return result;
};
```
李恒道
发表于 2024-9-30 22:23:39
https://leetcode.cn/problems/minimum-recolors-to-get-k-consecutive-black-blocks/submissions/569333398/
直接灵感秒了
```js
var minimumRecolors = function (blocks, k) {
let pos = 0;
let render = 0;
let result = Number.MAX_SAFE_INTEGER
for (let index = 0; index < blocks.length; index++) {
const color = blocks;
if (color == 'W') {
render++;
}
pos++;
if (pos < k) {
continue;
}
if (blocks == 'W') {
render--
}
result = Math.min(result, render)
}
return result
};
```
李恒道
发表于 2024-9-30 22:40:50
https://leetcode.cn/problems/defuse-the-bomb/submissions/569336973/
感觉秒
```js
var decrypt = function (code, k) {
if (k == 0) {
return new Array(code.length).fill(0)
}
if (k > 0) {
const result = new Array(code.length).fill(0)
for (let index = 1; index <= k; index++) {
result += index < code.length ? code : code
}
for (let index = 1; index < result.length; index++) {
const nextNum = (index + k < code.length ? code : code[(index + k) % code.length])
result = result - code + nextNum
}
return result
}
if (k < 0) {
const result = new Array(code.length).fill(0)
for (let index = 1; index <= k*-1; index++) {
result += code
}
for (let index = 1; index < result.length; index++) {
const nextNum = ((index + k-1) >= 0 ? code : code)
result = result + code -nextNum
}
return result
}
};
```
李恒道
发表于 2024-9-30 23:24:17
https://leetcode.cn/problems/count-number-of-ways-to-place-houses/submissions/569343472/
dp秒了
```js
var countHousePlacements = function (n) {
const dp = new Array(n).fill(0).map(() => new Array(4).fill(0));
dp = 1;//不存在房屋
dp = 1;//存在房屋
dp = 1;//上存在下不存在
dp = 1;//上不存在下存在
for (let index = 1; index < dp.length; index++) {
dp = (dp + dp + dp + dp) % (1000000000 + 7)
dp = dp % (1000000000 + 7)
dp = (dp + dp) % (1000000000 + 7)
dp = (dp + dp) % (1000000000 + 7)
}
let result = 0
for (let index = 0; index < 4; index++) {
result+= dp
}
return result% (1000000000 + 7)
};
```
李恒道
发表于 2024-10-1 00:24:34
https://leetcode.cn/problems/merge-strings-alternately/submissions/569350125/?envType=study-plan-v2&envId=programming-skills
简单题压压惊
```go
func mergeAlternately(word1 string, word2 string) string {
var pos int
var result string
for {
if pos >= len(word1) || pos >= len(word2) {
break
}
result += word1 + word2
pos++
}
if pos >= len(word1) {
//拷贝2
result += word2
} else {
//拷贝1
result += word1
}
return result
}
```
李恒道
发表于 2024-10-1 01:21:51
https://leetcode.cn/problems/plus-one/submissions/569353600/?envType=study-plan-v2&envId=programming-skills
有点go的感觉了
但是还是编码不规范
```go
func plusOne(digits []int) []int {
var pos int = len(digits) - 1
digits++
for {
if digits <= 9 {
break
}
digits = digits - 10
pos--
if pos >= 0 {
digits += 1
} else {
digits = append([]int{1}, digits...)
break
}
}
return digits
}
```