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     }

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

时间: 2024-07-29 21:53:37

leetCode题解之Number of Lines To Write String的相关文章

LeetCode算法题-Number of Lines To Write String(Java实现)

这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为100个单位,如果写一个字母会导致该行的宽度超过100个单位,则会写入下一行.给出一个数组宽度,一个数组,其中widths[0]是'a'的宽度,widths[1]是'b'的宽度,widths[25]是'z'的宽度. 现在回答两个问题:S中至少有一个字符有多少行,最后一行使用的宽度是多少?将答案作为长

[LeetCode] Number of Lines To Write String

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 arra

[LeetCode&amp;Python] Problem 806. Number of Lines To Write String

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 arra

leetcode题解||Palindrome Number问题

problem: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of

LeetCode题解之Number of 1 Bits

1.题目描述 2.问题分析 使用C++ 标准库的 bitset 类,将整数转换为 二进制,然后将二进制表示转换为字符串,统计字符串中 1 的个数即可. 3.代码 1 int hammingWeight(uint32_t n) { 2 bitset<32> b(n); 3 string b_s = b.to_string() ; 4 5 int count_one = 0; 6 for(string::iterator it = b_s.begin(); it != b_s.end() ; ++

[Swift]LeetCode806. 写字符串需要的行数 | Number of Lines To Write String

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 arra

806. Number of Lines To Write String (5月24日)

解答 class Solution { public: vector<int> numberOfLines(vector<int>& widths, string S) { vector<int> result; int line=1,units=0; map<char,int> pairs; char c='a'; for(int width:widths){ pairs.insert(make_pair(c,width)); ++c; } for

[LeetCode 题解]:Palindrome Number

前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of c

[LeetCode 题解]: Reverse Nodes in K-Groups

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod