美文网首页
1221. Split a String in Balanced

1221. Split a String in Balanced

作者: 30岁每天进步一点点 | 来源:发表于2020-03-04 11:41 被阅读0次

附leetcode链接:https://leetcode.com/problems/split-a-string-in-balanced-strings/
1221. Split a String in Balanced StringsBalanced 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.

public int balancedStringSplit(String s) {
       int countOfLR = 0;
       int countS = 0;
       for(int i = 0;i<s.length();i++) {
            if(s.charAt(i) == 'R') 
                  countOfLR++;
            else
                  countOfLR--;
            if(countOfLR==0)
                  countS++;
       }
       return countS;
}

小结:用++、--来实现R、L数量的平衡
取字符串的某个字符 s.charAt(i)
char charAt(int index):返回指定索引处的 char 值
附:https://www.runoob.com/java/java-string.html

相关文章

网友评论

      本文标题:1221. Split a String in Balanced

      本文链接:https://www.haomeiwen.com/subject/yahmlhtx.html