Leetcode806.Number of Lines To Write String写字符串需要的行数
我们要把给定的字符串 S 从左到右写到每一行上,每一行的最大宽度为100个单位,如果我们在写某个字母的时候会使这行超过了100 个单位,那么我们应该把这个字母写到下一行。我们给定了一个数组 widths ,这个数组 widths[0] 代表 'a' 需要的单位, widths[1] 代表 'b' 需要的单位,..., widths[25] 代表 'z' 需要的单位。
现在回答两个问题:至少多少行能放下S,以及最后一行使用的宽度是多少个单位?将你的答案作为长度为2的整数列表返回。
示例 1: 输入: 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" 输出: [3, 60] 解释: 所有的字符拥有相同的占用单位10。所以书写所有的26个字母, 我们需要2个整行和占用60个单位的一行。
示例 2: 输入: 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" 输出: [2, 4] 解释: 除去字母'a'所有的字符都是相同的单位10,并且字符串 "bbbcccdddaa" 将会覆盖 9 * 10 + 2 * 4 = 98 个单位. 最后一个字母 'a' 将会被写到第二行,因为第一行只剩下2个单位了。 所以,这个答案是2行,第二行有4个单位宽度。
注:
- 字符串 S 的长度在 [1, 1000] 的范围。
- S 只包含小写字母。
- widths 是长度为 26的数组。
- widths[i] 值的范围在 [2, 10]。
class Solution {
public:
vector<int> numberOfLines(vector<int>& widths, string S) {
vector<int> res;
int sum = 0;
int len = S.size();
int cnt = 0;
int current = 0;
for(int i = 0; i < len; i++)
{
sum += widths[S[i] - 'a'];
if(sum <= 100)
{
current = sum;
}
else
{
cnt++;
sum = widths[S[i] - 'a'];
current = sum;
}
}
res.push_back(cnt + 1);
res.push_back(current);
return res;
}
};
Leetcode806.Number of Lines To Write String写字符串需要的行数的更多相关文章
- [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 maximu ...
- 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
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 ...
- 806. Number of Lines To Write String - LeetCode
Question 806. Number of Lines To Write String Solution 思路:注意一点,如果a长度为4,当前行已经用了98个单元,要另起一行. Java实现: p ...
- 【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的字母从左到右写成行.每行最大宽度为 ...
随机推荐
- Jeecms之JSP访问action类
因为Jeecms采用spring容器管理类,在web容器加载的时候类都已经实例化好了.我们可以通过在JSP中访问spring上下文的方式来调用action业务类例: ApplicationContex ...
- csdn vip文章:Unity游戏开发-小游戏-非常简单的游戏-unity-
原文https://blog.csdn.net/qq_20484877/article/details/81841190 1*创建物体 Create菜单下 3D Object菜单下Cube 1.1设置 ...
- CentOS 6.5 FTP安装配置
安装配置 rpm -q vsftpd #检查是否安装了FTP yum -y install vsftpd #安装FTP chkconfig vsftpd on #设置开机启动 service vsft ...
- springcloud 与分布式系统(转载)
原地址:http://blog.csdn.net/neosmith/article/details/51919038 本文不是讲解如何使用spring Cloud的教程,而是探讨Spring Clou ...
- 2019阿里云开年Hi购季满返活动火热报名中!
摘要: 在每年开年的这个大幅度优惠促销月,怎样才能花最少的钱配置最特惠的云服务?请看本文! 2019阿里云云上采购季活动已经于2月25日正式开启,从已开放的活动页面来看,活动分为三个阶段: 2月25日 ...
- H5C3--文本阴影text-shadow
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MongoDB命令的简单操作(一)
MongoDB是工作在集合和文档上的一种概念. 1.创建数据库 use name2.查看所以的数据库列表 show dbs3.查看当前数据库 db4.向数据库插入数据 db.items.insert( ...
- Hibernate中的Query
//1:创建query对象,方法里面写hql语句 Query query = session.createQuery("from User"); //2:利用query对象中的方法 ...
- “Bootstrap做的响应式菜单在iPhone上点击不了二级菜单“的解决办法!
只需把把点击的a(被点击的)变成button即可.
- fedora python访问mysql
1 下载驱动库 http://sourceforge.net/projects/mysql-python/ 2, yum install mysql-dev* yum install python-d ...