1221. Split a String in Balanced

2020-03-04  本文已影响0人  30岁每天进步一点点

附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

上一篇下一篇

猜你喜欢

热点阅读