转载

[iOS] Swift 刷 LeetCode 工具方法

Swif-LeetCode-Utils

Provide fast way to create and print for ListNode & TreeNode on the LeetCode. :blush:

If you want to test your Algorithms without the LeetCode,try these Code .

ListNode

public class ListNode {
    public var val: Int
    public var next: ListNode?
    public init(_ val: Int) {
        self.val = val
        self.next = nil
    }
}

Example

//create ,return the head ListNode
var newList = createListNode([1,2,3,4,5,6])

//print ,return the string
var str = printListNode(newList) //" 1 2 3 4 5 6"

TreeNode

public class TreeNode {
    public var val: Int
    public var left: TreeNode?
    public var right: TreeNode?
    public init(_ val: Int) {
        self.val = val
        self.left = nil
        self.right = nil
    }
}

In LeetCode, null means pass the leaf, in this Utils, i use 'nil' to represent a empty leaf.

Using The same order on the LeetCode.

//create. init with a [Int?] array.
var newTreeNode = createTreeNode([3,1,nil,1,nil,3,nil,4])

//print 
var treeStr = printTree(newTreeNode!) // " 3 1 null 1 null 4"

End

If there is any problems or some better solutaion, pls give me a issue.

原文  https://github.com/Danny1451/Swift-LeetCode-Utils
正文到此结束
Loading...