새소식

인기 검색어

프로그래머스/Lv.2

[Swift] 하노이의 탑

  • -
import Foundation func solution(_ n:Int) -> [[Int]] { var result: [[Int]] = [] func hanoi(_ n:Int, _ from: Int, _ by: Int, _ to: Int) { if n == 1 { result.append([from, to]) return } hanoi(n-1, from, to, by) hanoi(1, from, by, to) hanoi(n-1, by, from, to) } hanoi(n, 1, 2, 3) return result }

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.