问题

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. 这 10 款插件让你的 GitHub 更好用、更有趣
  2. 如何从多个项目创建 ASP.NET 应用程序以进行组开发
  3. 雅虎向阿里巴巴示好原因有二
  4. 投资100亿美元,谷歌计划在2020年向美国办事处和数据中心
  5. WPF不同线程之间的控件的访问
  6. [原]ImportError: No module named thrift.Thrift问题解决
  7. python与线性代数 解线性方程组
  8. NUC1372 Bull Math【大数】
  9. 计算机网络安全基础知识
  10. c++ strcmp函数
  11. Win32_1深入浅出windows消息机制
  12. php网站渗透实战_PHP网站安全-漏洞渗透及解决方式—概述
  13. Inno SetUp中文语言包以及在脚本中使用
  14. 使用Arduino驱动 ADS1115 ADC采样芯片
  15. CentOS 无法连接网络解决办法
  16. 7-3 IP地址转换分数 20
  17. mysql 从a到z 查询_mysql 查询数据时按照A-Z顺序排序返回结果集
  18. 无线路由器的dhcp服务器是什么,路由器dhcp是什么 路由器dhcp服务器如何设置
  19. KaliLinux装好系统后安装常用软件
  20. 网站每天更新几十篇上百篇文章是怎么做到的?

热门文章

  1. 第39级台阶--递归
  2. 【MC-CNN论文翻译】Computing the Stereo Matching Cost with a Convolutional Neural Network
  3. html5 video断点续播,Vue中集成vue-video-player及相关api/vue视频播放插件/支持断点续播...
  4. ThinkPHP6 自定义分页样式 快速配置
  5. python定义一个汽车类_定义一个创造汽车的工厂类
  6. 【嵌入式蓝桥杯】程序执行完中断将不再触发 /* Go to infinite loop when Hard Fault exception occurs */
  7. WARNING: There was an error checking the latest version of pip.
  8. 百度低代码框架amis介绍及实例讲解
  9. 手机怎么设置腾达路由器后显示远端服务器,怎么用手机设置腾达路由器
  10. python添加背景图片_Python实例 tkinter canvas (设置背景图片及文字)