日月换新天。为有牺牲多壮志,敢教

[Swift]LeetCode1309. 解码字母到整数映射 | Decrypt String from Alphabet to Integer Mapping

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

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

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

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

Given a string s formed by digits ('0' - '9') and '#' . We want to map s to English lowercase characters as follows:

  • Characters ('a' to 'i') are represented by ('1' to '9') respectively.
  • Characters ('j' to 'z') are represented by ('10#' to '26#') respectively. 

Return the string formed after mapping.

It's guaranteed that a unique mapping will always exist.

 

Example 1:

Input: s = "10#11#12"
Output: "jkab"
Explanation: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".

Example 2:

Input: s = "1326#"
Output: "acz"

Example 3:

Input: s = "25#"
Output: "y"

Example 4:

Input: s = "12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#"
Output: "abcdefghijklmnopqrstuvwxyz"

 

Constraints:

  • 1 <= s.length <= 1000
  • s[i] only contains digits letters ('0'-'9') and '#' letter.
  • s will be valid string such that mapping is always possible.

给你一个字符串 s,它由数字('0' - '9')和 '#' 组成。我们希望按下述规则将 s 映射为一些小写英文字符:

  • 字符('a' - 'i')分别用('1' - '9')表示。
  • 字符('j' - 'z')分别用('10#' - '26#')表示。 

返回映射之后形成的新字符串。

题目数据保证映射始终唯一。

 

示例 1:

输入:s = "10#11#12"
输出:"jkab"
解释:"j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".

示例 2:

输入:s = "1326#"
输出:"acz"

示例 3:

输入:s = "25#"
输出:"y"

示例 4:

输入:s = "12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#"
输出:"abcdefghijklmnopqrstuvwxyz"

 

提示:

  • 1 <= s.length <= 1000
  • s[i] 只包含数字('0'-'9')和 '#' 字符。
  • s 是映射始终存在的有效字符串。

Runtime: 4 ms

Memory Usage: 21.1 MB
复制代码
 1 class Solution {
 2     func freqAlphabets(_ s: String) -> String {
 3         let s:[Character] = Array(s)
 4         var res:String = String()
 5         var i:Int = 0
 6         while(i < s.count)
 7         {
 8             if i < s.count - 2 && s[i + 2] == "#"
 9             {
10                 //"j":106 , "0": 48 , "1":49
11                 let num:Int =  106 +  (Int(s[i].asciiValue!)  - 49) * 10 + Int(s[i + 1].asciiValue!) - 48
12                 res.append(num.ASCII)
13                 i += 2
14             }
15             else
16             {
17                 //a:97 , "1":49
18                 let num:Int =  97 + Int(s[i].asciiValue! ) -  49
19                 res.append(num.ASCII)
20             }
21             i += 1
22         }
23         return res
24     }
25 }
26 
27 //Int扩展
28 extension Int
29 {
30     //Int转Character,ASCII值(定义大写为字符值)
31     var ASCII:Character
32     {
33         get {return Character(UnicodeScalar(self)!)}
34     }
35 }
复制代码

Runtime: 16 ms
Memory Usage: 21.3 MB
复制代码
 1  class Solution {
 2     private let chars = Array<Character>(" abcdefghijklmnopqrstuvwxyz")
 3     func freqAlphabets(_ s: String) -> String {
 4         var ans = ""
 5         var sCopy = s
 6         while !sCopy.isEmpty {
 7             if let index = sCopy.firstIndex(of: "#"), sCopy.distance(from: sCopy.startIndex, to: index) == 2 {
 8                 let indexStr = String(sCopy[sCopy.startIndex...sCopy.index(after: sCopy.startIndex) ])
 9                 sCopy.removeFirst(3)
10                 ans.append(chars[Int(indexStr)!])
11             } else {
12                 ans.append(chars[Int("\(sCopy.removeFirst())")!])
13             }
14         }
15         return ans
16     }
17  }
复制代码

 

posted @   为敢技术  阅读(542)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2019-01-05 [Swift]LeetCode259.三数之和较小值 $ 3Sum Smaller
2019-01-05 [Swift]LeetCode256.粉刷房子 $ Paint House
2019-01-05 [Swift]LeetCode255.验证二叉搜索树的先序序列 $ Verify Preorder Sequence in Binary Search Tree
2019-01-05 [Swift]LeetCode254.因子组合 $ Factor Combinations
2019-01-05 [Swift通天遁地]四、网络和线程-(10)处理图片:压缩、缩放、圆角、CoreImage滤镜、缓存
2019-01-05 [Swift通天遁地]四、网络和线程-(9)上传图片并实时显示上传进度
点击右上角即可分享
微信分享提示
西雅图
10:14发布
西雅图
10:14发布
4°
南风
3级
空气质量
相对湿度
94%
今天
小雨
3°/9°
周二
小雨
3°/9°
周三
中雨
5°/9°