leetCode题解之Number of Lines To Write String

Posted 山里的小勇子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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     }

 

以上是关于leetCode题解之Number of Lines To Write String的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 题解 || Letter Combinations of a Phone Number 问题

LeetCode 447. Number of Boomerangs; 149. Max Points on a Line

[LeetCode] Number of Islands II 岛屿的数量之二

LeetCode题解之Missing Number

[LeetCode] 305. Number of Islands II 岛屿的数量之二

LeetCode题解之Valid Triangle Number