Commit 411fa133 by asahiocean 🚀

### 38. Count and Say

`https://leetcode.com/problems/count-and-say/`
parent cabd6b9d
 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()
 \ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!