1. 程式人生 > >Java/806. Number of Lines To Write String 寫字串所需要的行數

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;
    }
}