增减字符串匹配(力扣题库)

public static void main(String[] args) {

        int i [] =diStringMatch("IDID");
        for (int j = 0; j < i.length; j++) {
            System.out.print(i[j]+"-");
        }
    }

    public static int[] diStringMatch(String S){
        char[] strs=S.toCharArray();
        int[] a= new  int[S.length()+1];
        for (int i = 0; i <= S.length(); i++) {
            a[i]=i;
        }
        int[] A = new int[S.length()+1];
        int i1 = 0;
        int i2 = 0;
        for (int i = 0; i <S.length(); i++) {
            if (strs[i]=='I'){
              A[i]=a[i1];
                i1++;
            }else if (strs[i]=='D'){
                A[i]=a[a.length-i2-1];
                i2++;
            }
        }
        A[a.length-1]=a[i1];
        return A;
    }

猜你喜欢

转载自blog.csdn.net/qq_42903710/article/details/88553825