806. Number of Lines To Write String
806. Number of Lines To Write String
- 先得到一个res = {a : 80 , b : 10, c : 20.....的key-value对象}(目的是当调用res["a"]时得到一个等于10的值);
- 遍历传入的字符串,把每个元素带入到res中,并把所有的值进行累加;得到一个累加值sum,如:sum= sum + res["a"] + res["b"] + res["c"]....+res["z"];
- 当sum>100时,例如res["a"] + res["b"] + res["c"] =110,大于100,此时第一行应该只有res["a"]和res["b"],将flag加1,同时给sum赋值为当前的res[S[i]]值,sum重新开始下一行计数;
var numberOfLines = function (widths, S) {
let letterArr = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
let res = {};
let sum = 0;
// flag要从1开始计数,不满一行按一行计算
let flag = 1; letterArr.forEach((item, index) => {
//item 表示letterArr中的每个元素
//index 表示letterArr中的每个元素的索引值
// 得到一个对象{ a: 10,......}
res[item] = widths[index];
});
// 遍历传入的字符串,拿到每一个字母
for (let i = 0; i < S.length; i++) {
sum = sum + res[S[i]];
//当累加的和大于100时,也就是超出一行
if (sum > 100) {
flag++;
//给sum赋值为当前的res[S[i]],sum重新开始下一行计数
sum = res[S[i]];
} else if (sum == 100) {
flag++;
sum = 0;
}
}
return [flag, sum]; }; var widths = [10, 10, 10, 10, 13, 13, 13, 10, 7, 8, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
var S = "abcdefghijklmnopqrstuvwxyz";
console.log(numberOfLines(widths, S));
806. Number of Lines To Write String的更多相关文章
- 【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 解题报告
题目要求 We are to write the letters of a given string S, from left to right into lines. Each line has m ...
- [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 ...
- 【LeetCode】806. Number of Lines To Write String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用ASIIC码求长度 使用字典保存长度 日期 题目 ...
- 806. Number of Lines To Write String (5月24日)
解答 class Solution { public: vector<int> numberOfLines(vector<int>& widths, string S) ...
- LeetCode算法题-Number of Lines To Write String(Java实现)
这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为 ...
- [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 ...
- [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 ...
随机推荐
- hdu4507 数位dp+推公式
推公式的能力需要锻炼.. /* dp的时候要存结构体 里面三个元素: cnt,就是满足条件的个数 sum1,就是满足条件的数字和 sum2,满足条件的数字平方和 推导过程:还是用记忆化搜索模板 dp[ ...
- python计算文件的行数的方法
1.简单方法把文件读入一个大的列表中,然后统计列表的长度. count = len(open("文件名").readlines()) print count 2.读取文件某一行 ...
- [原创]基于Zynq Linux环境搭建(二)
在此篇,我们编译UBOOT 解压: [#17#17:26:56 FPGADeveloper@ubuntu ~/Zybo_Demo]$tar zxvf *.tar.gz 在解压过程中出现下述问题 tar ...
- Java NIO系列1-概观
Java NIO系列1-概观 Java NIO.中间的N你既可以理解为(new),也就是新的IO,相对于java1.5之前的IO它确实是新的;也可以理解为(no-blocking),也就是非阻塞的IO ...
- 第四章:4.0 python常用的模块
1.模块.包和相关语法 使用模块好处: 最大的好处是大大提高了代码的可维护性.其次,编写代码不必从零开始.当一个模块编写完毕,就可以被其他地方引用.我们在编写程序的时候,也经常引用其他模块,包括Pyt ...
- python实现FTP程序
python实现FTP程序 程序源码 上传功能 查看文件 cd功能 创建目录 程序源码 目录结构 服务端 主程序 import optparse import socketserver import ...
- Python编码规范:IF中的多行条件
Python编码规范:IF中的多行条件 转载 2017年03月08日 09:40:45 http://blog.csdn.net/wsc449/article/details/60866700 有时我 ...
- (转)RBAC权限模型——项目实战
一.前言 权限一句话来理解就是对资源的控制,对web应用来说就是对url的控制,关于权限可以毫不客气的说几乎每个系统都会包含,只不过不同系统关于权限的应用复杂程序不一样而已,现在我们在用的权限模型基本 ...
- Linux内核笔记:epoll实现原理(一)
一.说明 针对的内核版本为4.4.10. 本文只是我自己看源码的简单笔记,如果想了解epoll的实现,强烈推荐下面的文章: The Implementation of epoll(1) The Imp ...
- (81)Wangdao.com第十六天_JavaScript 严格模式
严格模式 除了正常的运行模式,JavaScript 还有第二种运行模式:严格模式(strict mode).顾名思义,这种模式采用更加严格的 JavaScript 语法 同样的代码,在正常模式和严格模 ...