제목 1221, 저울은 문자열을 분할

I, 자격

는 "평형 문자열"에서, 'L'과 'R'문자의 개수는 동일하다.
균형 잡힌 문자열의 감안할 때, 당신은 많은 균형 문자열 등으로 나눕니다.
반환 값은 평형의 최대 수는 문자열의 부분에 의해 얻어 질 수있다. (1)
그림 삽입 설명 여기

둘째, 아이디어

주의 사항 : 문자열을 균형해야하는 문자열을 분할은 모두

당신에게 균형 잡힌 문자열을 제공하기 위해, 그들은 한 번 차단의 두 번째와 같은 위치에 도달하면 다음 한 수에 의하여 계산 직접 하나를 분할 한 다음 계속 더 0, 1 최악입니다있다.

셋째, 코드

public class T1221 {

    public static void main(String[] args) {
    
        System.out.println( balancedStringSplit("RLRRLLRLRL") );    //4
        System.out.println( balancedStringSplit("RLLLLRRRLR") );    //3
        System.out.println( balancedStringSplit("LLLLRRRR") );    //1
        System.out.println( balancedStringSplit("RLRRRLLRLL") );    //2
    }

    public static int balancedStringSplit(String s) {

        int  i = 0, j = 0, output = 0;
        for ( int count = 0; count < s.length(); count ++ ){
            if ( i == j ){
                i = 0;
                j = 0;
                output++;
            }

            if (s.charAt(count) == 'R' )
                i++;
            else
                j++;
        }

        return output;
    }
}

  1. 출처 : 숙박 버튼 (LeetCode)
    링크 : HTTPS : //leetcode-cn.com/problems/split-a-string-in-balanced-strings
    모든 네트워크에서 공제 저작권. 상업 무단 전재 소스를 표시하시기 바랍니다 승인 된 공식, 비상업적 재판에 문의하시기 바랍니다. ↩︎

게시 24 개 원래 기사 · 원의 칭찬 0 · 조회수 (107)

추천

출처blog.csdn.net/weixin_45980031/article/details/103466343