zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1324. 竖直打印单词 | Print Words Vertically

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(let_us_code)
    ➤博主域名:https://www.zengqiang.org
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/12213371.html
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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.


    给你一个字符串 s。请你按照单词在 s 中的出现顺序将它们全部竖直返回。
    单词应该以字符串列表的形式返回,必要时用空格补位,但输出尾部的空格需要删除(不允许尾随空格)。
    每个单词只能放在一列上,每一列中也只能有一个单词。

    示例 1:

    输入:s = "HOW ARE YOU"
    输出:["HAY","ORO","WEU"]
    解释:每个单词都应该竖直打印。
    "HAY"
     "ORO"
     "WEU"
    示例 2:

    输入:s = "TO BE OR NOT TO BE"
    输出:["TBONTB","OEROOE"," T"]
    解释:题目允许使用空格补位,但不允许输出末尾出现空格。
    "TBONTB"
    "OEROOE"
    " T"
    示例 3:

    输入:s = "CONTEST IS COMING"
    输出:["CIC","OSO","N M","T I","E N","S G","T"]
     

    提示:

    1 <= s.length <= 200
    s 仅含大写英文字母。
    题目数据保证两个单词之间只有一个空格。


    Runtime: 4 ms
    Memory Usage: 22 MB
     1 class Solution {
     2     func printVertically(_ s: String) -> [String] {
     3         let aux:[[Character]] = s.components(separatedBy:" ").map{Array($0)}
     4         let n:Int = aux.count
     5         var ans:[String] = [String]()
     6         var c:Int = 0
     7         while(true)
     8         {
     9             var str:String = String()
    10             var flag:Int = 0
    11             var index:Int = 0
    12             for i in 0..<n
    13             {
    14                 if c < aux[i].count
    15                 {
    16                     str.append(aux[i][c])
    17                     flag = 1
    18                     index = i
    19                 }
    20                 else
    21                 {
    22                     str.append(" ")
    23                 }
    24             }
    25             if flag == 0{return ans}
    26             ans.append(str.subString(0, index + 1))
    27             c += 1
    28         }
    29     }
    30 }
    31 
    32 extension String {
    33      // 截取字符串:指定索引和字符数
    34     // - begin: 开始截取处索引
    35     // - count: 截取的字符数量
    36     func subString(_ begin:Int,_ count:Int) -> String {
    37         let start = self.index(self.startIndex, offsetBy: max(0, begin))
    38         let end = self.index(self.startIndex, offsetBy:  min(self.count, begin + count))
    39         return String(self[start..<end])
    40     }
    41 }
  • 相关阅读:
    Java实现网易163邮箱好友通讯录的解析功能(带源码)
    wordpress优化第四招 修改评论模板,留住客户,让评论在新的页面打开。
    wordpress优化 使用SAE提供的jquery.js替代wordpress原生的
    出售wordpress的淘宝客主题一套
    做了一个可以生成在线mp3 flash播放器的网站
    wordpress优化第三招 开启gzip减少网页流量
    20多个常用的免费WebService接口
    wordpress优化第一招 压缩css和js减少流量提高博客速度(尤其适用SAE)
    Linux学习笔记10常用操作命令(useradd命令、passwd 命令)
    Linux学习笔记08linux文本处理(cat命令、more命令、head命令、tail命令)
  • 原文地址:https://www.cnblogs.com/strengthen/p/12213371.html
Copyright © 2011-2022 走看看