728x90
๋ฐ์ํ
Generate a Countdown of Numbers in an Array
Example
countdown(5) โ [5, 4, 3, 2, 1, 0] countdown(1) โ [1, 0] countdown(0) โ [0]
์ซ์๋ฅผ ๋ฃ์ผ๋ฉด ์ซ์๋ถํฐ 0๊น์ง ๋ฐฐ์ด์์ ๋ฃ์ด์ ๋ณด์ฌ์ค ๊ทธ๋์ ์์๋ ์ซ์ -> 0 ์์ผ๋ก
๋ด ํ์ด
for๋ฌธ์ ์ฌ์ฉํ์ฌ ์ซ์์์ 0๋ถํฐ ์ฐจ๋ก๋๋ก ๋นผ์ ๋ฃ์ด์ฃผ๋ฉด ๋๊ฒ ๋ค ์๊ฐ
func countdown(_ start: Int) -> [Int] {
var arr = [Int]()
for i in 0...start{
arr.append(start-i)
}
return arr
}
๋ ์ข์ ์ ๋ต
ํต์ฌ ๋ reversed() ์์?
func countdown(_ start: Int) -> [Int] {
return (0...start).reversed()
}
728x90
๋ฐ์ํ
'๐ Problem Solution' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค ํ๋ก๊ทธ๋๋ฐ for๋ฌธ 4,5,6,7๋ฒ Swift (0) | 2020.01.21 |
---|---|
๋ฐฑ์คํ๋ก๊ทธ๋๋ฐ for ๋ฌธ 1,2,3๋ฒ Swift (0) | 2020.01.20 |
Edabit Very Easy Swift (0) | 2020.01.18 |
Edabit Very Easy Swift (0) | 2020.01.17 |
๋ฐฑ์คํ๋ก๊ทธ๋๋ฐ 2๋จ๊ณ 1,2,3,4,5,6๋ฒ Swift (0) | 2020.01.16 |
๋๊ธ