leetCode题解之Number of Lines To Write String

1、题目描述

2、分析

  使用一个map将字母和数字对应起来,方便后续使用。

3、代码

 1 vector<int> numberOfLines(vector<int>& widths, string S) {
 2         map<char,int> m;
 3         vector<int> ans;
 4         
 5         for( int i = 0; i< 26;i++)
 6             m[i+'a'] = widths[i];
 7         
 8         int lines = 1;
 9         int curLen =0;
10         int lastLen = 0;
11         for( size_t t = 0; t < S.size(); t++)
12         {
13             curLen += m[ S[t] ];
14             lastLen = curLen;
15             if(curLen > 100)
16             {
17                 lines += 1;
18                 curLen = 0;
19                 t--;
20             }     
21         }
22         ans.push_back(lines);
23         ans.push_back(lastLen);
24         
25         return ans;
26     }
pp
原文地址:https://www.cnblogs.com/wangxiaoyong/p/8929104.html