[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 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.
Example :
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.
Example :
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.
Note:
- The length of
S
will be in the range [1, 1000]. S
will only contain lowercase letters.widths
is an array of length26
.widths[i]
will be in the range of[2, 10]
.
这道题给了我们一个字符串,让我们把里面的字母写下来,规定了每一行的长度为100,然后每个字母的长度可以在widths数组中查询,说是如果某一个字母加上后超过了长度100的限制,那么就移动到下一行,问我们最终需要多少行,和最后一行的长度。这道题并没有太大的难度和技巧,就是楞头写呗,遍历所有的字母,然后查表得到其宽度,然后看加上这个新宽度是否超了100,超了的话,行数计数器自增1,并且当前长度为这个字母的长度,因为另起了一行。如果没超100,那么行长度就直接加上这个字母的长度。遍历完成后返回行数和当前行长度即可,参见代码如下:
class Solution {
public:
vector<int> numberOfLines(vector<int>& widths, string S) {
int cnt = , cur = ;
for (char c : S) {
int t = widths[c - 'a'];
if (cur + t > ) ++cnt;
cur = (cur + t > ) ? t : cur + t;
}
return {cnt, cur};
}
};
参考资料:
https://leetcode.com/problems/number-of-lines-to-write-string/solution/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Number of Lines To Write String 写字符串需要的行数的更多相关文章
- Leetcode806.Number of Lines To Write String写字符串需要的行数
我们要把给定的字符串 S 从左到右写到每一行上,每一行的最大宽度为100个单位,如果我们在写某个字母的时候会使这行超过了100 个单位,那么我们应该把这个字母写到下一行.我们给定了一个数组 width ...
- Java实现 LeetCode 806 写字符串需要的行数 (暴力模拟)
806. 写字符串需要的行数 我们要把给定的字符串 S 从左到右写到每一行上,每一行的最大宽度为100个单位,如果我们在写某个字母的时候会使这行超过了100 个单位,那么我们应该把这个字母写到下一行. ...
- [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 maximu ...
- Q806 写字符串需要的行数
我们要把给定的字符串 S 从左到右写到每一行上,每一行的最大宽度为100个单位,如果我们在写某个字母的时候会使这行超过了100 个单位,那么我们应该把这个字母写到下一行.我们给定了一个数组 width ...
- 806. Number of Lines To Write String - LeetCode
Question 806. Number of Lines To Write String Solution 思路:注意一点,如果a长度为4,当前行已经用了98个单元,要另起一行. Java实现: p ...
- 806. Number of Lines To Write String
806. Number of Lines To Write String 整体思路: 先得到一个res = {a : 80 , b : 10, c : 20.....的key-value对象}(目的是 ...
- 【Leetcode_easy】806. Number of Lines To Write String
problem 806. Number of Lines To Write String solution: class Solution { public: vector<int> nu ...
- 【LeetCode】806. Number of Lines To Write String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用ASIIC码求长度 使用字典保存长度 日期 题目 ...
- LeetCode算法题-Number of Lines To Write String(Java实现)
这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为 ...
随机推荐
- Run Configurations(Debug Configurations)->Arguments里填写program arguments和VM arguments
如图: 1.program arguments存储在String[] args里 2.VM arguments设置的是虚拟机的属性,是传给java虚拟机的.KV形式存储的,是可以通过System.ge ...
- 第五节: Quartz.Net五大构件之Trigger的四大触发类
一. WithSimpleSchedule(ISimpleTrigger) 1. 用途:时.分.秒上的轮询(和timer类似),实际开发中,该场景占绝大多数. 2. 轮询的种类:永远轮询和限定次数轮询 ...
- MySQL关于日志配置安全整改及处理方法
[环境介绍] 系统环境:Linux + mysql 5.7.18 + 主从复制架构 [背景描述] 需求:MySQL数据库都有每年的集团安全整改,常常要求弱口令扫描,基线扫描,漏洞扫描等等.对于MySQ ...
- 083_Remove Duplicates from Sorted List
class ListNode: def __init__(self,x): self.val=x self.next=None ####注意这道题并不是把重复元素全部去掉而是保留一个#### #### ...
- Anaconda+django安装问题
Anaconda使用中常遇到如下问题: 如果Anaconda不是最新版本,可在Anaconda Prompt中使用如下命令更新至最新版 conda update -n base -c defaults ...
- vue通过extend动态创建全局组件(插件)学习小记
测试环境:nodejs+webpack,例子是看文章的,注释为自己的理解 创建一个toast.vue文件: <template> <div class="wrap" ...
- python 列表 元组 字典 集合
列表 lst = [i for i in range(10)] 切片 # 把下标小于2的显示出来 print(lst[:2]) # 把10个数有大到小输出 print(lst[::-1]) # 把下标 ...
- SpringMVC+Spring+Hibernate整合开发
最近突然想认真研究下java web常用框架,虽然现在一直在用,但实现的整体流程不是很了解,就在网上搜索资料,尝试自己搭建,以下是自己的搭建及测试过程. 一.准备工作: 1/安装并配置java运行环境 ...
- js通过高德地图获取当前位置的经度纬度
效果图如下: 已经获取到了经度纬度了 代码如下: <!doctype html> <html> <head> <meta charset="utf- ...
- 使用Pycharm创建一个Django项目
在使用python写脚本一段时间后,想尝试使用Django来编写一个python项目,现做以下记录备忘: 1.创建项目 如果本地没有安装与所选python版本对应Django版本,pycharm会自动 ...