• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode1324.竖直打印单词|PrintWordsVertically

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode847.访问所有节点的最短路径|ShortestPathVisitingAllNodes ...发布时间:2022-07-13
下一篇:
Swift与OC的相互调用发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap