mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -639,7 +639,46 @@ func reverse(b []byte) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```go
|
||||||
|
//双指针解法。指针逆序遍历,将遍历后得到的单词(间隔为空格,用以区分)顺序放置在额外空间
|
||||||
|
//时间复杂度O(n),空间复杂度O(n)
|
||||||
|
func reverseWords(s string) string {
|
||||||
|
strBytes := []byte(s)
|
||||||
|
n := len(strBytes)
|
||||||
|
// 记录有效字符范围的起始和结束位置
|
||||||
|
start, end := 0, n-1
|
||||||
|
// 去除开头空格
|
||||||
|
for start < n && strBytes[start] == 32 {
|
||||||
|
start++
|
||||||
|
}
|
||||||
|
// 处理全是空格或空字符串情况
|
||||||
|
if start == n {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
// 去除结尾空格
|
||||||
|
for end >= 0 && strBytes[end] == 32 {
|
||||||
|
end--
|
||||||
|
}
|
||||||
|
// 结果切片,预分配容量
|
||||||
|
res := make([]byte, 0, end-start+1)//这里挺重要的,本人之前没有预分配容量,每次循环都添加单词,导致内存超限(也可能就是我之前的思路有问题)
|
||||||
|
// 从后往前遍历有效字符范围
|
||||||
|
for i := end; i >= start; {
|
||||||
|
// 找单词起始位置,直接通过循环条件判断定位
|
||||||
|
for ; i >= start && strBytes[i] == 32; i-- {
|
||||||
|
}
|
||||||
|
j := i
|
||||||
|
for ; j >= start && strBytes[j]!= 32; j-- {
|
||||||
|
}
|
||||||
|
res = append(res, strBytes[j+1:i+1]...)
|
||||||
|
// 只在不是最后一个单词时添加空格
|
||||||
|
if j > start {
|
||||||
|
res = append(res, 32)
|
||||||
|
}
|
||||||
|
i = j
|
||||||
|
}
|
||||||
|
return string(res)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### JavaScript:
|
### JavaScript:
|
||||||
|
@ -273,22 +273,20 @@ class Solution:
|
|||||||
### Go
|
### Go
|
||||||
```go
|
```go
|
||||||
func monotoneIncreasingDigits(n int) int {
|
func monotoneIncreasingDigits(n int) int {
|
||||||
s := strconv.Itoa(N)//将数字转为字符串,方便使用下标
|
s := strconv.Itoa(n)
|
||||||
ss := []byte(s)//将字符串转为byte数组,方便更改。
|
// 从左到右遍历字符串,找到第一个不满足单调递增的位置
|
||||||
n := len(ss)
|
for i := len(s) - 2; i >= 0; i-- {
|
||||||
if n <= 1 {
|
if s[i] > s[i+1] {
|
||||||
return n
|
// 将该位置的数字减1
|
||||||
}
|
s = s[:i] + string(s[i]-1) + s[i+1:]
|
||||||
for i := n-1; i > 0; i-- {
|
// 将该位置之后的所有数字置为9
|
||||||
if ss[i-1] > ss[i] { //前一个大于后一位,前一位减1,后面的全部置为9
|
for j := i + 1; j < len(s); j++ {
|
||||||
ss[i-1] -= 1
|
s = s[:j] + "9" + s[j+1:]
|
||||||
for j := i; j < n; j++ { //后面的全部置为9
|
}
|
||||||
ss[j] = '9'
|
}
|
||||||
}
|
}
|
||||||
}
|
result, _ := strconv.Atoi(s)
|
||||||
}
|
return result
|
||||||
res, _ := strconv.Atoi(string(ss))
|
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -447,3 +445,4 @@ public class Solution
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -527,3 +527,89 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Go
|
||||||
|
|
||||||
|
前缀和
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"bufio"
|
||||||
|
"strings"
|
||||||
|
"strconv"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var n, m int
|
||||||
|
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
|
||||||
|
line, _ := reader.ReadString('\n')
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
params := strings.Split(line, " ")
|
||||||
|
|
||||||
|
n, _ = strconv.Atoi(params[0])
|
||||||
|
m, _ = strconv.Atoi(params[1])//n和m读取完成
|
||||||
|
|
||||||
|
land := make([][]int, n)//土地矩阵初始化
|
||||||
|
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
line, _ := reader.ReadString('\n')
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
values := strings.Split(line, " ")
|
||||||
|
land[i] = make([]int, m)
|
||||||
|
for j := 0; j < m; j++ {
|
||||||
|
value, _ := strconv.Atoi(values[j])
|
||||||
|
land[i][j] = value
|
||||||
|
}
|
||||||
|
}//所有读取完成
|
||||||
|
|
||||||
|
//初始化前缀和矩阵
|
||||||
|
preMatrix := make([][]int, n+1)
|
||||||
|
for i := 0; i <= n; i++ {
|
||||||
|
preMatrix[i] = make([]int, m+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for a := 1; a < n+1; a++ {
|
||||||
|
for b := 1; b < m+1; b++ {
|
||||||
|
preMatrix[a][b] = land[a-1][b-1] + preMatrix[a-1][b] + preMatrix[a][b-1] - preMatrix[a-1][b-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
totalSum := preMatrix[n][m]
|
||||||
|
|
||||||
|
minDiff := math.MaxInt32//初始化极大数,用于比较
|
||||||
|
|
||||||
|
//按行分割
|
||||||
|
for i := 1; i < n; i++ {
|
||||||
|
topSum := preMatrix[i][m]
|
||||||
|
|
||||||
|
bottomSum := totalSum - topSum
|
||||||
|
|
||||||
|
diff := int(math.Abs(float64(topSum - bottomSum)))
|
||||||
|
if diff < minDiff {
|
||||||
|
minDiff = diff
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//按列分割
|
||||||
|
for j := 1; j < m; j++ {
|
||||||
|
topSum := preMatrix[n][j]
|
||||||
|
|
||||||
|
bottomSum := totalSum - topSum
|
||||||
|
|
||||||
|
diff := int(math.Abs(float64(topSum - bottomSum)))
|
||||||
|
if diff < minDiff {
|
||||||
|
minDiff = diff
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(minDiff)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user