{"id":78,"date":"2019-01-05T17:01:45","date_gmt":"2019-01-05T09:01:45","guid":{"rendered":"http:\/\/132.145.126.25\/?p=78"},"modified":"2019-04-24T10:40:28","modified_gmt":"2019-04-24T02:40:28","slug":"swift-pointer-zh","status":"publish","type":"post","link":"https:\/\/www.tsihyoung.com\/index.php\/2019\/01\/05\/swift-pointer-zh\/","title":{"rendered":"swift\u6307\u6a19\uff08\u6307\u91dd\uff09\u554f\u984c"},"content":{"rendered":"\n<p>swift\u8a9e\u8a00\u4e0d\u9f13\u52f5\u4f7f\u7528\u6307\u6a19\uff0c\u56e0\u6b64\u4e0d\u63d0\u4f9b\u6709\u5982C\/C++\u4e2d\u7684*\u65b9\u6cd5\u3002\u4f46\u6709\u4e9b\u6642\u5019\u6211\u5011\u4e5f\u9700\u8981\u76f4\u63a5\u4fee\u6539\u5167\u5b58\u5730\u5740\u4e0a\u7684\u6578\u64da\uff0c\u7232\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u4e3b\u8981\u6709\u5169\u7a2e\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f7f\u7528\u4e0d\u5b89\u5168\u7684\u6307\u6a19\uff1a\u6309\u662f\u5426\u53ef\u4fee\u6539\u3001\u662f\u5426\u4f7f\u7528\u539f\u59cb\u6578\u64da\u3001\u662f\u5426\u7ba1\u7406\u7de9\u885d\u5340\u5206\u7232\u5982\u4e0b8\u7a2e\u985e\u578b<\/li><\/ul>\n\n\n\n<table class=\"wp-block-table aligncenter is-style-stripes\" style=\"word-break:break-all;word-wrap:break-all;table-layout:fixed\"><tbody><tr><td><\/td><td>\u4e0d\u53ef\u4fee\u6539<\/td><td>\u53ef\u4fee\u6539<\/td><td>\u7de9\u885d<\/td><td>\u4fee\u6539\u53ca\u7de9\u885d<\/td><\/tr><tr><td>\u539f\u59cb\u6578\u64da<\/td><td><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/unsaferawpointer\">UnsafeRawPointer<\/a><\/td><td><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/unsafemutablerawpointer\">UnsafeMutableRawPointer<\/a><\/td><td><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/unsaferawbufferpointer\">UnsafeRawBufferPointer<\/a><\/td><td><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/unsafemutablerawbufferpointer\">UnsafeMutableRawBufferPointer<\/a><\/td><\/tr><tr><td>\u985e\u578b\u6578\u64da<\/td><td><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/unsafepointer\">UnsafePointer<\/a><\/td><td><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/unsafemutablebufferpointer\">UnsafeMutablePointer<\/a><\/td><td><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/unsafebufferpointer\">UnsafeBufferPointer<\/a><\/td><td><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/unsafemutablebufferpointer\">UnsafeMutableBufferPointer<\/a><\/td><\/tr><\/tbody><\/table>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f7f\u7528\u985e\uff1aClass\u3002<\/li><\/ul>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cswift\u4e26\u4e0d\u6703\u81ea\u52d5\u7ba1\u7406\u4e0d\u5b89\u5168\u6307\u6a19\u7684\u5167\u5b58\uff0c\u5373\u4f7f\u7528\u8005\u9700\u8981\u81ea\u884c\u5206\u914d\u5167\u5b58\u3001\u521d\u59cb\u5316\u5167\u5bb9\u3001\u91cb\u653e\u5167\u5bb9\u4ee5\u53ca\u91cb\u653e\u5167\u5b58\uff0c\u9019\u4e00\u9ede\u8207C\u8f03\u7232\u76f8\u4f3c\u3002\u56e0\u6b64\u4f7f\u7528\u4e0d\u5b89\u5168\u6307\u6a19\u6642\u61c9\u7279\u5225\u5c0f\u5fc3\u3002<\/p>\n\n\n\n<p>\u7232\u5b89\u5168\u8d77\u898b\uff0c\u5982\u679c\u53ea\u9700\u8981\u5728\u7a0b\u5f0f\u4e2d\u4fee\u6539\u67d0\u4e9b\u4f4d\u7f6e\u7684\u503c\uff0c\u53ef\u8003\u616e\u4f7f\u7528\u985e\u3002\u7576\u985e\u5be6\u4f8b\u4f5c\u7232\u53c3\u6578\u50b3\u905e\u6642\uff0c\u5be6\u969b\u4e0a\u662f\u50b3\u905e\u7684\u53c3\u8003\u800c\u975e\u62f7\u8c9d\u3002\u5229\u7528\u9019\u500b\u7279\u6027\uff0c\u6211\u5011\u53ef\u4ee5\u5be6\u73fe\u4e00\u90e8\u5206\u6307\u6a19\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u5728\u4e0b\u9762\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u53ea\u6709\u4e00\u500b\u6574\u578b\u8b8a\u91cf\u7684\u985eclass ref\uff0c\u4e26\u5c07\u8a72\u985e\u7684\u5be6\u4f8b\u4f5c\u7232\u53c3\u6578\u50b3\u905e\u7d66\u51fd\u6578func classVar(_ input: ref)\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ref {\n    var x: Int\n    \n    init(_ x: Int) {\n        self.x = x\n    }\n}\n\nfunc classVar(_ input: ref) {\n    input.x = -1\n}\n\nlet input = ref(1)\nclassVar(input)\nprint(input.x)\n\n\/\/output: -1<\/code><\/pre>\n\n\n\n<p>\u5728\u9019\u88cf\uff0c\u5118\u7ba1\u5be6\u4f8binput\u662f\u5e38\u503c\u578b\uff08let\uff09\uff0c\u4f46\u7531\u65bc\u985e\u5be6\u4f8b\u53c3\u6578\u662f\u4ee5\u53c3\u8003\u50b3\u905e\uff0c\u56e0\u6b64\u51fd\u6578classVar\u53ef\u4ee5\u4fee\u6539input\u4e2d\u7684\u8b8a\u91cfvar x\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4e0d\u4f7f\u7528\u985e\uff0c\u5247\u9808\u8981\u4fee\u6539\u50b3\u5165\u53c3\u6578\u7684\u5c6c\u6027\u7232inout\uff0c\u9019\u6a23\u666e\u901a\u53c3\u6578\u6703\u4ee5<a href=\"https:\/\/docs.swift.org\/swift-book\/ReferenceManual\/Declarations.html#ID545\">copy-in copy-out<\/a>\u6a5f\u5236\u50b3\u905e\uff08\u53ef\u80fd\u6703\u88ab\u512a\u5316\u70ba\u53c3\u8003\u50b3\u905e\uff09\u3002\u4f46\u6b64\u6642\u50b3\u5165\u7684\u53c3\u6578\u5fc5\u9808\u7232\u53ef\u4fee\u6539\uff08mutable\uff09\u8b8a\u91cf\u7684\u5730\u5740\uff0c\u5373\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var input = 1\n\nfunc normalVar(_ input: inout Int) {\n    input = -1\n}\n\nnormalVar(&amp;input)\nprint(input)\n\n\/\/output: -1<\/code><\/pre>\n\n\n\n<p>\u4e0b\u9762\u6211\u5011\u518d\u770b\u4e00\u500b\u4f8b\u5b50\u3002\u5728\u9019\u500b\u4f8b\u5b50\u88cf\uff0c\u6211\u5011\u7528\u5169\u7a2e\u65b9\u6cd5\u65b0\u5efa\u5169\u500bref\u578b\u6578\u7d44\uff0c\u7136\u5f8c\u53d6\u5f97\u4e26\u4fee\u6539\u5b83\u5011\u6700\u5f8c\u4e00\u500b\u5143\u7d20\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let classArray1 = [ref(1),ref(1)]\nlet classArray2 = [ref](repeating: ref(1), count: 2)\n\nlet arrayLast1 = classArray1.last!\nlet arrayLast2 = classArray2.last!\n\narrayLast1.x = -1\narrayLast2.x = -1\n\nfor element in classArray1 {\n    print(element.x)\n}\n\nfor element in classArray2 {\n    print(element.x)\n}\n\n\/\/output: classArray1: 1, -1; classArray2: -1, -1<\/code><\/pre>\n\n\n\n<p>\u53ef\u898b\uff0c\u7531\u65bc\u985e\u5be6\u4f8b\u50b3\u905e\u53c3\u8003\uff0carrayLast\u8207\u6578\u7d44\u6700\u5f8c\u4e00\u500b\u5143\u7d20\u5171\u7528\u5167\u5b58\u5730\u5740\uff0c\u9019\u8207\u5176\u4ed6\u985e\u578b\uff08\u5982\u6578\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u6709\u5f88\u5927\u4e0d\u540c\u3002\u800c\u5c0d\u65bc\u7b2c\u4e8c\u7a2e\u521d\u59cb\u5316\u65b9\u6cd5 (repeating: Element, count: Int)\uff0c\u5176\u91cd\u8907\u5143\u7d20\u662f\u5be6\u4f8b\u7684\u53c3\u8003\uff0c\u4f7f\u7528\u6b64\u65b9\u6cd5\u6642\u9808\u5c0f\u5fc3\u3002<\/p>\n\n\n\n<p>\u5229\u7528\u985e\u5be6\u4f8b\u50b3\u905e\u53c3\u8003\u7684\u7279\u6027\uff0c\u6211\u5011\u53ef\u4ee5\u7528\u985e\u4f86\u5b9a\u7fa9\u93c8\u8868\u3001\u6a39\u7b49\u7d50\u69cb\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class linkedList {\n    var val: Int\n    var next: linkedList?\n    init(_ val: Int, _ next: linkedList?) {\n        self.val = val\n        self.next = next\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u672c\u8eab\u4f5c\u7232\u6578\u64da\u7d50\u69cb\u985e\u578b\uff0c\u4f7f\u7528\u7d50\u69cb\uff08struct\uff09\u5b9a\u7fa9\u66f4\u7232\u5408\u7406\u3002\u4f46\u662fswift\u4e2d\u7d50\u69cb\u5be6\u4f8b\u662f\u6309\u62f7\u8c9d\u50b3\u905e\uff0c\u56e0\u6b64\u6211\u5011\u5fc5\u9808\u8f49\u800c\u4f7f\u7528\u985e\u4f86\u5b9a\u7fa9\u5b83\u5011\u3002<\/p>\n\n\n\n<p>\u4f5c\u7232\u9019\u90e8\u5206\u7684\u61c9\u7528\uff0c\u6211\u5011\u4f86\u770bLeetCode 110\u984c<a href=\"https:\/\/leetcode.com\/problems\/balanced-binary-tree\/\">\u5e73\u8861\u6a39<\/a>\u3002\u5728\u9673\u751f\u7d66\u51fa\u7684\u5f8c\u5e8f\u904d\u6b77<a href=\"https:\/\/github.com\/ChenYangyao\/project-leetcode-solution\/blob\/master\/cpp\/problem110.cpp\">cpp\u89e3\u7b54<\/a>\u4e2d\uff0c\u7528\u5230\u4e86\u6307\u6a19&amp;height\u53bb\u4fee\u6539\u6bcd\u7bc0\u9ede\u7684lheight\u6216rheight\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bool isBalanced(TreeNode* root) {\n    stack&lt;StkItem> stk;\n    int height = 0;\n    if( root ) stk.emplace( root, height );\n    while( !stk.empty() ){\n        auto &amp;top = stk.top();\n        if( top.is_visited ){\n            if( top.lheight - top.rheight > 1 || top.rheight - top.lheight > 1 ) \n                return false;\n            top.height = max(top.lheight, top.rheight) + 1;\/\/\u6b64\u8655\u901a\u904e\u6307\u6a19\u4fee\u6539\u6bcd\u7bc0\u9ede\u7684lheight\u6216rheight\u3002\n            stk.pop();\n        }else{\n            if( top.p->right ) stk.emplace( top.p->right, top.rheight );\n            if( top.p->left ) stk.emplace( top.p->left, top.lheight );\n            top.is_visited = true;\n        }\n    }\n    return true;\n}\n\nstruct StkItem{\n    TreeNode *p;\n    bool is_visited = false;\n    int lheight = 0, rheight = 0;\n    int &height;\n    StkItem( TreeNode *_p, int &amp;_height ): p(_p), height(_height){}\n};<\/code><\/pre>\n\n\n\n<p>\u5728<a href=\"https:\/\/github.com\/ChenYangyao\/project-leetcode-solution\/blob\/master\/swift\/problem110.swift\">swift\u7248\u672c<\/a>\u4e2d\uff0c\u6211\u5011\u5c07lheight\u548crheight\u5b9a\u7fa9\u7232\u4e00\u500b\u8d85\u985eheight\uff0c\u4e26\u5c07stackItem\u5b9a\u7fa9\u7232height\u7684\u5b50\u985e\u4f86\u7232\u5b50\u7bc0\u9ede\u7372\u53d6\u6a39\u9ad8\u8b8a\u91cf\u3002stackItem\u4e2d\u5f15\u5165\u5143\u7d20parent: height\u4ee5\u4fbf\u4e4b\u5f8c\u4fee\u6539\u6bcd\u7bc0\u9ede\u7684\u6a39\u9ad8\uff1b\u5f15\u5165direction\u8b8a\u91cf\u4f86\u78ba\u5b9a\u67d0\u5b50\u7bc0\u9ede\u662f\u6bcd\u7bc0\u9ede\u7684\u5de6\u6216\u53f3\u7bc0\u9ede\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private class height {\n    var leftHeight: Int = 0\n    var rightHeight: Int = 0\n}\n\nprivate class stackItem: height {\n    let p: TreeNode\n    var visited: Bool = false\n    var direction: Bool\n    var parent: height\n    \n    init(_ p: TreeNode, _ parent: height, _ direction: Bool) {\n        self.p = p\n        self.parent = parent\n        self.direction = direction\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u521d\u59cb\u5316\u5be6\u4f8bstackItem(top.p.left!, top, false)\u6642\uff08\u8a18\u7232s\uff09\uff0c\u53ef\u4ee5\u5c07\u5b50\u985etop\u50b3\u7d66\u8d85\u985eheight\u3002\u6b64\u6642top\u4e2d\u53ea\u6709\u8d85\u985e\u6210\u54e1\u6703\u88ab\u50b3\u905e\uff0c\u4e26\u5728s\u4e2d\u751f\u6210\u4e00\u500b\u6307\u5411{top.leftHeight, top.rightHeight}\u7684\u8d85\u985e\u5be6\u4f8b\uff0c\u76f8\u7576\u65bcs.parent.leftHeight = &amp;top.leftHeight; s.parent.rightHeight = &amp;top.rightHeight\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func isBalanced(_ root: TreeNode?) -> Bool {\n    if (root == nil) {\n        return true\n    }\n    \n    var stack = [stackItem]()\n    stack.append(stackItem(root!,height(),false))\n    \n    while !stack.isEmpty {\n        let top = stack.last!\n        if top.visited {\n            if ((top.leftHeight - top.rightHeight) > 1 || (top.rightHeight - top.leftHeight) > 1) {\n                return false\n            }\n            let parentHeight = max(top.leftHeight,top.rightHeight) + 1\n            if top.direction { \/\/\u4fee\u6539\u6bcd\u7bc0\u9ede\u6a39\u9ad8\n                top.parent.rightHeight = parentHeight\n            } else {\n                top.parent.leftHeight = parentHeight\n            }\n            _ = stack.removeLast()\n        } else {\n            if (top.p.left != nil) {\n                stack.append(stackItem(top.p.left!,top,false))\n            }\n            if (top.p.right != nil) {\n                stack.append(stackItem(top.p.right!,top,true))\n            }\n            top.visited = true\n        }\n    }\n    return true\n}<\/code><\/pre>\n\n\n\n<p>\u9019\u6a23\u6211\u5011\u5c31\u901a\u904e\u985e\u5be6\u73fe\u4e86\u6307\u6a19\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>swift\u8a9e\u8a00\u4e0d\u9f13\u52f5\u4f7f\u7528\u6307\u6a19\uff0c\u56e0\u6b64\u4e0d\u63d0\u4f9b\u6709\u5982C\/C++\u4e2d\u7684*\u65b9\u6cd5\u3002\u4f46\u6709\u4e9b\u6642\u5019\u6211\u5011\u4e5f\u9700\u8981\u76f4\u63a5\u4fee\u6539\u5167\u5b58\u5730\u5740\u4e0a\u7684\u6578\u64da\uff0c\u7232\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u4e3b\u8981\u6709\u5169\u7a2e\u65b9\u6cd5\uff1a \u4f7f\u7528\u4e0d\u5b89\u5168\u7684\u6307\u6a19\uff1a\u6309\u662f\u5426\u53ef\u4fee\u6539\u3001\u662f\u5426\u4f7f\u7528\u539f\u59cb\u6578\u64da\u3001\u662f\u5426\u7ba1\u7406\u7de9\u885d\u5340\u5206\u7232\u5982\u4e0b8\u7a2e\u985e\u578b \u4e0d\u53ef\u4fee\u6539 \u53ef\u4fee\u6539 \u7de9\u885d \u4fee\u6539\u53ca\u7de9\u885d \u539f\u59cb\u6578\u64da UnsafeRawPointer UnsafeMutableRawPointer UnsafeRawBufferPointer UnsafeMutableRawBufferPointer \u985e\u578b\u6578\u64da UnsafePointer UnsafeMutablePointer UnsafeBufferPointer UnsafeMutableBufferPointer \u4f7f\u7528\u985e\uff1aClass\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cswift\u4e26\u4e0d\u6703\u81ea\u52d5\u7ba1\u7406\u4e0d\u5b89\u5168\u6307\u6a19\u7684\u5167\u5b58\uff0c\u5373\u4f7f\u7528\u8005\u9700\u8981\u81ea\u884c\u5206\u914d\u5167\u5b58\u3001\u521d\u59cb\u5316\u5167\u5bb9\u3001\u91cb\u653e\u5167\u5bb9\u4ee5\u53ca\u91cb\u653e\u5167\u5b58\uff0c\u9019\u4e00\u9ede\u8207C\u8f03\u7232\u76f8\u4f3c\u3002\u56e0\u6b64\u4f7f\u7528\u4e0d\u5b89\u5168\u6307\u6a19\u6642\u61c9\u7279\u5225\u5c0f\u5fc3\u3002 \u7232\u5b89\u5168\u8d77\u898b\uff0c\u5982\u679c\u53ea\u9700\u8981\u5728\u7a0b\u5f0f\u4e2d\u4fee\u6539\u67d0\u4e9b\u4f4d\u7f6e\u7684\u503c\uff0c\u53ef\u8003\u616e\u4f7f\u7528\u985e\u3002\u7576\u985e\u5be6\u4f8b\u4f5c\u7232\u53c3\u6578\u50b3\u905e\u6642\uff0c\u5be6\u969b\u4e0a\u662f\u50b3\u905e\u7684\u53c3\u8003\u800c\u975e\u62f7\u8c9d\u3002\u5229\u7528\u9019\u500b\u7279\u6027\uff0c\u6211\u5011\u53ef\u4ee5\u5be6\u73fe\u4e00\u90e8\u5206\u6307\u6a19\u7684\u529f\u80fd\u3002 \u5728\u4e0b\u9762\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u53ea\u6709\u4e00\u500b\u6574\u578b\u8b8a\u91cf\u7684\u985eclass ref\uff0c\u4e26\u5c07\u8a72\u985e\u7684\u5be6\u4f8b\u4f5c\u7232\u53c3\u6578\u50b3\u905e\u7d66\u51fd\u6578func classVar(_ input: ref)\uff1a \u5728\u9019\u88cf\uff0c\u5118\u7ba1\u5be6\u4f8binput\u662f\u5e38\u503c\u578b\uff08let\uff09\uff0c\u4f46\u7531\u65bc\u985e\u5be6\u4f8b\u53c3\u6578\u662f\u4ee5\u53c3\u8003\u50b3\u905e\uff0c\u56e0\u6b64\u51fd\u6578classVar\u53ef\u4ee5\u4fee\u6539input\u4e2d\u7684\u8b8a\u91cfvar x\u3002 \u5982\u679c\u4e0d\u4f7f\u7528\u985e\uff0c\u5247\u9808\u8981\u4fee\u6539\u50b3\u5165\u53c3\u6578\u7684\u5c6c\u6027\u7232inout\uff0c\u9019\u6a23\u666e\u901a\u53c3\u6578\u6703\u4ee5copy-in copy-out\u6a5f\u5236\u50b3\u905e\uff08\u53ef\u80fd\u6703\u88ab\u512a\u5316\u70ba\u53c3\u8003\u50b3\u905e\uff09\u3002\u4f46\u6b64\u6642\u50b3\u5165\u7684\u53c3\u6578\u5fc5\u9808\u7232\u53ef\u4fee\u6539\uff08mutable\uff09\u8b8a\u91cf\u7684\u5730\u5740\uff0c\u5373\uff1a \u4e0b\u9762\u6211\u5011\u518d\u770b\u4e00\u500b\u4f8b\u5b50\u3002\u5728\u9019\u500b\u4f8b\u5b50\u88cf\uff0c\u6211\u5011\u7528\u5169\u7a2e\u65b9\u6cd5\u65b0\u5efa\u5169\u500bref\u578b\u6578\u7d44\uff0c\u7136\u5f8c\u53d6\u5f97\u4e26\u4fee\u6539\u5b83\u5011\u6700\u5f8c\u4e00\u500b\u5143\u7d20\u3002 \u53ef\u898b\uff0c\u7531\u65bc\u985e\u5be6\u4f8b\u50b3\u905e\u53c3\u8003\uff0carrayLast\u8207\u6578\u7d44\u6700\u5f8c\u4e00\u500b\u5143\u7d20\u5171\u7528\u5167\u5b58\u5730\u5740\uff0c\u9019\u8207\u5176\u4ed6\u985e\u578b\uff08\u5982\u6578\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u6709\u5f88\u5927\u4e0d\u540c\u3002\u800c\u5c0d\u65bc\u7b2c\u4e8c\u7a2e\u521d\u59cb\u5316\u65b9\u6cd5 (repeating: Element, count: Int)\uff0c\u5176\u91cd\u8907\u5143\u7d20\u662f\u5be6\u4f8b\u7684\u53c3\u8003\uff0c\u4f7f\u7528\u6b64\u65b9\u6cd5\u6642\u9808\u5c0f\u5fc3\u3002 \u5229\u7528\u985e\u5be6\u4f8b\u50b3\u905e\u53c3\u8003\u7684\u7279\u6027\uff0c\u6211\u5011\u53ef\u4ee5\u7528\u985e\u4f86\u5b9a\u7fa9\u93c8\u8868\u3001\u6a39\u7b49\u7d50\u69cb\uff0c\u4f8b\u5982\uff1a \u672c\u8eab\u4f5c\u7232\u6578\u64da\u7d50\u69cb\u985e\u578b\uff0c\u4f7f\u7528\u7d50\u69cb\uff08struct\uff09\u5b9a\u7fa9\u66f4\u7232\u5408\u7406\u3002\u4f46\u662fswift\u4e2d\u7d50\u69cb\u5be6\u4f8b\u662f\u6309\u62f7\u8c9d\u50b3\u905e\uff0c\u56e0\u6b64\u6211\u5011\u5fc5\u9808\u8f49\u800c\u4f7f\u7528\u985e\u4f86\u5b9a\u7fa9\u5b83\u5011\u3002 \u4f5c\u7232\u9019\u90e8\u5206\u7684\u61c9\u7528\uff0c\u6211\u5011\u4f86\u770bLeetCode 110\u984c\u5e73\u8861\u6a39\u3002\u5728\u9673\u751f\u7d66\u51fa\u7684\u5f8c\u5e8f\u904d\u6b77cpp\u89e3\u7b54\u4e2d\uff0c\u7528\u5230\u4e86\u6307\u6a19&amp;height\u53bb\u4fee\u6539\u6bcd\u7bc0\u9ede\u7684lheight\u6216rheight\u3002 \u5728swift\u7248\u672c\u4e2d\uff0c\u6211\u5011\u5c07lheight\u548crheight\u5b9a\u7fa9\u7232\u4e00\u500b\u8d85\u985eheight\uff0c\u4e26\u5c07stackItem\u5b9a\u7fa9\u7232height\u7684\u5b50\u985e\u4f86\u7232\u5b50\u7bc0\u9ede\u7372\u53d6\u6a39\u9ad8\u8b8a\u91cf\u3002stackItem\u4e2d\u5f15\u5165\u5143\u7d20parent: height\u4ee5\u4fbf\u4e4b\u5f8c\u4fee\u6539\u6bcd\u7bc0\u9ede\u7684\u6a39\u9ad8\uff1b\u5f15\u5165direction\u8b8a\u91cf\u4f86\u78ba\u5b9a\u67d0\u5b50\u7bc0\u9ede\u662f\u6bcd\u7bc0\u9ede\u7684\u5de6\u6216\u53f3\u7bc0\u9ede\u3002 \u5728\u521d\u59cb\u5316\u5be6\u4f8bstackItem(top.p.left!, top, false)\u6642\uff08\u8a18\u7232s\uff09\uff0c\u53ef\u4ee5\u5c07\u5b50\u985etop\u50b3\u7d66\u8d85\u985eheight\u3002\u6b64\u6642top\u4e2d\u53ea\u6709\u8d85\u985e\u6210\u54e1\u6703\u88ab\u50b3\u905e\uff0c\u4e26\u5728s\u4e2d\u751f\u6210\u4e00\u500b\u6307\u5411{top.leftHeight, top.rightHeight}\u7684\u8d85\u985e\u5be6\u4f8b\uff0c\u76f8\u7576\u65bcs.parent.leftHeight = &amp;top.leftHeight; s.parent.rightHeight = &amp;top.rightHeight\u3002 \u9019\u6a23\u6211\u5011\u5c31\u901a\u904e\u985e\u5be6\u73fe\u4e86\u6307\u6a19\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"post_formats":[],"languages":[6],"post_translationss":[],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-programme"],"guten_post_layout_featured_media_urls":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"guten_post_layout_landscape_large":false,"guten_post_layout_portrait_large":false,"guten_post_layout_square_large":false,"guten_post_layout_landscape":false,"guten_post_layout_portrait":false,"guten_post_layout_square":false,"sciencexlite-blog-thumb":false},"_links":{"self":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":0,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/tags?post=78"},{"taxonomy":"post_format","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/post_formats?post=78"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/languages?post=78"},{"taxonomy":"post_translations","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/post_translationss?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}