当前位置:网站首页>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 :
边栏推荐
- . NET Core . NET Framework
- 2021 three "myths" of science and technology industry -- yuancosmos, NFT and web 3
- What is the role of short video AI intelligent audit? Why do I need intelligent auditing?
- Where should DNS start? I -- from the failure of Facebook
- Gin security -3: fast implementation of CSRF verification
- Share a super Mary source code
- Disaster recovery series (VII) -- hybrid cloud public network export disaster recovery construction
- 3000 frame animation illustrating why MySQL needs binlog, redo log and undo log
- Global and Chinese markets of natural starch 2022-2028: Research Report on technology, participants, trends, market size and share
- Is it possible to transfer files on the fortress server? How to operate?
猜你喜欢

How to view the role of PMO in agile organizations?

JS advanced programming version 4: generator learning

How does PMO select and train project managers?

How to gradually improve PMO's own ability and management level
Application of JDBC in performance test

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

Four aspects of PMO Department value assessment

What are the main dimensions of PMO performance appraisal?

I am 30 years old, no longer young, and have nothing

How PMO uses two dimensions for performance appraisal
随机推荐
What are the server host security risks? How to prevent safety risks?
On line project LAN on-line software use ----phpstudy
Global and Chinese market of gas fire pit 2022-2028: Research Report on technology, participants, trends, market size and share
Is it safe to open an online securities account or to go to the business department
What is the role of computer auto audit audio? What content failed to pass the audit?
Install bitgarden open source password manager
What technology is used for video intelligent audit? Difficulties encountered in video audit?
This article introduces you to the necessity of database connection pooling
Global and Chinese market for hydropower plants 2022-2028: Research Report on technology, participants, trends, market size and share
JS delete object attribute
Full instructions for databinding
Encryption and decryption analysis of returned data of an e-commerce app (IV)
Excel text function
Is it safe for Huatai Securities to open an account online for securities companies with low handling fees and commissions
Talk about leap seconds
Which is better for securities companies? I don't understand. Is it safe to open an account online?
How to log in to the server through the fortress machine to transfer files? What are the specific steps?
JS naming conventions
Pathname attribute of link a object
JS five methods to judge whether a certain value exists in an array