LeetCode 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 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.
题目分析及思路
题目给出一个字符串和每个字母的长度数组,且限制每一行的最大值是100个单位,要求得到最后的行数和最后一行的宽度。可以遍历该字符串,每100个单位判断一下。
python代码
class Solution:
def numberOfLines(self, widths: 'List[int]', S: 'str') -> 'List[int]':
lines, width = 1, 0
for s in S:
w = widths[ord(s) - ord('a')]
width += w
if width > 100:
lines += 1
width = w
return [lines, width]
LeetCode 806 Number of Lines To Write String 解题报告的更多相关文章
- 【LeetCode】806. Number of Lines To Write String 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用ASIIC码求长度 使用字典保存长度 日期 题目 ...
 - 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】833. Find And Replace in String 解题报告(Python)
		
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
 - 【LeetCode】434. Number of Segments in a String 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...
 - [LeetCode&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 maximu ...
 - 806. Number of Lines To Write String  (5月24日)
		
解答 class Solution { public: vector<int> numberOfLines(vector<int>& widths, string S) ...
 - 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
 
随机推荐
- java实现栈-使用LinkedList
			
import java.util.LinkedList; public class LinkedListStack { public static void main(String[] args) { ...
 - python一天一题(3)
			
#--coding=utf8-- from selenium import webdriver import time import logging import os.path ''' 搜索取搜索的 ...
 - Go指南练习_图像
			
https://tour.go-zh.org/methods/25 一.题目描述 还记得之前编写的图片生成器吗?我们再来编写另外一个,不过这次它将会返回一个 image.Image 的实现而非一个数据 ...
 - Javascript Base64加密解密代码
			
<script language="javascript" runat="server"> var keyStr = "ABCDEFGHI ...
 - IntelliJ IDEA出现Search for无法进入编辑状态
			
今天由于多次修改系统时间,然后又进行查询,导致IntelliJ IDEA一直处于Search for,无法修改代码 原因: 可能是在不正确的系统时间启动的IDEA,然后启动完成后又把时间改成正确的 解 ...
 - 自己开发chrome插件生成二维码
			
摘要: 最近在开发微信项目时,需要在微信调试,所以经常会在微信中输入本地服务地址,输入起来特别麻烦,所以自己就想了想微信中的扫一扫,然后开发了这款chrome插件,将当前url生成二维码,用微信扫一扫 ...
 - [IR] Concept Search and LSI
			
基于术语关系的贝叶斯网络信息检索模型扩展研究 LSI 阅读笔记 背景知识 提出一种改进的共现频率法,利用该方法挖掘了索引术语之间的相关关系,将这种相关关系引入信念网络模型,提出了一个具有两层术语节点的 ...
 - 15适配器模式Adapter
			
一.什么是适配器模式 Adapter模式也叫适配器模式,是构造型模式之一 ,通过Adapter模式可以改变已有类(或外部类)的接 口形式. 二.适配器模式应用场景 在大规模的系统开发过程中,我们常常碰 ...
 - SSL、数字签名、CA 工作原理通俗描述
			
SSL(Secure Socket Layer) 是一种加密技术,可以提供对称加密和非对称加密.由于它在协议层里正好是在传输层与应用层之间,这就决定了上层应用必须经过它,这就是它广泛流行和易于实现的原 ...
 - D - Lake Counting
			
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented ...