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 }
  • 相关阅读:
    HDU3336 Count the string —— KMP next数组
    CodeForces
    51Nod 1627 瞬间移动 —— 组合数学
    51Nod 1158 全是1的最大子矩阵 —— 预处理 + 暴力枚举 or 单调栈
    51Nod 1225 余数之和 —— 分区枚举
    51Nod 1084 矩阵取数问题 V2 —— 最小费用最大流 or 多线程DP
    51Nod 机器人走方格 V3 —— 卡特兰数、Lucas定理
    51Nod XOR key —— 区间最大异或值 可持久化字典树
    HDU4825 Xor Sum —— Trie树
    51Nod 1515 明辨是非 —— 并查集 + 启发式合并
  • 原文地址:https://www.cnblogs.com/strengthen/p/12213371.html
Copyright © 2011-2022 走看看