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 }
  • 相关阅读:
    今天一个人跑了趟香山
    周六钻胡同
    (转)C#中protected用法详解
    C# base和this
    error BK1506 : cannot open file '.\Debug\ex73View.sbr': No such file or directory
    error PRJ0003 : Error spawning 'cmd.exe'
    VS2008卸载时遇到“加载安装组件时遇到问题,取消安装” 在卸载或者升级VS2008的时候,遇到“加载安装组件时遇到问题,取消安装”的情况
    我们在建立Win32工程的时候,要选择是Win32控制台应用程序还是Win32项目,那么两者到底有什么区别呢?
    开发板重新烧写时出现ERROR: Checksum failure (expected=0x3D67E6F computed=0x3E0E0CA)
    把PC上的代码移植到WINCE上
  • 原文地址:https://www.cnblogs.com/strengthen/p/12213371.html
Copyright © 2011-2022 走看看