1324. Print Words Vertically**

https://leetcode.com/problems/print-words-vertically/

题目描述

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.

Example 1:

Input: s = "HOW ARE YOU"
Output: ["HAY","ORO","WEU"]
Explanation: Each word is printed vertically. "HAY""ORO""WEU"

Example 2:

Input: s = "TO BE OR NOT TO BE"
Output: ["TBONTB","OEROOE","   T"]
Explanation: Trailing spaces is not allowed.
"TBONTB"
"OEROOE"
"   T"

Example 3:

Input: s = "CONTEST IS COMING"
Output: ["CIC","OSO","N M","T I","E N","S G","T"]

Constraints:

  • 1 <= s.length <= 200
  • s contains only upper case English letters.
  • It’s guaranteed that there is only one space between 2 words.

C++ 实现 1

按空格 split 将 s 中的 word 提取出来, 并求出最大的 word 的长度. 注意 s 本身是以 English letters 开头和结尾的, 这意味着 s = " HOW ARE YOU " 这种例子是不会出现的.

class Solution {public:vector<string> printVertically(string s) {vector<string> split_words;int max_len = 0;for (int i = 0; i < s.size(); ++ i) {int j = i;while (j < s.size() && s[j] != ' ') ++ j;split_words.push_back(s.substr(i, j - i));max_len = std::max(max_len, j - i);i = j;}vector<string> res;for (int i = 0; i < max_len; ++ i) {string word;for (auto &w : split_words) {word += i < w.size() ? w[i] : ' ';}// remove trailing spaces// 也可以使用 int end = word.find_last_not_of(' ');// word.substr(0, end + 1);int j = word.size();while (j - 1 >= 0 && word[j - 1] == ' ') -- j;word = word.substr(0, j);res.push_back(word);}return res;}
};

1324. Print Words Vertically**相关推荐

  1. Print Words Vertically(C++竖直打印单词)

    解题思路: (1)切分+填空 class Solution { public:vector<string> printVertically(string s) {vector<str ...

  2. MySQL工具1:mysqladmin

    每两秒显示一下MySQL的状态,一共显示5次. # mysqladmin -uroot -p -i 2 -c 5 status 查看MySQL的运行状态: #mysqladmin -uroot -p ...

  3. 翻译【mysqladmin --help】

    翻译[mysqladmin --help] mysqladmin  Ver 8.42 Distrib 5.5.32, for Linux on x86_64 Copyright (c) 2000, 2 ...

  4. mysqladmin 管理命令详细应用

    mysqladmin 工具的使用格式: mysqladmin [option] command [command option] command ...... option 选项: -c  numbe ...

  5. 利用python,将python语法代码翻译成js语法代码

    将python自动翻译成js脚本 说明 由于想用通过写python代码直接翻译出来对应的js代码,python代码有它自身的简洁性,代码量比较少,可以做一个脚本翻译成运行速度高但是代码量打的编程语言, ...

  6. python的print格式化输出,以及使用format来控制。

    20210305 time.strftime("%Y%m%d%H%M%S", time.localtime()) 时间格式化 20210206 https://www.runoob ...

  7. PyTorch代码调试利器: 自动print每行代码的Tensor信息

    本文介绍一个用于 PyTorch 代码的实用工具 TorchSnooper.作者是TorchSnooper的作者,也是PyTorch开发者之一. GitHub 项目地址: https://github ...

  8. python 怎样让 print 打印的结果不换行

    遍历打印一个list ,打印的结果换行了如下: list_a = [1, 2, 3, 4] for i in list_a:print(i) 打印结果如下: 现在想是打印结果不换行 使用 end 关键 ...

  9. SyntaxError: Missing parentheses in call to ‘print‘. Did you mean print(i, colour[i])?

    报错如下: 错误提示如下: 问题的处理方法: 在print后面添加小括号 如下 people = ["李白","杜甫","我"] for i ...

  10. python 错误之SyntaxError: Missing parentheses in call to 'print'

    SyntaxError: Missing parentheses in call to 'print' 由于python的版本差异,造成的错误. python2: print "hello ...

最新文章

  1. 对 Jenkins+ANT+Jmeter 接口测试的实践
  2. linux 反弹shell(二)反弹shell的本质
  3. 【ijkplayer】编译 Android 版本的 ijkplayer ⑤ ( 执行 init-android-libyuv.sh | 执行 init-android-soundtouch.sh )
  4. 运维基础(3)备份篇
  5. UML 中各种图形重要性的排行
  6. with pdo mysql_如何在PHP下开启PDO MySQL的扩展
  7. 强化学习(二)--动态规划寻找最优策略
  8. 优化器——梯度下降优化算法综述
  9. Julia: arrow,一种革命性的数据格式
  10. linux硬盘检测工具,利用Smartmontools工具检测Linux硬盘状况 – 安装及基本应用 | 老左笔记...
  11. 《黑客攻防从入门到精通》:社会工程学
  12. ionic 中使用 slidebox 利用angular ng-repeat 渲染后不显示问题
  13. 论文笔记(2):Cartoon Avatar Generation with Configurable Attributes based on GAN
  14. 服务器阵列卡缓存显示错误,服务器阵列卡(缓存)
  15. GNS3中配置直连交换端口的路由器端口IP地址
  16. Fedora分区扩容以及如何修复引导
  17. 深度学习笔记(四十一)循环神经网络及正反向传播
  18. 批量修改文件内容(Python版)
  19. [转]程序员成功之路 ——The road ahead for programmer(演讲稿)
  20. 三个最好的免费CD/DVD刻录软件下载

热门文章

  1. A Game of Thrones(96)
  2. php 微信多客服系统,如何使用微信公众平台开发模式实现多客服
  3. 一年时间,拿到了人生中的第一个20万
  4. win7旗舰版通知windows不是正版副本解决方法
  5. 如何写论文的introduction
  6. qiankun加载react子应用报错[import-html-entry]: error occurs while executing normal script
  7. 学习java第20天
  8. 从哪些方面评价一款在线客服系统产品
  9. OpenShift — 部署 OKD 4.5
  10. 面试常问--你最大的长处和弱点分别是什么?这些长处和弱点对你在企业的业绩会有什么样的影响