首页 > 其他 > 详细

leetCode题解之Number of Lines To Write String

时间:2018-04-24 13:31:02      阅读:181      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/wangxiaoyong/p/8929104.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!