전체 글229 Split a String in Balanced Strings 문제 링크 리트코드에서 본 모법답안 public int balancedStringSplit(String s) { int res = 0, cnt = 0; //res는 나눠질 수 있는 갯수, cnt는 L이 있으면 ++, R이면 --. for (int i = 0; i < s.length(); ++i) { cnt += s.charAt(i) == 'L' ? 1 : -1; if (cnt == 0) ++res; //cnt가 0인 경우는 R과 L의 갯수가 같은 경우, 즉 나줘질 수 있을 때 밖에 없음. } return res; } 위 코드의 경우, 갯수를 초기화 할 필요도 없고 변수를 두개나 할 필요도 없음. 답지 보기 전에 푼 거. class Solution { public int balancedStringSplit.. 2021. 4. 6. 이전 1 ··· 36 37 38 39 다음