zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1221. 分割平衡字符串 | Split a String in Balanced Strings

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(let_us_code)
    ➤博主域名:https://www.zengqiang.org
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/11627001.html
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    Balanced strings are those who have equal quantity of 'L' and 'R' characters.

    Given a balanced string s split it in the maximum amount of balanced strings.

    Return the maximum amount of splitted balanced strings.

    Example 1:

    Input: s = "RLRRLLRLRL"
    Output: 4
    Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.
    Example 2:

    Input: s = "RLLLLRRRLR"
    Output: 3
    Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'.
    Example 3:

    Input: s = "LLLLRRRR"
    Output: 1
    Explanation: s can be split into "LLLLRRRR".

    Constraints:

    1 <= s.length <= 1000
    s[i] = 'L' or 'R'


    在一个「平衡字符串」中,'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'


    0ms

     1 class Solution {
     2     func balancedStringSplit(_ s: String) -> Int {
     3         if s.isEmpty { return 0 }
     4         
     5         var r = 0
     6         var l = 0
     7         var count = 0
     8         var arrayString = Array(s)
     9         for var i in 0..<arrayString.count {
    10             if arrayString[i] == "L" {
    11                 l += 1
    12             } else {
    13                 r += 1
    14             }
    15             
    16             if r == l {
    17                 count += 1
    18             }
    19         }
    20         return count
    21     }
    22 }

    4ms

     1 class Solution {
     2     func balancedStringSplit(_ s: String) -> Int {
     3         var balance = 0
     4         var count = 0 
     5         
     6         for cha in s {
     7             if cha == "R" {
     8                 balance += 1
     9             } else {
    10                 balance -= 1
    11             }
    12             
    13             if balance == 0 {
    14                 count += 1
    15             }
    16         }
    17         
    18         return count
    19     }
    20 }

    8ms

     1 class Solution {
     2     func balancedStringSplit(_ s: String) -> Int {
     3         var b = 0
     4         var ans = 0
     5         for c in s {
     6             if c == "L" {
     7                 b -= 1
     8             } else {
     9                 b += 1
    10             }
    11             if b == 0 {
    12                 ans += 1
    13             }
    14         }
    15         return ans
    16     }
    17 }

    12ms

     1 class Solution {
     2     func balancedStringSplit(_ s: String) -> Int {
     3         var counter = 0
     4         var balancedStrings = [String]()
     5         var balancedString = ""
     6         for char in s {
     7             var val = char == "R" ? 1 : -1
     8             counter += val
     9             
    10             if counter == 0 {
    11                 balancedStrings.append(balancedString)
    12                 balancedString = ""
    13             } else {
    14                 balancedString += String(char)
    15             }
    16         }
    17         return balancedStrings.count
    18     }
    19 }
  • 相关阅读:
    浅谈ajax
    textsizeadjust属性
    vue通信
    json、jsonp的定义和区别
    js中的eval()和catch()
    WebRTC之Android客户端
    人工神经网络(从原理到代码) Step 01 感知器 梯度下降
    简单实用的matlab柱状图显示比例及计数
    JAVA 常用包
    设置MySQL允许远程连接
  • 原文地址:https://www.cnblogs.com/strengthen/p/11627001.html
Copyright © 2011-2022 走看看