806. Number of Lines To Write String

整体思路:
  1. 先得到一个res = {a : 80 , b : 10, c : 20.....的key-value对象}(目的是当调用res["a"]时得到一个等于10的值);
  2. 遍历传入的字符串,把每个元素带入到res中,并把所有的值进行累加;得到一个累加值sum,如:sum= sum + res["a"] + res["b"] + res["c"]....+res["z"];
  3. 当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的更多相关文章

  1. 【Leetcode_easy】806. Number of Lines To Write String

    problem 806. Number of Lines To Write String solution: class Solution { public: vector<int> nu ...

  2. 806. Number of Lines To Write String - LeetCode

    Question 806. Number of Lines To Write String Solution 思路:注意一点,如果a长度为4,当前行已经用了98个单元,要另起一行. Java实现: p ...

  3. 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 ...

  4. [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 ...

  5. 【LeetCode】806. Number of Lines To Write String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用ASIIC码求长度 使用字典保存长度 日期 题目 ...

  6. 806. Number of Lines To Write String (5月24日)

    解答 class Solution { public: vector<int> numberOfLines(vector<int>& widths, string S) ...

  7. LeetCode算法题-Number of Lines To Write String(Java实现)

    这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. uni-app调用原生的文件系统管理器(可选取附件上传)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Jace Config

    一.jace配置 1.按照Jace 的默认IP配置自己电脑的IP网段(同一个网段),连接上之后使用默认的密码登录,导入tridiumEMEA…..的授权文件,之后创建Station,选择需要的协议驱动 ...

  3. 二维前缀和好题hdu6514

    #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; ]; )* ...

  4. Linux 目录结构和常用命令

    Linux目录结构 目录 说明 bin 存放二进制可执行文件(ls,cat,mkdir等) boot 存放用于系统引导时使用的各种文件 dev 用于存放设备文件 etc 存放系统配置文件 home 存 ...

  5. beta冲刺6/7

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:beta冲刺(6/7) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 ppt制作 视频拍摄 接下来的计划 准备答辩 ...

  6. Android - JSON Parser Tutorial

    Android provides four different classes to manipulate JSON data. These classes are JSONArray,JSONObj ...

  7. 第二篇flask响应方式

    响应三件套 1.Flask中的HTTPResponse @app.route('/home') # app中的route装饰器 def home(): # 视图 return '登陆成功' #HTTP ...

  8. JAVA篇<一> 继承extends(已转移到JAVA总结篇)

    前题:如果不经过指出继承,那么所有的类都继承了JAVA中的Object类. 正文: 继承的关键字是:extends,是所有面向对象语言的重要特性. 例public class TestExtends ...

  9. java之微信支付通知

    微信支付,是现在大多数平台都需要接入的一个支付方式,没办法,谁让现在的用户都习惯了这种消费方式呢 我今天只说说微信支付通知,我们后台怎么接收通知,并把我们的订单的状态改为已支付, 至于为什么不说支付的 ...

  10. Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案

    转自http://www.cnblogs.com/fnz0/p/5713102.html 不知道自己什么时候才有这种钻研精神- -. 1      背景 系统中需要批量生成单据数据到数据库表,所以采用 ...