首页 > 其他分享 >leetcode-1422-easy

leetcode-1422-easy

时间:2023-05-18 20:34:49浏览次数:37  
标签:1422 int chars score right easy ans leetcode left

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.

思路一: 暴力求解

    public int maxScore(String s) {
        char[] chars = s.toCharArray();
        int ans = 0;

        for (int i = 0; i < chars.length - 1; i++) {

            int temp = 0;
            for (int j = 0; j <= i; j++) {
                if (chars[j] == '0') {
                    temp++;
                }
            }

            for (int j = i + 1; j < chars.length; j++) {
                if (chars[j] == '1') {
                    temp++;
                }
            }
            ans = Math.max(ans, temp);
        }

        return ans;
    }

思路二:先统计 1 的个数,然后从左往右遍历,对 0 的个数进行累加,然后对比两者的和,只用两次遍历就可求解

    public int maxScore(String s) {
        char[] chars = s.toCharArray();

        int oneCount = 0;
        for (char c : chars) {
            if (c == '1') {
                oneCount++;
            }
        }

        int zeroCount = 0;
        int ans = 0;
        for (int i = 0; i < chars.length - 1; i++) {
            if (chars[i] == '0') {
                zeroCount++;
            } else {
                oneCount--;
            }
            ans = Math.max(ans, zeroCount + oneCount);
        }

        return ans;
    }

标签:1422,int,chars,score,right,easy,ans,leetcode,left
From: https://www.cnblogs.com/iyiluo/p/17413209.html

相关文章

  • leetcode-1295-easy
    FindNumberswithEvenNumberofDigitsGivenanarraynumsofintegers,returnhowmanyofthemcontainanevennumberofdigits.Example1:Input:nums=[12,345,2,6,7896]Output:2Explanation:12contains2digits(evennumberofdigits).345cont......
  • leetcode-1103-easy
    DistributeCandiestoPeopleWedistributesomenumberofcandies,toarowofn=num_peoplepeopleinthefollowingway:Wethengive1candytothefirstperson,2candiestothesecondperson,andsoonuntilwegivencandiestothelastperson.Th......
  • #yyds干货盘点# LeetCode程序员面试金典:相交链表
    1.简述:给你两个单链表的头节点 headA和headB,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回null。图示两个链表在节点c1开始相交:题目数据保证整个链式结构中不存在环。注意,函数返回结果后,链表必须保持其原始结构。自定义评测:评测系统的输入......
  • #yyds干货盘点# LeetCode程序员面试金典:从中序与后序遍历序列构造二叉树
    题目:给定两个整数数组inorder和postorder,其中inorder是二叉树的中序遍历,postorder是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。 示例1:输入:inorder=[9,3,15,20,7],postorder=[9,15,7,20,3]输出:[3,9,20,null,null,15,7]示例2:输入:inorder=[-1],postorder......
  • 动态规划算法基础及leetcode例题
    01基础理论题型:动规基础(斐波那契数列or爬楼梯);背包问题;打家劫舍;股票问题;子序列问题动规误区:只要看懂递推就ok(递推公式只是一部分)解决动态规划应该要思考的几步:状态转移的DP数组以及下标的含义递推公式DP数组为何初始化遍历顺序打印DP数组02例题基础题目509.斐波那......
  • leetcode:二叉树的最大深度
    题目描述给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,20,null,null,15,7],3/\920/\157返回它的最大深度 3。题目链接:104.二叉树......
  • Leetcode-Easy 806. Number of Lines To Write String
    题目描述给一个字符串S,从左到右将它们排列行,每行最大长度为100,,同时给定一个数组withds,widths[0]对应着a的宽度,widths[1]对应着b的宽度,...,widths[25]对应着z的宽度。求:至少需要多少行以及最后一行的长度下面是一个实例:Example:Input:widths=[10,10,10,10,10,10,1......
  • leetcode 1321 餐館營業額變化增長
    leetcode1321餐館營業額變化增長selectdistinctc2.visited_on,(selectsum(amount)fromCustomerc1wherec1.visited_on<=c2.visited_onandc1.visited_on>=date_sub(c2.visited_on,interval6day))asamount,round((selectsum(amount)fromCustomerc1......
  • 二刷Leetcode-Days04
    数组:/***27.移除元素*@paramnums*@paramval*@return很多考察数组、链表、字符串等操作的面试题,都使用双指针法。*/publicintremoveElement(int[]nums,intval){intleft=0;for(inti=0;i<nums.l......
  • SpringBoot使用EasyExcel将Excel数据直接转换为类对象
    背景相比于读取excel到List<List<String>>对象中,抽象一个方法将excel数据直接一步读取到指定的类对象中,更为方便。代码通过类Class读取excel数据到对象/***使用Class来读取Excel**@paraminputStreamExcel的输入流*@paramexcelTypeEnumExcel的格式(XLS或XLSX......