Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitce77879

Browse files
author
Boudhayan Biswas
committed
Add encode decode strings
1 parent1b1cd4d commitce77879

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎swift/Encode-Decode-Strings.swift‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
classCodec{
3+
func encode(_ strs:[String])->String{
4+
if strs.isEmpty{return"#"}
5+
varcounts=[String]()
6+
forstrin strs{
7+
counts.append("\(str.count)")
8+
}
9+
return counts.joined(separator:",")+"#"+ strs.joined()
10+
}
11+
12+
func decode(_ s:String)->[String]{
13+
if s=="#"{return[]}
14+
letindex= s.firstIndex(of:"#")!
15+
letcounts=String(s[s.startIndex...s.index(before: index)]).components(separatedBy:",")
16+
varsIndex= s.index(after: index)
17+
vardecodedStrings=[String]()
18+
forcountin counts{
19+
letendIndex= s.index(sIndex, offsetBy:Int(count)!-1)
20+
if sIndex> endIndex{
21+
decodedStrings.append("")
22+
continue
23+
}
24+
decodedStrings.append(String(s[sIndex...endIndex]))
25+
sIndex= s.index(after: endIndex)
26+
}
27+
return decodedStrings
28+
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp