问题

I'm trying to implement this algorithm in Javascript.

Given a string s. Return all the words vertically in the same order in which they appear in s.

Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).

Each word would be put on only one column and that in one column there will be only one word.

Input: s = "TO BE OR NOT TO BE"

Output: ["TBONTB","OEROOE"," T"]

Explanation: Trailing spaces is not allowed.

"TBONTB"

"OEROOE"

" T"

My solution:

var printVertically = function(s) {

let ans = [];

if(s === null || s.length === 0)

return ans;

let arr = s.split(" ");

let biggest = 0;

for(let i=0; i

if(arr[i].length > biggest)

biggest = arr[i].length;

}

let getBigWord = false;

while(arr.length !== 0) {

let word = arr.shift().split("");

if(!getBigWord && word.length === biggest)

getBigWord = true;

for(i=0; i

if(ans.length <= i)

ans[i] = word[i] === undefined ? " " : word[i];

else if(word[i] !== undefined) {

ans[i] += word[i];

} else if(!getBigWord) {

ans[i] += " ";

}

}

}

return ans;

};

For the input above, it works. However, if I change the input the solutions doesn't work. For example:

Input: s = "CONTEST IS COMING"

Output: ["CIC","OSO","N M","T I","E N","S G","T"]

My output will be: ["CIC","OSO","NM","TI","EN","SG","T"]

Does anyone know what I'm doing wrong?

Thanks

回答1:

Basically, this is a series of simple operations:

Split the string into words.

Create a matrix (2d array). The longest word in the string is the height (row count) of the matrix and the length of the number of strings is the width (column count).

Put the words in the string into the array rotated, that is, swap i and js where i is the row and j is the column.

Join and trim the right side of each row.

const verticize = s => {

const words = s.split(/\s+/);

return [...Array(Math.max(...words.map(e => e.length)))]

.map((_, i) =>

[...Array(words.length)]

.map((_, j) => words[j][i] || " ").join("").trimEnd());

};

console.log(verticize("CONTEST IS COMING"));

回答2:

You're never resetting the value of getBigWord so it's not working properly once you've seen a big word. Also it does not look like it would work properly for multiple big words.

This seems to work better:

var printVertically = function(s) {

let ans = [];

if(s === null || s.length === 0)

return ans;

let arr = s.split(" ");

let biggest = 0;

for(let i=0; i

if(arr[i].length > biggest)

biggest = arr[i].length;

}

while(arr.length !== 0) {

let word = arr.shift().split("");

let getBigWord = false

if(word.length === biggest) {

getBigWord = true;

}

for(i=0; i

if(ans.length <= i)

ans[i] = word[i] === undefined ? " " : word[i];

else if(word[i] !== undefined) {

ans[i] += word[i];

} else if(!getBigWord) {

ans[i] += " ";

}

}

}

for(i = 0; i < ans.length; i++) {

// Modern version :

// ans[i] = ans[i].trimRight();

ans[i] = ans[i].replace(/\s+$/g, "");

}

return ans;

};

I need to trim at the end to avoid extra spaces

来源:https://stackoverflow.com/questions/62659930/print-words-vertically-in-javascript

printvertically Java_Print Words Vertically in JavaScript相关推荐

  1. 像程序员一样思考:如何仅使用JavaScript,HTML和CSS来构建Snake

    by Panayiotis Nicolaou 通过Panayiotis Nicolaou 像程序员一样思考:如何仅使用JavaScript,HTML和CSS来构建Snake (Think like a ...

  2. JavaScript 开发工具webstrom使用指南

    WebStorm 是 JetBrains 推出的一款商业的 JavaScript 开发工具 任何一个编辑器都需要保存(ctrl + s),这是所有win平台上编辑类软件的特点,但是webstorm编辑 ...

  3. jquery 图像滑块_jQuery缩略图图像滑块– CSS,JavaScript

    jquery 图像滑块 In continuation with the tutorial on "Creating your own Content-Slider with Paginat ...

  4. HTML网页设计期末课程大作业~仿腾讯游戏官网设计与实现(HTML+CSS+JavaScript)

    HTML期末大作业~基于HTML+CSS+JavaScript腾讯游戏官网设计与实现 关于HTML期末网页制作,大作业A+水平 ~腾讯游戏官网HTML+CSS+JavaScript实现,共有游戏首页 ...

  5. JavaScript 插件

    概览 单个还是全部引入 JavaScript 插件可以单个引入(使用 Bootstrap 提供的单个 *.js 文件),或者一次性全部引入(使用 bootstrap.js 或压缩版的 bootstra ...

  6. 使用谷歌地图 Javascript版

    谷歌称Map JavaScript V3版是同时为PC和移动设备开发的,使用Html5. 首先需要在 Google Console 申请KEY,创建 一个 Browser key ,简单demo就可以 ...

  7. javascript控制台_使用JavaScript控制画布

    javascript控制台 您的指南 (YOUR GUIDE TO) Welcome readers from ◎ Your Guide to Coding Creativity on the Can ...

  8. css3图片旋转轮播_使用CSS和JavaScript构建3D旋转轮播

    css3图片旋转轮播 A lot has been said on the use of traditional 2D carousels, for example this piece on Sma ...

  9. 【AJAX】JavaScript的面向对象

    Ajax中后端数据返回后需要前端通过JavaScript来实现动态数据更新的问题.所以,在Ajax中加深了一遍JavaScript面向对象的印象. 基础部分: JavaScript中创建对象并简单对象 ...

  10. 【JavaScript总结】JavaScript语法基础:JS高级语法

    作用域链: 1.JS中只有函数能够限定作用域的范围: 2.变量处理在制定的函数范围内,还有一个特殊的作用域,就是没有用var 声明的全局作用域 3.js中的作用域链是为了清晰的表示出所有变量的作用范围 ...

最新文章

  1. 你以为你真的了解final吗?
  2. Linux高性能server编程——高级I/O函数
  3. linux文件本编辑,Linux就该这么学 -- 命令 -- 文本文件编辑命令
  4. SQL查询的安全方案
  5. 解锁Android性能优化的五大误区!满满干货指导
  6. win8远程访问mysql_Windows 安装 mysql8.0 配置远程访问
  7. 联发科芯片全线缺货:是OPPO太强还是英特尔抢产能
  8. 一台mysql数据库服务器_在一台服务器安装多个MySQL数据库
  9. Qt QT_BEGIN_NAMESPACE
  10. 最全总结 | 聊聊 Python 数据处理全家桶(Sqlite篇)
  11. 种子点生长算法下——三维种子点生长
  12. 《Java进阶学习+面试宝典》分享给大家
  13. ClickHouse安装与引擎
  14. h5课件制作_教师必备:实用H5课件制作技巧
  15. 用python统计字母个数_如何用python统计字符串中字母个数?
  16. 老调重谈:C语言中的指针和数组
  17. 《黄昏清兵卫》中学到的工作态度
  18. C++常用的音频工具库
  19. 【08】英语词汇速记大全1词根词缀记忆法
  20. 易语言误报优化助手 v1.5

热门文章

  1. 网络编程中常用的fd是什么
  2. HDU1922 POJ3004 Subway planning “神题”留名
  3. 人民网首届内容科技大赛 视界云荣膺三甲晋级决赛
  4. java 设置纸张大小设置_Java读取打印机自定义纸张.
  5. 读《华为“打工皇帝”徐家骏的十年感悟》的心志提升
  6. HashMap常见面试题
  7. 佳能打印服务 android,佳能打印机app
  8. postgreSql版的occurs函数
  9. 10个小故事,思考大数据
  10. 在服务系统部署MFC程序,出现DLL缺失情况的问题解决方式