β¨οΈ Language/swift
[νλ‘κ·Έλλ¨Έμ€ LV.1] μλ°μλ°μλ°μλ°μλ°μ?
hyebin (Helia)
2023. 2. 7. 06:56
λ°μν
νλ‘κ·Έλλ¨Έμ€ LV.1 λͺ¨μ
μλ°μλ°μλ°μλ°μλ°μ?
λ¬Έμ μ€λͺ
κΈΈμ΄κ° nμ΄κ³ , "μλ°μλ°μλ°μ...."μ κ°μ ν¨ν΄μ μ μ§νλ λ¬Έμμ΄μ 리ν΄νλ ν¨μ, solutionμ μμ±νμΈμ. μλ₯Ό λ€μ΄ nμ΄ 4μ΄λ©΄ "μλ°μλ°"μ 리ν΄νκ³ 3μ΄λΌλ©΄ "μλ°μ"λ₯Ό 리ν΄νλ©΄ λ©λλ€.
μ ν μ¬ν
- nμ κΈΈμ΄ 10,000 μ΄νμΈ μμ°μμ λλ€.
μ μΆλ ₯ μ
n | return |
3 | "μλ°μ" |
4 | "μλ°μλ°" |
μ μΆ
func solution(_ n:Int) -> String {
var re = ""
for i in 1...n{
if i % 2 == 1{
re += "μ"
}
else{
re += "λ°"
}
}
return re
}
1λΆν° nκΉμ§ λ°λ³΅λ¬Έμ μ€ννμ¬, νμλΌλ©΄ "μ" μ§μλΌλ©΄ "λ°"μ re λ³μμ μΆκ°νλ€.
λ€λ₯Έ νμ΄
func solution(_ n:Int) -> String {
return "\(String(repeating: "μλ°", count: n / 2))\(n % 2 == 0 ? "" : "μ")"
}
"μλ°"μ n/2 λ§νΌ λ°λ³΅ν ν, nμ΄ νμλΌλ©΄ "μ"λ₯Ό λ€μ μΆκ°νλ€.
λ°μν