Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
asahiocean
LeetCode
Commits
411fa133
Verified
Commit
411fa133
authored
Jul 21, 2021
by
asahiocean
🚀
Browse files
38. Count and Say
https://leetcode.com/problems/count-and-say/
parent
cabd6b9d
Changes
2
Hide whitespace changes
Inline
Side-by-side
String/Count and Say/count-and-say.playground/Contents.swift
0 → 100644
View file @
411fa133
import
Foundation
// 38. Count and Say
// https://leetcode.com/problems/count-and-say/
// Discuss: https://vk.cc/c4a7lg
class
Solution
{
func
countAndSay
(
_
n
:
Int
)
->
String
{
guard
n
>
1
&&
n
<=
30
else
{
return
"1"
}
let
prev
=
countAndSay
(
n
-
1
)
var
curr
=
prev
.
first
!
,
count
=
0
,
result
=
""
prev
.
forEach
{
if
$0
!=
curr
{
result
+=
"
\(
count
)\(
curr
)
"
count
=
1
curr
=
$0
}
else
{
count
+=
1
}
}
result
+=
"
\(
count
)\(
curr
)
"
return
result
}
}
import
XCTest
// Executed 2 tests, with 0 failures (0 unexpected) in 0.006 (0.008) seconds
class
Tests
:
XCTestCase
{
private
let
s
=
Solution
()
func
test0
()
{
XCTAssertEqual
(
s
.
countAndSay
(
1
),
"1"
)
}
func
test1
()
{
XCTAssertEqual
(
s
.
countAndSay
(
4
),
"1211"
)
}
}
Tests
.
defaultTestSuite
.
run
()
String/Count and Say/count-and-say.playground/contents.xcplayground
0 → 100644
View file @
411fa133
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground
version=
'5.0'
target-platform=
'ios'
>
<timeline
fileName=
'timeline.xctimeline'
/>
</playground>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment