One Leetcode per day-942. Increase or decrease string matching

Insert picture description here

class Solution {
    
    
    public int[] diStringMatch(String S) {
    
    
        // I 偶数 从小到大分配
        // D 奇数 从大到小分配
        // 排放
        int N = S.length();
        int[] ans = new int[N+1];
        int i = 0;
        int j = N;
        int k = 0;
        while(k<=N+1){
    
    
            if(k>=N){
    
    
                ans[k] = i;
                break;
            }else{
    
    
                if(S.charAt(k)=='I'){
    
    
                    ans[k] = i;
                    i+=1;
                }else if(S.charAt(k)=='D'){
    
    
                    ans[k] = j;
                    j-=1;
                }
            }
            k++;
        }
        return ans;
    }
}

Guess you like

Origin blog.csdn.net/weixin_41041275/article/details/112058957