<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by zxholy on Medium]]></title>
        <description><![CDATA[Stories by zxholy on Medium]]></description>
        <link>https://medium.com/@zxholy?source=rss-266902acf464------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LIUnyhXxWvnO8lxhdmkUCw.png</url>
            <title>Stories by zxholy on Medium</title>
            <link>https://medium.com/@zxholy?source=rss-266902acf464------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 18:19:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zxholy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Golang: panic and recover]]></title>
            <link>https://medium.com/@zxholy/golang-panic-and-recover-c48616f47bb7?source=rss-266902acf464------2</link>
            <guid isPermaLink="false">https://medium.com/p/c48616f47bb7</guid>
            <category><![CDATA[try-catch]]></category>
            <category><![CDATA[exception]]></category>
            <category><![CDATA[recover]]></category>
            <category><![CDATA[panic]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[zxholy]]></dc:creator>
            <pubDate>Tue, 16 Jan 2018 03:58:54 GMT</pubDate>
            <atom:updated>2018-01-16T03:58:54.536Z</atom:updated>
            <content:encoded><![CDATA[<p>在Golang中，虽然有panic and recover的机制，但是感觉在实际使用中，是不建议使用的。或者说是 rarely use。。普通的异常还是要通过返回错误码的方式去处理。</p><p>panic and recover虽然类似java的try-catch机制，但是是用来处理一些npe或者无法继续程序的严重错误的，还有一些runtime error。但是一般golang中，初始化变量，会进行零值初始化，其实是比较好可以避免nil pointer的情况。</p><p>panic and recover一般作用于goroutine上，并且recover需要处理同一个panic的gorountine。recover要和defer配合使用。</p><p>对于这方面描述比较详细的文章，我觉得<a href="https://golangbot.com/panic-and-recover/">这篇</a>写的不错。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c48616f47bb7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[看Golang自带测试库有感]]></title>
            <link>https://medium.com/@zxholy/%E7%9C%8Bgolang%E8%87%AA%E5%B8%A6%E6%B5%8B%E8%AF%95%E5%BA%93%E6%9C%89%E6%84%9F-ece8e6559d9a?source=rss-266902acf464------2</link>
            <guid isPermaLink="false">https://medium.com/p/ece8e6559d9a</guid>
            <dc:creator><![CDATA[zxholy]]></dc:creator>
            <pubDate>Thu, 11 Jan 2018 07:37:17 GMT</pubDate>
            <atom:updated>2018-01-11T07:37:17.404Z</atom:updated>
            <content:encoded><![CDATA[<p>语言约束严格点挺好的，这样官方就可以做很多事情了。有时候灵活的语法，带来的结果不一定高效，包括看别人代码和自己写代码。Golang标准库都包含了测试库，非常方便进行mock测试和单元测试，只要按照约定的规范写Test方法就行，而且版本兼容性好。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ece8e6559d9a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[简单的技术回顾]]></title>
            <link>https://medium.com/@zxholy/%E7%AE%80%E5%8D%95%E7%9A%84%E6%8A%80%E6%9C%AF%E5%9B%9E%E9%A1%BE-f0fbaef51b52?source=rss-266902acf464------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0fbaef51b52</guid>
            <dc:creator><![CDATA[zxholy]]></dc:creator>
            <pubDate>Fri, 05 Jan 2018 04:37:45 GMT</pubDate>
            <atom:updated>2018-01-05T04:37:45.031Z</atom:updated>
            <content:encoded><![CDATA[<p>简单的技术回顾</p><p>2017年，技术方面主要是感受了 Scala 的魅力（Scala 真的是有点复杂，但是很吸引人），用 Spark stack 的一堆东西做了一些数据工作，对 Java 在函数式编程方面有了一些新的认识（一开始的时候还挺期待 Java 9 的，后来就没有太多关注了）。18年，实战一下 Golang 吧，了解一下容器云，学习 ML 的知识。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0fbaef51b52" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[LeetCode] Remove Duplicate Letters]]></title>
            <link>https://medium.com/@zxholy/leetcode-remove-duplicate-letters-78aeebc4eaae?source=rss-266902acf464------2</link>
            <guid isPermaLink="false">https://medium.com/p/78aeebc4eaae</guid>
            <category><![CDATA[string]]></category>
            <category><![CDATA[greedy]]></category>
            <category><![CDATA[stack]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[zxholy]]></dc:creator>
            <pubDate>Thu, 14 Dec 2017 05:12:16 GMT</pubDate>
            <atom:updated>2017-12-14T05:12:54.008Z</atom:updated>
            <content:encoded><![CDATA[<p>首先要读懂题目，刚开始，读了一遍，觉得只要先去重，再按照字典顺序排序就行了。</p><blockquote>the smallest in lexicographical order among all possible results</blockquote><p>正确的理解是：去重后，要保留字母在字符串中的相对顺序，并且尽量按照字典最小顺序排序。</p><p>这道题目想了很久，果然是hard级别的，没有什么思路。</p><p>看了提示，解决这个问题的算法是用贪心算法（Greedy），关键思路是：<strong>对于子串求最优，然后不断扩大计算范围，直至完整的字符串。</strong></p><p>有两种解决方法：</p><ol><li>求出字符串中，<strong>每个字符最后出现的位置</strong>，然后依次从前往后（index从小到大）求出每个子串最优解，即最小的字符，直到完整字符串，得出结果。</li><li>使用stack存储最终计算结果。计算过程中，依次遍历字符串中的字符，<strong>越小的字符，越沉到栈底</strong>。最终stack中的元素，从栈底到栈顶的字符排序，就是最终的结果。</li></ol><p>下面给出这两种解法的代码。</p><p>方法一：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4570b11a41a0d58c2281b8bf874ab6a6/href">https://medium.com/media/4570b11a41a0d58c2281b8bf874ab6a6/href</a></iframe><p>有几个点要注意：</p><ul><li>charLastIndexMap用来保存每个字符最后出现的位置。charLastIndexMap的大小，就是一共有多少独立字符。</li><li>子串的范围是 [start, end]，前后都是闭合的，包括end那个位置。</li><li>找到minChar后，要从charLastIndexMap里去掉这个字符。并且start的位置要移动到当前这个minChar+1的位置。</li></ul><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>方法二：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/814bcadf1f15f83dba93c03b59403faf/href">https://medium.com/media/814bcadf1f15f83dba93c03b59403faf/href</a></iframe><p>Tips：</p><ul><li>counter用来保存每个字符出现的次数。</li><li>stack用来保存最后的结果，越小的元素，越沉底。</li><li>每次取出一个字符后，一定要先在counter map里减1。</li><li>如果栈顶的元素比当前元素大，而且后面还有栈顶这个元素（count&gt;0），那就pop栈顶元素。</li><li>最后的结果是从小到大的一个串。stack里如果是pop出来的结果，需要builder.reverse一下。这里用的是stream的foreach，是从栈底到栈顶的，所以不用reverse。</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78aeebc4eaae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[LeetCode] Word Search II]]></title>
            <link>https://medium.com/@zxholy/leetcode-word-search-ii-7bfbf43f3c88?source=rss-266902acf464------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bfbf43f3c88</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[trie-tree]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[string]]></category>
            <category><![CDATA[search]]></category>
            <dc:creator><![CDATA[zxholy]]></dc:creator>
            <pubDate>Mon, 11 Dec 2017 10:23:52 GMT</pubDate>
            <atom:updated>2017-12-11T10:23:52.474Z</atom:updated>
            <content:encoded><![CDATA[<p>搞定了<a href="https://medium.com/@zxholy/leetcode-word-search-8a5eef31427f">Word Search</a>，就想接着看升级版本，看到题目类似，主要区别就是输入从一个词，变成了一组词。明知道还继续用之前的算法，肯定是不行，但是想跑一下看，会在什么地方上有瓶颈。于是简单粗暴的改了一下之前的算法，提了一版：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23fcab68359fe44accce8f205f712a31/href">https://medium.com/media/23fcab68359fe44accce8f205f712a31/href</a></iframe><p>其实这个代码还是改了一下的，因为踩到了一个坑，就是元素重复的情况。说不明白，看一下错误就明白了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gv9sPY2Bz2Z2jsuYVdDqqg.png" /></figure><p>然后才把最后的返回结果改成了：<strong>result.stream()<br> .collect(Collectors.</strong><em>toList</em><strong>())</strong>;</p><p>最后卡在了一个test case上：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bXlc1HOOyLOZOLxGE-bpkQ.png" /></figure><p>因为截图的尺寸问题，整个输入的字符串数组，是非常多的。。思考，不知道怎么解决。看了hint，提示用Trie去做，感觉靠谱，于是我先写了一个<a href="https://medium.com/@zxholy/leetcode-implement-trie-prefix-tree-567e0ae94109">Trie的数据结构</a>，然后再配合去解决这个题目。</p><p>适用trie的思路就是，构造一个trie tree，然后去查找，看有没有命中的word。</p><p>但是一开始我的思路错误，是去依据二维数组去构造这个tree，导致。。非常复杂。。因为一个二维数组，构造这个trie tree就是非常多可能性，几乎是不可能的事情。。。（我是这么认为的）但是我当时没有想那么多，还是尝试了一下，然后去search(word)看能不能找到。。自然是失败了。</p><p>正确的思路应该是根据words数组去构造trie tree，然后再去二维数组里，search看有没有正确的路径。找到正确的路径，就是所有匹配的词了。其中用的visited数组，其实就是剪枝（pruning）策略的一种。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/831986aa7381e58d22e1bd9dc685c3ec/href">https://medium.com/media/831986aa7381e58d22e1bd9dc685c3ec/href</a></iframe><p>当然，也开始看最火的<a href="https://discuss.leetcode.com/topic/33246/java-15ms-easiest-solution-100-00/2">一篇帖子</a>。可以研究一下，如何极致优化代码。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bfbf43f3c88" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[LeetCode] Implement Trie (Prefix Tree)]]></title>
            <link>https://medium.com/@zxholy/leetcode-implement-trie-prefix-tree-567e0ae94109?source=rss-266902acf464------2</link>
            <guid isPermaLink="false">https://medium.com/p/567e0ae94109</guid>
            <category><![CDATA[trie-tree]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[prefix-tree]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[aho-corasick]]></category>
            <dc:creator><![CDATA[zxholy]]></dc:creator>
            <pubDate>Mon, 11 Dec 2017 07:29:47 GMT</pubDate>
            <atom:updated>2017-12-12T03:35:12.009Z</atom:updated>
            <content:encoded><![CDATA[<p>做到这个题目，是因为在解决 <a href="https://medium.com/@zxholy/leetcode-word-search-ii-7bfbf43f3c88">Word Search II</a> 的问题时，根据hint过来的。</p><p>其实，我之前在iqiyi做评论关键词识别的时候，是用了<a href="https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm">AC多模匹配算法</a>来辅助的，其中关键的一个数据结构就是<a href="https://en.wikipedia.org/wiki/Trie">Trie Tree</a>。过了这么久了，猛的一看，居然记不得怎么做了。。还是静下来细想了一下，写了一个版本，顺利的一遍就通过了。下面贴一下我的代码：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b93220a3121e16d75ae596191f0f36e9/href">https://medium.com/media/b93220a3121e16d75ae596191f0f36e9/href</a></iframe><p>上面的代码比较简单，没太多要说的。</p><p>后来我在跑Word Search II的算法时，用了这个版本的Trie，居然出现了超时的情况。。有点醉，在LC自家论坛里翻到了另外一个版本的Trie，其实就是把childs（这里写的有问题。。应该叫children，😂，我的错）从hashset改成了char 数组，对应的node的方法，也简单粗暴地改成了计算数组值的问题。然后我在Word Search II里用了这个char数组版本的Trie，居然过了。。内心一阵草尼玛呼啸而过。先贴一下这个版本的代码：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d965da4f048e19dd0fb1ae43447d7e67/href">https://medium.com/media/d965da4f048e19dd0fb1ae43447d7e67/href</a></iframe><p>虽然我叫它FastTrie，这只是为了区分我的版本，哈哈。其实这个Fast在我心里是要打引号的，因为这个版本为了速度牺牲了通用性。很显然，只是适用英文字符！但是，又想想，毕竟这只是一个局限性很强的算法题目，并不适用于工程中的开发，为了解决超时问题，这么做，也是可以的。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=567e0ae94109" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[LeetCode] Word Search]]></title>
            <link>https://medium.com/@zxholy/leetcode-word-search-8a5eef31427f?source=rss-266902acf464------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a5eef31427f</guid>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[backtracking]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[dfs]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[zxholy]]></dc:creator>
            <pubDate>Wed, 06 Dec 2017 09:59:06 GMT</pubDate>
            <atom:updated>2017-12-06T09:59:06.992Z</atom:updated>
            <content:encoded><![CDATA[<p>这个题目，题都没有看清，就开始做了。。忽略了里面的条件，以为只是简单的判断是否存在，用了一个Map就撸完了。。=。= 反省一下。不过居然跑过了2/3的testcase，有点😱。</p><p>有两个重要的条件，一个是adjacent，就是必须要相邻。还有一个是每一个元素只能使用一次。对于这两个条件，相邻就是简单的位置判断，元素只能使用一次，就是再搞一个同样的二维数组，记录一下visit的状态即可。</p><p>解决思路就是用深搜加回溯（DFS+Backtracking），还是像解决<a href="https://medium.com/@zxholy/leetcode-decode-string-e5ae9495bb84">Decode String</a>那个题目一样，我对这种类似DFS感觉其实就是递归。而回溯的思想是分步分解问题，也是在DFS的过程中完成的，常见的解法同样是递归，如果出现了不符合条件的，则返回到上层方法中继续。DFS的关键是构造一个统一的输入，还有明确边界条件。</p><p>我在写完代码后，debug了一下testcase，还是发现了一些问题。但是对于有递归的方法不容易测试，建议加上中间过程的日志，便于调试。下面贴出来ac的代码：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee02dbe751d7398d4f8263d1418c7357/href">https://medium.com/media/ee02dbe751d7398d4f8263d1418c7357/href</a></iframe><p>有一些关键点需要注意：</p><ul><li>定义rows和cols是别搞错了。。我就开始的时候搞错了。</li><li>search方法的退出条件里，有一个是wordIndex到了word的边界，这时候应该是返回true。因为都搜索完整个串了，认为是完全匹配上了。我开始的时候，写成false了，认为是超出了边界。。这样导致的结果会永远搜索都不会解释（搜的是最后一个字符）。</li><li>在开始判断时，要先把visited map里对应的位置设置true，如果这个位置不满足，再把标记改回去。</li></ul><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>题目不算难，只要细心一些还是可以搞定的，注意边界条件。但是感觉这个题目的应用还是可以挺广的，如何应用的一些发散的想法，写在了<a href="https://medium.com/@zxholy/the-application-of-leetcode-algorithms-3a03d9e80961">前面的文章</a>里了。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a5eef31427f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The application of LeetCode algorithms]]></title>
            <link>https://medium.com/@zxholy/the-application-of-leetcode-algorithms-3a03d9e80961?source=rss-266902acf464------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a03d9e80961</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[application]]></category>
            <category><![CDATA[divergent-thinking]]></category>
            <dc:creator><![CDATA[zxholy]]></dc:creator>
            <pubDate>Tue, 05 Dec 2017 02:46:37 GMT</pubDate>
            <atom:updated>2017-12-05T02:46:37.326Z</atom:updated>
            <content:encoded><![CDATA[<p>不要为了刷题而刷题，也不要为了写算法题而写。关联想象一下，每一道题目，可以有哪些应用？也许对解决日常问题，可以提供全新的思路。</p><p>下面的内容会不定期更新，因为关联了github的gist，所以可以不需要改动这个文章本身，只要更新gist的内容即可。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aacc84cba98855d53181321c5b4c5f48/href">https://medium.com/media/aacc84cba98855d53181321c5b4c5f48/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a03d9e80961" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[LeetCode] Maximum Product Subarray]]></title>
            <link>https://medium.com/@zxholy/leetcode-maximum-product-subarray-e85e06907f8?source=rss-266902acf464------2</link>
            <guid isPermaLink="false">https://medium.com/p/e85e06907f8</guid>
            <category><![CDATA[time-complexity]]></category>
            <category><![CDATA[subarray]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[dynamic-programming]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[zxholy]]></dc:creator>
            <pubDate>Tue, 05 Dec 2017 02:18:18 GMT</pubDate>
            <atom:updated>2017-12-05T02:18:18.444Z</atom:updated>
            <content:encoded><![CDATA[<p>这道题目一看，我以为是求和的。。但是仔细读了一下，发现子集 [2, 3] 的结果居然是6，原来product是求积的意思。心疼自己的渣英语两秒钟。。</p><p>这题目，一看就是DP啊，而且纸上画了一下，感觉状态转移方程也不难。我定义了两个指针: i, j，用来表示连续子集的开头和结尾。然后转移方程是：<strong>func[i, j] = func[i, j-1] * j</strong></p><p>是不是非常简单？5分钟就写完了。跑了一下网上给出的唯一的一个test case：[2,3,-2,4], 果断AC了。然后提交就悲剧了。。挂掉的test case:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*rWow5wLESGIuXRUaLJJ2cQ@2x.png" /></figure><p>额，这是定义了一个temp的比较值，为Integer.MIN_VALUE. 导致的。改了一发，提交又挂了。test case如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*b6_tjyhqwy32ALON-TNn-g@2x.png" /></figure><p>发现我的状态矩阵没有考虑斜对角线的变量，应该都是 func[i,i] = nums[i]。所以加上了func[i, j]的初始化。又仔细检查了一下，感觉应该没有问题了。又提交，再跪了。这次不是算法的正确性问题，而是超时了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zwjDtQqTJT4cCjhxKTFanA@2x.png" /></figure><p>下面贴出来跑到倒数第二个testcase的代码：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64bb9ed65af501a42991050419ab13cc/href">https://medium.com/media/64bb9ed65af501a42991050419ab13cc/href</a></iframe><p>如果是时间超了，可以做的，就是时间复杂度的优化。上面的时间复杂度是O(n²)。对于这样的一个一维数组最好的自然是O(n)。其实上面的解题过程，忽略了一个情况，就是正负对于结果的影响。举个例子，-4*3是-12，如果再乘以-1结果就是12了。所以对于求乘积结果的最大值，需要记录下来计算过程中最大值和最小值，因为最小值（如果是一个负数）乘以一个负数，结果很可能比之前的最大值还要大。</p><p>因此，可以通过遍历一遍，同时使用两个数组来保存计算结果，而不是一个二维数组。max[i] 和 min[i] 分别表示从0开始到i时，当前子串的最大值和最小值。代码如下：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/699aba4830d677e644497d149c65b0bc/href">https://medium.com/media/699aba4830d677e644497d149c65b0bc/href</a></iframe><p>显然上面的代码还有可以优化的地方，就是使用的空间可以是O(1)。因为max[] 和 min[] 记录了每一步的最大值和最小值，我们完全可以只保存全局的最大值和最小值，这样只要两个int变量就行了。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6098440185e96e1723b36686ce6b919a/href">https://medium.com/media/6098440185e96e1723b36686ce6b919a/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e85e06907f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Leetcode] Decode String]]></title>
            <link>https://medium.com/@zxholy/leetcode-decode-string-e5ae9495bb84?source=rss-266902acf464------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5ae9495bb84</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[stack]]></category>
            <category><![CDATA[dfs]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[zxholy]]></dc:creator>
            <pubDate>Wed, 29 Nov 2017 11:49:17 GMT</pubDate>
            <atom:updated>2017-12-06T09:42:59.416Z</atom:updated>
            <content:encoded><![CDATA[<p>刚开始思考解法的时候，就是按照基本的规则描述进行思考的。我的第一想法是DFS去做。其实就是用递归（Recursion），如果默认在最外层加上 1[…] 就是一个递归的标准形式：每次都是把方括号内的字符串，进行递归decode就行。</p><p>然后就是一段非常惨烈的代码，除了毫无用处的封装方法外，基本上整个思路是乱的。贴一下帮自己记录一下：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1508180ca8a3567428282e6399ab4ea2/href">https://medium.com/media/1508180ca8a3567428282e6399ab4ea2/href</a></iframe><p>有几个问题：</p><ul><li>没必要使用List这种数据结构。对于最终结果因为是从左到右的解析，所以完全可以用一个StringBuilder完成，或者直接用String的追加也行。</li><li>递归最重要的是先明确退出的边界条件。最开始思路是对的，就是加一个1[…]的结构。但是忽略的边界条件。边界条件应该是`]`。这样就可以只解析中括号内的string了。</li><li>对于括号中间嵌套括号的情况，我的想法是一次完整的找到最外层中括号的字串。其实只要从左到右以此解析就行，遇到内嵌的数字和`[`只要进行递归即可。所以，也不用记录括号出现的次数。</li><li>对于repeat counter的清空是没有问题的，但是对于inBracketContent的清空就是一开始没有想清楚了。</li></ul><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>看提示，大部分都是用stack的方式去做。自己也在反复研究了一下stack的算法，写了一个acceptable的代码。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8877d31b0356b9c5250bc8d48288534/href">https://medium.com/media/c8877d31b0356b9c5250bc8d48288534/href</a></iframe><p>stack去做，关键点1：搞清楚countStack，contentStack还有contentInBracket的作用就行。</p><ul><li>countStack：用来保存数字</li><li>contentInBracket：用来记录在中括号中间的内容。依旧可以想象整个字符串是1[…]的结构，这样最终的contentInBarcket就是你要的结果。</li><li>contentStack：用来记录发生content repeat之前的内容。里面存的就是contentInBarcket。</li></ul><p>关键点2：因为不是递归，而是从左到右依次判断，所以要明确数字和左右括号不同的执行逻辑。自然分成了4种逻辑：</p><ul><li>数字：求出数字后，push到计数器stack</li><li>左括号：遇到左括号，就是要开始一段inner content decode了。所以就把之前已经算出来的contentInBarcket压栈。</li><li>右括号：触发content repeat的逻辑，从contentStack里pop出之前的内容，然后拼接上当前的contentInBarcket。</li><li>普通字母：正常的记录contentInBarcket。</li></ul><p>其实stack的方法，如果之前没有进行过类似的训练，是不容易一下想到的。但是一旦习惯这种解决问题的思路的话，以后可以自然联想到stack去解决问题。</p><p>说回之前想的，如果用递归的方式可以解决吗？当然是可以的。但是下面先贴一个错误的例子：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7f6ce51846be102961f86e2263931ed/href">https://medium.com/media/d7f6ce51846be102961f86e2263931ed/href</a></iframe><p>有两个关键的错误点：</p><ul><li>忽略了Java的值的传递方式。就是index。</li></ul><p>网上有一些用C++的方式实现的递归版本，与java版本有一个不同是，C++可以传递一个指针，但是在Java版本中需要进行调整，维护一个全局指针，也可以传递一个数组，然后在数组里，记录指针的变化。</p><ul><li>把递归decode方法写在repeat content里了。正确的应该是先decode出来子串的结果，然后直接拼接结果就行。Orz</li></ul><p>.</p><p>.</p><p>其实关于递归解决方法，在一开始的那个错误代码分析里，提到了一些要注意的点。下面贴一个正确的版本：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f04bcdee9da36a56450bcb334bc421e/href">https://medium.com/media/9f04bcdee9da36a56450bcb334bc421e/href</a></iframe><p>其实这道题目，看到也有用正则去做的，这种解法就比较神奇了，看来正则牛逼了，也是可以的。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5ae9495bb84" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>