LeetCode1422. Maximum Score After Splitting a String

2024-01-04 12:20

本文主要是介绍LeetCode1422. Maximum Score After Splitting a String,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 一、题目
    • 二、题解

一、题目

Given a string s of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring).

The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring.

Example 1:

Input: s = “011101”
Output: 5
Explanation:
All possible ways of splitting s into two non-empty substrings are:
left = “0” and right = “11101”, score = 1 + 4 = 5
left = “01” and right = “1101”, score = 1 + 3 = 4
left = “011” and right = “101”, score = 1 + 2 = 3
left = “0111” and right = “01”, score = 1 + 1 = 2
left = “01110” and right = “1”, score = 2 + 1 = 3
Example 2:

Input: s = “00111”
Output: 5
Explanation: When left = “00” and right = “111”, we get the maximum score = 2 + 3 = 5
Example 3:

Input: s = “1111”
Output: 3

Constraints:

2 <= s.length <= 500
The string s consists of characters ‘0’ and ‘1’ only.

二、题解

class Solution {
public:int maxScore(string s) {int n = s.size();int maxNum = 0;for(int i = 1;i < n;i++){int leftZero = 0,rightOne = 0;for(int j = 0;j < i;j++) if(s[j] == '0') leftZero++;for(int k = i;k < n;k++) if(s[k] == '1') rightOne++;maxNum = max(maxNum,leftZero+rightOne);}return maxNum;}
};

这篇关于LeetCode1422. Maximum Score After Splitting a String的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/569319

相关文章

string字符会调用new分配堆内存吗

gcc的string默认大小是32个字节,字符串小于等于15直接保存在栈上,超过之后才会使用new分配。

856. Score of Parentheses

856. Score of Parentheses class Solution:def scoreOfParentheses(self, s: str) -> int:stack=[]i=0for c in s:if c=='(':stack.append(c)else:score=0while stack[-1]!='(':score+=stack.pop()stack.pop()score

hdu2072(string的应用)

单词数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 25447    Accepted Submission(s): 5957 Problem Description lily的好朋友xiaoou333最近很空,他

【UVA】10739 - String to Palindrome(动态规划)

比较水的动态规划 dp[i][j] 将原串 i ~ j 之内的字符转化为回文字符所需要的最小操作次数 其中删除操作和添加操作本质上是一样的。 三个状态转移方程: dp[i][j] = min(dp[i][j] ,dp[i + 1][j]); dp[i][j] = min(dp[i][j] ,dp[i + 1][j - 1]); dp[i][j] = min(dp[i][j] ,dp[

理解String的compareTo()方法返回值

compareTo()的返回值是整型,它是先比较对应字符的大小(ASCII码顺序), 如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值。 如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符作比较, 以此类推,直至比较的字符或被比较的字符有一方全比较完,这时就比较字符的长度。 我们可以通过阅读源码加深对compareTo()的理解: comp

【JavaScript】基本数据类型与引用数据类型区别(及为什么String、Boolean、Number基本数据类型会有属性和方法?)

基本数据类型   JavaScript基本数据类型包括:undefined、null、number、boolean、string。基本数据类型是按值访问的,就是说我们可以操作保存在变量中的实际的值。 1)基本数据类型的值是不可变的 任何方法都无法改变一个基本类型的值,比如一个字符串: var name = "change";name.substr();//hangconsole.log

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

leetcode#541. Reverse String II

题目 Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of

Java中Map取值转String Null值处理

Map<String, Object> 直接取值转String String value = (String)map.get("key") 当map.get(“key”)为Null值时会报错。 使用String类的valueOf静态方法可以解决这个问题 String value = String.valueOf(map.get("key"))