Java/806. Number of Lines To Write String 写字符串所需要的行数

题目


代码部分(2ms 100%)

class Solution {
    public int[] numberOfLines(int[] widths, String S) {
        int[] res = new int[2];
        char[] ch = S.toCharArray();
        
        int line = 1, sum = 0;
        for(int i = 0; i < ch.length; i++){
            sum += widths[ch[i] - 97];
            if(sum > 100){
                line++;
                sum = widths[ch[i] - 97];
            }
        }
        res[0] = line;
        res[1] = sum;   
       
        
        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38959715/article/details/85545970
今日推荐