题目
类型:数组
解题思路
使用变量 a 代指当前有多少行是满的,使用变量 b 代指当前填充光标所在的位置。
代码
class Solution {
public int[] numberOfLines(int[] widths, String s) {
int a = 0, b = 0;
for (char c : s.toCharArray()) {
int t = widths[c - 'a'];
if (b + t > 100 && ++a >= 0) b = t;
else b += t;
}
if (b != 0) a++;
return new int[]{a, b};
}
}