美文网首页
leetcode 942 DI String Match

leetcode 942 DI String Match

作者: 点一下我的id | 来源:发表于2019-04-22 20:36 被阅读0次

我竟花了好长一段时间,ac之后百度了下,基本方法都一样。
思路:初始化I为0,D为S的长度,遇到'I'则I++,遇到'D'则D--。

    public int[] diStringMatch(String S) {
        int N = S.length() + 1;
        int[] A = new int[N];

        int I=0,D=S.length();
        for (int i = 0; i < S.length(); i++) {
            if(S.charAt(i)=='I'){
                A[i]=I++;
            }else {
                A[i]=D--;
            }

        }
        A[N-1]=D;
        return A;
    }

相关文章

网友评论

      本文标题:leetcode 942 DI String Match

      本文链接:https://www.haomeiwen.com/subject/zuzugqtx.html