Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 861c770

Browse files
Merge pull request youngyangyang04#2866 from twoliang/master
0151.翻转字符串里的单词、44. 开发商购买土地(卡码网):go版本解法、738.调递增的数字:原go版本答案有错误,已更正
2 parents 5cd1eec + aa6f361 commit 861c770

File tree

3 files changed

+141
-17
lines changed

3 files changed

+141
-17
lines changed

‎problems/0151.翻转字符串里的单词.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,46 @@ func reverse(b []byte) {
639639
}
640640
}
641641
```
642-
642+
```go
643+
//双指针解法。指针逆序遍历,将遍历后得到的单词(间隔为空格,用以区分)顺序放置在额外空间
644+
//时间复杂度O(n),空间复杂度O(n)
645+
func reverseWords(s string) string {
646+
strBytes := []byte(s)
647+
n := len(strBytes)
648+
// 记录有效字符范围的起始和结束位置
649+
start, end := 0, n-1
650+
// 去除开头空格
651+
for start < n && strBytes[start] == 32 {
652+
start++
653+
}
654+
// 处理全是空格或空字符串情况
655+
if start == n {
656+
return ""
657+
}
658+
// 去除结尾空格
659+
for end >= 0 && strBytes[end] == 32 {
660+
end--
661+
}
662+
// 结果切片,预分配容量
663+
res := make([]byte, 0, end-start+1)//这里挺重要的,本人之前没有预分配容量,每次循环都添加单词,导致内存超限(也可能就是我之前的思路有问题)
664+
// 从后往前遍历有效字符范围
665+
for i := end; i >= start; {
666+
// 找单词起始位置,直接通过循环条件判断定位
667+
for ; i >= start && strBytes[i] == 32; i-- {
668+
}
669+
j := i
670+
for ; j >= start && strBytes[j]!= 32; j-- {
671+
}
672+
res = append(res, strBytes[j+1:i+1]...)
673+
// 只在不是最后一个单词时添加空格
674+
if j > start {
675+
res = append(res, 32)
676+
}
677+
i = j
678+
}
679+
return string(res)
680+
}
681+
```
643682

644683

645684
### JavaScript:

‎problems/0738.单调递增的数字.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -273,22 +273,20 @@ class Solution:
273273
### Go
274274
```go
275275
func monotoneIncreasingDigits(n int) int {
276-
s := strconv.Itoa(N)//将数字转为字符串,方便使用下标
277-
ss := []byte(s)//将字符串转为byte数组,方便更改。
278-
n := len(ss)
279-
if n <= 1 {
280-
return n
281-
}
282-
for i := n-1; i > 0; i-- {
283-
if ss[i-1] > ss[i] { //前一个大于后一位,前一位减1,后面的全部置为9
284-
ss[i-1] -= 1
285-
for j := i; j < n; j++ { //后面的全部置为9
286-
ss[j] = '9'
287-
}
288-
}
289-
}
290-
res, _ := strconv.Atoi(string(ss))
291-
return res
276+
s := strconv.Itoa(n)
277+
// 从左到右遍历字符串,找到第一个不满足单调递增的位置
278+
for i := len(s) - 2; i >= 0; i-- {
279+
if s[i] > s[i+1] {
280+
// 将该位置的数字减1
281+
s = s[:i] + string(s[i]-1) + s[i+1:]
282+
// 将该位置之后的所有数字置为9
283+
for j := i + 1; j < len(s); j++ {
284+
s = s[:j] + "9" + s[j+1:]
285+
}
286+
}
287+
}
288+
result, _ := strconv.Atoi(s)
289+
return result
292290
}
293291
```
294292

@@ -447,3 +445,4 @@ public class Solution
447445
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
448446
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
449447
</a>
448+

‎problems/kamacoder/0044.开发商购买土地.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,89 @@ int main()
527527
}
528528

529529
```
530+
531+
### Go
532+
533+
前缀和
534+
535+
```go
536+
package main
537+
538+
import (
539+
"fmt"
540+
"os"
541+
"bufio"
542+
"strings"
543+
"strconv"
544+
"math"
545+
)
546+
547+
func main() {
548+
var n, m int
549+
550+
reader := bufio.NewReader(os.Stdin)
551+
552+
line, _ := reader.ReadString('\n')
553+
line = strings.TrimSpace(line)
554+
params := strings.Split(line, " ")
555+
556+
n, _ = strconv.Atoi(params[0])
557+
m, _ = strconv.Atoi(params[1])//n和m读取完成
558+
559+
land := make([][]int, n)//土地矩阵初始化
560+
561+
for i := 0; i < n; i++ {
562+
line, _ := reader.ReadString('\n')
563+
line = strings.TrimSpace(line)
564+
values := strings.Split(line, " ")
565+
land[i] = make([]int, m)
566+
for j := 0; j < m; j++ {
567+
value, _ := strconv.Atoi(values[j])
568+
land[i][j] = value
569+
}
570+
}//所有读取完成
571+
572+
//初始化前缀和矩阵
573+
preMatrix := make([][]int, n+1)
574+
for i := 0; i <= n; i++ {
575+
preMatrix[i] = make([]int, m+1)
576+
}
577+
578+
for a := 1; a < n+1; a++ {
579+
for b := 1; b < m+1; b++ {
580+
preMatrix[a][b] = land[a-1][b-1] + preMatrix[a-1][b] + preMatrix[a][b-1] - preMatrix[a-1][b-1]
581+
}
582+
}
583+
584+
totalSum := preMatrix[n][m]
585+
586+
minDiff := math.MaxInt32//初始化极大数,用于比较
587+
588+
//按行分割
589+
for i := 1; i < n; i++ {
590+
topSum := preMatrix[i][m]
591+
592+
bottomSum := totalSum - topSum
593+
594+
diff := int(math.Abs(float64(topSum - bottomSum)))
595+
if diff < minDiff {
596+
minDiff = diff
597+
}
598+
}
599+
600+
//按列分割
601+
for j := 1; j < m; j++ {
602+
topSum := preMatrix[n][j]
603+
604+
bottomSum := totalSum - topSum
605+
606+
diff := int(math.Abs(float64(topSum - bottomSum)))
607+
if diff < minDiff {
608+
minDiff = diff
609+
}
610+
}
611+
612+
fmt.Println(minDiff)
613+
}
614+
```
615+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /