2019年9月17日 星期二

[Easy] 806. Number of Lines To Write String

widths為寫入a~z字母個別所需的寬度,每行的上限是100,超過就寫下一行,答案返回寫入S所需的行數和最後一行的寬度。


    public int[] numberOfLines(int[] widths, String S) {
        char ca[] = S.toCharArray();
        int wtmp = 0,ltmp = 1;
        for(char c:ca){
            int ctmp = widths[c-'a'];           
            if(wtmp+ctmp>100){
                ltmp++;
                wtmp=0;
            }
            wtmp+=ctmp;            
        }
        
        int ans[] = {ltmp,wtmp};
        return ans;
    }



沒有留言:

張貼留言