当前位置:网站首页>2021-12-25: given a string s consisting only of 0 and 1, assume that the subscript is from
2021-12-25: given a string s consisting only of 0 and 1, assume that the subscript is from
2022-06-23 21:14:00 【Fuda scaffold constructor's daily question】
2021-12-25: Given a given one, only 0 and 1 Composed string S, Suppose the subscript is from 1 Start , Regulations i Character value of position Vi The calculation method is as follows :
1 i == 1 when ,Vi = 1;
2 i > 1 when , If Si != Si-1,Vi = 1;
3 i > 1 when , If Si == Si-1,Vi = Vi-1 + 1.
You can delete it at will S The characters in , Back to the whole S Maximum value of ,
String length <=5000.
From Tencent .
answer 2021-12-25:
recursive . Try the model from left to right .
At present index The character of the position is reserved ; At present index The character of position is not reserved . The maximum of these two cases .
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
ret := max1("000001100000")
fmt.Println(ret)
}
func max1(s string) int {
if len(s) == 0 {
return 0
}
str := []byte(s)
arr := make([]int, len(str))
for i := 0; i < len(arr); i++ {
if str[i] == '0' {
} else {
arr[i] = 1
}
}
return process1(arr, 0, 0, 0)
}
// Recursive meaning :
// Currently in arr[index...] Make a choice on , str[index...] Left side , The latest figure is lastNum
// also lastNum Value brought , Has been pulled up to baseValue
// Back in the str[index...] Make a choice on , The ultimate maximum value
// index -> 0 ~ 4999
// lastNum -> 0 or 1
// baseValue -> 1 ~ 5000
// 5000 * 2 * 5000 -> 5 * 10^7( too !)
func process1(arr []int, index, lastNum, baseValue int) int {
if index == len(arr) {
return 0
}
curValue := 0
if lastNum == arr[index] {
curValue = baseValue + 1
} else {
curValue = 1
}
// At present index The character of the position is reserved
next1 := process1(arr, index+1, arr[index], curValue)
// At present index The character of position is not reserved
next2 := process1(arr, index+1, lastNum, baseValue)
return getMax(curValue+next1, next2)
}
func getMax(a, b int) int {
if a > b {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- SAP retail uses transaction code wb03 to display a factory configured in the background
- Making CSR file for face core
- Use of pathinfo/pathname in nodejs
- How to build Tencent cloud game server? Differences between cloud game platforms and ordinary games
- Customize view to imitate today's headlines and like animation!
- The element of display:none cannot get offsetwidth and offsetHeight
- How to solve the problem of large traffic audio audit? What are the common approval methods?
- 上线项目之局域网上线软件使用-----phpStudy
- QPS fails to go up due to frequency limitation of public network CLB bandwidth
- Global and Chinese market of gas fire pit 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

JS advanced programming version 4: generator learning
Application of JDBC in performance test

3000 frame animation illustrating why MySQL needs binlog, redo log and undo log

New SQL syntax quick manual!

How to view the role of PMO in agile organizations?

Four aspects of PMO Department value assessment

How PMO uses two dimensions for performance appraisal

How does PMO select and train project managers?

How to gradually improve PMO's own ability and management level

I am 30 years old, no longer young, and have nothing
随机推荐
100 lines of code, using pyGame to make a snake game!
What is the difference between object storage and cloud disk service? What are the functions of cloud disk service?
What is the role of short video AI intelligent audit? Why do I need intelligent auditing?
Application of JDBC in performance test
Install bitgarden open source password manager
Using asp Net core MVC framework for building web applications
. NET Framework . Net core and Net standard
Can Tencent cloud disk service share data? What are the advantages of cloud disk service?
JS regular ignore case
Cobalt Strike Spawn & Tunnel
Newbeecoder. UI new open source control library DataGrid instructions
How to deal with unclear pictures? How to deal with color balance?
Pathname attribute of link a object
[5 minutes to play lighthouse] quickly use Chang'an chain
[development skills] how to add "live broadcast" status display bar on easynvr platform
Global and Chinese market of American football catch gloves 2022-2028: Research Report on technology, participants, trends, market size and share
How do I view the server when I log in to the fortress machine? Operation guide for novice
Script tag attributes and & lt; noscript&gt; label
Sharelist supports simultaneous mounting of Google drive/onedrive multiple network disks
What is the difference between a database and a cloud disk drive? What functions can cloud disk drives achieve?