1011. 字符串写入的行数

描述

把字符串S中的字符从左到右写入行中。 每行最大宽度度为100,如果往后新写一个字符导致该行宽度超过100,则写入下一行。
注意:一个字符的宽度不为1!给定一个数组widths,其中widths[0]是字符a的宽度,widths[1]是字符b的宽度,...,widths[25]是字符'z'的宽度。

问:把S全部写完,至少需要多少行?最后一行用去的宽度是多少? 将结果作为长度为2的整数列表返回。

  • S的长度范围为[1, 1000].
  • S仅有26个小写字母组成.
  • widths的长度为26.
  • widths[i]的范围为[2, 10].

样例

样例1 :

输入: 
widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "abcdefghijklmnopqrstuvwxyz"
输出: [3, 60]
解释: 
每个字符的宽度都是10. 为了把这26个字符都写进去,需要两个整行和一个用去60长度的行。

样例2:

输入: 
widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "bbbcccdddaaa"
输出: [2, 4]
解释: 
除'a'以外所有字符都是宽度为10, 
"bbbcccdddaa" 会覆盖9 * 10 + 2 * 4 = 98长度.
而最后的'a'则会写入第二行且.
所以结果是2行,最后一行是4长度.
Description
We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an array widths, an array where widths[0] is the width of 'a', widths[1] is the width of 'b', ..., and widths[25] is the width of 'z'.

Now answer two questions: how many lines have at least one character from S, and what is the width used by the last such line? Return your answer as an integer list of length 2.

The length of S will be in the range [1, 1000].
S will only contain lowercase letters.
widths is an array of length 26.
widths[i] will be in the range of [2, 10].
Have you met this question in a real interview?  
Example
Example1 :

Input: 
widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "abcdefghijklmnopqrstuvwxyz"
Output: [3, 60]
Explanation: 
All letters have the same length of 10. To write all 26 letters,
we need two full lines and one line with 60 units.
Example2:

Input: 
widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "bbbcccdddaaa"
Output: [2, 4]
Explanation: 
All letters except 'a' have the same length of 10, and 
"bbbcccdddaa" will cover 9 * 10 + 2 * 4 = 98 units.
For the last 'a', it is written on the second line because
there is only 2 units left in the first line.
So the answer is 2 lines, plus 4 units in the second line.
public class Solution {
    /**
     * @param widths: an array
     * @param S: a string
     * @return: how many lines have at least one character from S, and what is the width used by the last such line
     */
    public int[] numberOfLines(int[] widths, String S) {
        int[] result = new int[2];
        if (S == null || S.length() == 0) return result;
        
        result[0] = 1;
        char[] array = S.toCharArray();
        for (char c: array) {
            if (result[1] + widths[c - 'a'] <= 100) {
                result[1] += widths[c - 'a'];
            } else {
                result[1] = widths[c - 'a'];
                result[0]++;
            }
        }
        
        return result;
    }
}
public class Solution {
    /**
     * @param widths: an array
     * @param S: a string
     * @return: how many lines have at least one character from S, and what is the width used by the last such line
     */
       public int[] numberOfLines(int[] widths, String S) {
        // Write your code here
        
        char[] arr = S.toCharArray();

        int width = 0; 
        int line = 1;
        for (int i = 0; i < arr.length; i++) {
        	//判断该字符的unicode值
			int num = arr[i] - '0'  - 49;
			
			//获取长度
			if((width + widths[num]) <= 100) {
				width += widths[num];
			} else {
				width = widths[num];
				line++;
			}
			
		}
        int[] res = {line,width};
        return res;
    }
	
}
posted @ 2019-04-02 21:46  故人叹  阅读(142)  评论(0编辑  收藏  举报