附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
网友评论