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 ...
随机推荐
- Application Security Per-Engagement
1. an SQLi vulnerability will allow you to do the following query the database using select statem ...
- C语言fread/fwrite填坑记
坑的描述 用fwrite把数据写入文件,再用fread读取,发现后半部分的数据可能是错的. 原因:原本要写入文件的数据中,有0x0A,如果用的是文本模式打开的文件流,在windows下0x0A会被转换 ...
- 记一次Nginx+Keepalived高可用故障转移
Master端:192.168.2.156 ! Configuration File for keepalived global_defs { notification_email { acassen ...
- 2.使用RNN做诗歌生成
诗歌生成比分类问题要稍微麻烦一些,而且第一次使用RNN做文本方面的问题,还是有很多概念性的东西~~~ 数据下载: 链接:https://pan.baidu.com/s/1uCDup7U5rGuIlIb ...
- 洛谷.3733.[HAOI2017]八纵八横(线性基 线段树分治 bitset)
LOJ 洛谷 最基本的思路同BZOJ2115 Xor,将图中所有环的异或和插入线性基,求一下线性基中数的异或最大值. 用bitset优化一下,暴力的复杂度是\(O(\frac{qmL^2}{w})\) ...
- RAID 0 ~ RAID 7
一.RAID解析1.RAID 0(1)将几块磁盘并行组合,横向写数据(2)并发IO,写数据最快缺点:不提供数据冗余,如果其中一块磁盘废掉,则数据全毁详细:条带深度大时,一次IO只能从一块磁盘上读取,无 ...
- North American Invitational Programming Contest 2018
A. Cut it Out! 枚举第一刀,那么之后每切一刀都会将原问题划分成两个子问题. 考虑DP,设$f[l][r]$表示$l$点顺时针一直到$r$点还未切割的最小代价,预处理出每条边的代价转移即可 ...
- angular.isObject()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- __x__(9)0906第三天__常见的标签
<!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...
- css 控制文字显示两行,多余用省略号 手机端
p { width:100px; position:relative; line-height:20px; /*行高为高度的一半,这样就是两行*/ height:40px; overflow:hidd ...