zoukankan      html  css  js  c++  java
  • 1221. 分割平衡字符串

    地址:https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/

    <?php
    /**
    在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的。
    
    给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。
    
    返回可以通过分割得到的平衡字符串的最大数量。
    
     
    
    示例 1:
    
    输入:s = "RLRRLLRLRL"
    输出:4
    解释:s 可以分割为 "RL", "RRLL", "RL", "RL", 每个子字符串中都包含相同数量的 'L' 和 'R'。
    示例 2:
    
    输入:s = "RLLLLRRRLR"
    输出:3
    解释:s 可以分割为 "RL", "LLLRRR", "LR", 每个子字符串中都包含相同数量的 'L' 和 'R'。
    示例 3:
    
    输入:s = "LLLLRRRR"
    输出:1
    解释:s 只能保持原样 "LLLLRRRR".
     
    
    提示:
    
    1 <= s.length <= 1000
    s[i] = 'L' 或 'R'
    分割得到的每个字符串都必须是平衡字符串。
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/split-a-string-in-balanced-strings
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
     */
    
    class Solution {
    
        /**
         * @param String $s
         * @return Integer
         */
        function balancedStringSplit($s) {
            $count = 0;
            $num = 0;
    
            for ($i = 0, $length = strlen($s); $i < $length; $i++) {
                $s[$i] == 'R' ? $count++ : $count--;
                if ($count == 0) {
                    $num++;
                }
            }
    
            return $num;
        }
    }
  • 相关阅读:
    Spring--自定义注解
    IntelliJ IDEA实用插件
    Zero date value prohibited解决方法
    如何保证幂等性
    Map遍历的几种方式
    Static关键字
    索引失效 -- 使用Between范围查询时
    接口的不同写法在Swagger上的不同
    js与jquery获取input输入框中的值
    一个简单的 aiax请求例子
  • 原文地址:https://www.cnblogs.com/8013-cmf/p/13691079.html
Copyright © 2011-2022 走看看