<?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 邱德旺 on Medium]]></title>
        <description><![CDATA[Stories by 邱德旺 on Medium]]></description>
        <link>https://medium.com/@havbgbg68?source=rss-90d61e147cf4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*J7hTT1Z-17z7YpgWkIxZAQ.jpeg</url>
            <title>Stories by 邱德旺 on Medium</title>
            <link>https://medium.com/@havbgbg68?source=rss-90d61e147cf4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 11:39:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@havbgbg68/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[PostgreSQL 學習筆記 (一)]]></title>
            <link>https://medium.com/@havbgbg68/postgresql-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E4%B8%80-aed1d9b1abbb?source=rss-90d61e147cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/aed1d9b1abbb</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[postgresql]]></category>
            <dc:creator><![CDATA[邱德旺]]></dc:creator>
            <pubDate>Sun, 17 Mar 2019 14:21:54 GMT</pubDate>
            <atom:updated>2019-03-17T14:21:54.918Z</atom:updated>
            <content:encoded><![CDATA[<p>PostgresSQL 是一個開源的關係型資料庫管理系統，念作 ”post-gress-Q-L”，或是簡稱”postgres”。</p><p>此系列文章為 Udemy 上 <a href="https://www.udemy.com/the-complete-sql-bootcamp/">The Complete SQL Bootcamp</a> 這門課程的學習筆記。</p><ol><li><a href="#65c7">下載安裝 pgAdmin</a></li><li><a href="#7cbe">下載安裝 PostgreSQL</a></li><li><a href="#32a5">啟動 pgAdmin</a></li><li><a href="#b81c">新建資料庫</a></li><li><a href="#2658">restore 資料庫</a></li></ol><h3>下載安裝 pgAdmin</h3><p>pgAdmin 是一個管理 PostgreSQL 的開源圖形化工具，支援多種平台。現在最新的版本是 pgAdmin4 v4.3 (2019/3)，pgAdmin 3已經不維護了，這裡建議下載最新的版本。</p><p>到 pgAdmin 的<a href="https://www.pgadmin.org/">官網</a>，找到 Download 頁面下載 (以 Windows 為例)：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*-ChhJmJNhVwCFYKLXd1aTA.jpeg" /><figcaption>選擇作業系統，在 windows 上</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*aMrkUbLZLjfRW7ZS6un62g.jpeg" /><figcaption>找到最新的版本後，點擊 .exe 下載</figcaption></figure><h3>下載安裝 PostgreSQL</h3><p>目前最新的版本是 PostgreSQL 11 (2019/3)，到<a href="http://www.postgresql.org/">官網</a>後，找到下載頁面下載。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*Rn-NdIwmgEN__LYTGqp5NQ.jpeg" /><figcaption>選擇作業系統</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*znHK9QTrJ225WsrfHyiJJA.jpeg" /><figcaption>下載使用作業系統裡的最新版</figcaption></figure><p>安裝時，pgAdmin4 我們已經安裝過，這裡就不勾選。另外要注意的是密碼一定要記下來，之後會登入時會需要，如果忘記就只能重新安裝了</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*gEUtrzKj_j2sahCSXcbssQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*RfgBq-3jOHxaqfypQXoOwg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*OeQ7cbsZOxq-Bs7HgjDu8A.jpeg" /><figcaption>最後一個不勾選，目前用不到</figcaption></figure><h3>啟動 pgAdmin</h3><p>打開 pgAdmin 後，輸入剛剛的密碼登入。這裡可以看到PostgreSQL 就是我們剛剛安裝的版本11，如果有安裝其他版本也會在這裡看到。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BLIvlPywPnVF2r5oZAgvlA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*csRg8dqrx5Iai9ww8vh7Zg.jpeg" /><figcaption>登入後就是Admin的主介面</figcaption></figure><h3>新建資料庫</h3><p>新建一個 名為Test 的資料庫</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*7s81K-DHqpmY1xRJrepDRg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*8zHpApl6WEK_wkNa28dX9g.jpeg" /></figure><h3>restore 資料庫</h3><p>在我們建好的 test 資料庫上按右鍵，點擊 Restore，就可以將我們的tar檔導入。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/749/1*R7Yt_eMrOdhJYrioVCnsIw.jpeg" /></figure><p>General選擇檔案位置，可以點擊Filename輸入格右側的三點按鈕，打開類似檔案總管的視窗選擇檔案。<br>Restore option 選擇需要 Pre-data、Data、Post-data，各個選項的詳細定義可以在<a href="https://www.pgadmin.org/docs/pgadmin4/dev/restore_dialog.html">官方說明文件</a>中查詢。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*N7T9We9fJIIM0UFNOIgRiQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*TcseDUswLvu8BMPKjbVtSg.jpeg" /><figcaption>選擇檔案位置與選項後，按下 restore 即可導入</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*W6rSna7qwW7gnBs6nNfjGw.jpeg" /><figcaption>按下Filename右側的三點按鈕，可以選擇要導入的資料庫</figcaption></figure><p>下集待續…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aed1d9b1abbb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Home Assistant (二)— 設定]]></title>
            <link>https://medium.com/@havbgbg68/home-assistant-%E4%BA%8C-%E8%A8%AD%E5%AE%9A-e2c94c012de3?source=rss-90d61e147cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2c94c012de3</guid>
            <dc:creator><![CDATA[邱德旺]]></dc:creator>
            <pubDate>Sat, 16 Feb 2019 04:22:14 GMT</pubDate>
            <atom:updated>2019-02-16T04:22:14.341Z</atom:updated>
            <content:encoded><![CDATA[<p>這篇 Home Assistant 的第二篇，安裝完 Hass.io 後的基礎設定。</p><p>Home Assistant 主要透過更改 configuration.yaml 來設定 Home 裡的其他設置。有兩種方式可以更改 configuration.yaml ：</p><ol><li><a href="#5c62"><strong>Configurator </strong></a><strong><br></strong>透過安裝 configurator 這個 Add-on 後可以直接在 WEB 上更改onfiguration.yaml。</li><li><a href="#c247"><strong>Samba/Windows Networking </strong></a><strong><br></strong>透過安裝 Samba share 這個 Add-on 後可以在 windows 的「網路」上看到HASS 的共用資料夾，使用編輯器開啟yaml的方式更改。</li></ol><p>下面介紹兩種使用方式：</p><h4>使用Configurator</h4><p>在 Hass.io 主畫面的左側欄位找到 Hass.io ，點擊後找到 ADD-ON STORE</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*Bou9ga_WM8bZOZvgM5RnVA.jpeg" /></figure><p>進入 ADD-ON STORE 後，在 Offical add-ons 裡找到 Configurator，點選後按下 install 安裝。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*SYwnE34rb_J7go-uIuO_wQ.jpeg" /></figure><p>安裝後，在下方的 Config 裡的欄位找到 password 後打上要設定的密碼，之後進入 Configurator 會使用這個密碼登入，記得密碼要包含在&quot; &quot;裡，最後按下SAVE存檔。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*SVHAjHZmsg7ArjwhdS2Bug.jpeg" /></figure><p>接著回到上面按下 OPEN WEB UI ，瀏覽器會開啟一個新的分頁。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4JuIWloucxPkw5CH6Wo7vw.jpeg" /></figure><p>使用剛剛 Config 裡的帳號 admin 與設定好的密碼就可以登入囉。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9z26Sg-JqGEig6j_s8xqTg.jpeg" /></figure><p>找到左上角這個資料夾</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*WujkTI-mHcNmY55zZI1GRg.jpeg" /></figure><p>在 config/ 下，找到 confgiuration.yaml 點擊打開它。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*fEspzwmKOXM4TTgUooYBUQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gBQekm7LpnrbZGlQO89H_g.jpeg" /></figure><p>在最後加上，其中 url: <a href="http://hassio.local:8123/">http://hassio.local:8123</a> 可以改成 <a href="http://192.168.1.5:8123/">http://(your HASS.io IP):8123</a> ，改完後按下右上方的儲存。</p><pre>panel_iframe:<br>  configurator:<br>    title: Configurator<br>    icon: mdi:wrench<br>    url: <a href="http://hassio.local:3218/">http://hassio.local:3218</a></pre><p>詳細的語法使用方式可以在 <a href="https://www.home-assistant.io/components/panel_iframe/">Home Assistatn 的官網搜尋 panel_iframe </a>裡找到，這裡簡單說明一下，panel_iframe 是在 Hass.io 的主畫面左側欄位新增一個名為 Configurator 的欄位，裡面內容是 <a href="http://hassio.local:8123/">http://hassio.local:8123</a> 這個網址的內容。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4j4RxuZRY_2XN2Z74LB4ZQ.jpeg" /></figure><p>之後回到 Hass.io 的主畫面，在左側欄位找到設定裡面的<strong>檢查設定內容</strong>，點擊後出現 設定檔內容檢查正確，這樣我們就更改設定成功。接著在同一頁面下的服務器管理按下<strong>重啟</strong>，重新啟動 Hass.io。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cVkvjw8BETaXhazbWkO1lQ.jpeg" /></figure><p>重新啟動後，就可以在左側欄位裡看到剛剛在confgiuration.yaml裡新增的Configurator 欄位囉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*11-o-Ivt556M82PimMXcOw.jpeg" /></figure><h4><strong>使用Samba/Windows Networking</strong></h4><p>在 Hass.io 主畫面的左側欄位找到 Hass.io ，點擊後找到 ADD-ON STORE，進入 ADD-ON STORE 後，在 Offical add-ons 裡找到 Configurator，點選後按下 install 安裝。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*E74ciQ4v2s02ueQMktUL4Q.jpeg" /></figure><p>安裝後，在下方的 Config 裡的欄位找到 password 後打上要設定的密碼，之後會使用這個密碼登入，記得密碼要包含在 &quot; &quot; 裡，最後按下SAVE存檔。並按下 START 開始執行。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*ryYL7hx7sI0aKYRzQZePCg.jpeg" /></figure><p>到 Windows 下的 我的電腦 ，打開網路，就可以看到 HASSIO ，打開後輸入帳號密碼登入。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*QgBR7KKe-FFK2iFcNUYB1w.jpeg" /></figure><p>進入後找到 config 資料夾並打開。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*DdjkgIejMDZb0fkgFmyutw.jpeg" /></figure><p>這裡可以看到 configuration.yaml，使用本地端的文件編輯器打開就可以編輯囉。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*BL9HYf8RYF1i6IXkjRLuxg.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2c94c012de3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Home Assistant  —  Hass.io 安裝]]></title>
            <link>https://medium.com/@havbgbg68/home-assistant-hass-io-%E5%AE%89%E8%A3%9D-7cd00b57744e?source=rss-90d61e147cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/7cd00b57744e</guid>
            <category><![CDATA[home-assistant]]></category>
            <dc:creator><![CDATA[邱德旺]]></dc:creator>
            <pubDate>Thu, 14 Feb 2019 15:25:44 GMT</pubDate>
            <atom:updated>2019-02-16T04:23:11.132Z</atom:updated>
            <content:encoded><![CDATA[<h3>Home Assistant (一)— Hass.io 安裝</h3><p>首先先介紹下 Home Assistant，是個開源的智慧家庭系統，適合有 Coding 背景的人，不過不會 Coding 也沒關系，Home Assistant 有懶人的安裝方法(Hass.io)</p><p><a href="https://www.home-assistant.io/">Home Assistant</a></p><p>到官網後，按下 Getting Started 直接依照官網的步驟，加油~</p><p>開玩笑的，以下是我這個稍微會 coding 的人在安裝 Hass.io 的過程:</p><h4>進入 Install Home Assistant 頁面後，立馬看到我們需要</h4><blockquote><strong><em>Raspberry Pi 3 Model B+</em></strong><em><br>(power supply至少 2.5 A)(雖然是寫 2.5 A 但實際上 2 A 也勉強可行)</em></blockquote><blockquote><strong><em>Micro SD Card Class10 32 GB </em></strong><em><br>(貌似不用到 32 GB，16 GB 也可以)</em></blockquote><blockquote><strong><em>網路線<br></em></strong><em>(初次安裝推薦準備一條，可以簡單省事)</em></blockquote><blockquote><strong><em>SD 卡讀卡機 與 USB 隨身碟</em></strong><em><br>(若是使用 Wifi 連接的話就需要USB 隨身碟)</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oDKSfdjWMLA_JXdF7w2n9g.jpeg" /></figure><h4>下載 Hass.io 的映像檔，並使用 Etcher 安裝</h4><p>到<a href="https://www.home-assistant.io/hassio/installation/">下載頁面</a>依照所使用的 Raspberry Pi 下載對應的映像檔，並使用官方建議的 <a href="https://www.balena.io/etcher">balenaEtcher</a> 將映像檔燒錄進 SD 卡</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*b8JmMfnwh1H8YGgn4Zq3Dg.jpeg" /><figcaption>依照使用的Raspberry Pi下載對應的安裝檔</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*22PQFXXtruzO8XYkD8ldQg.jpeg" /><figcaption>使用 Etcher 燒錄映像檔</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*JemevF415kmQGKR-D_m-gQ.jpeg" /></figure><p>燒錄進 SD卡後，裝上 Raspberry Pi ，上電後… 等待 20 minutes later</p><p>這時候可以先打開瀏覽器，找到分享器設定頁面(通常是192.168.1.1)，輸入 <a href="http://hassio.local:8123/">http://hassio.local:8123</a> 或是 <a href="http://192.168.1.5:8123/">http://(your HASS.io IP):8123</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*wCu1Reu97D43FPPGnms9QA.jpeg" /><figcaption>找到 hassio 的 IP 位址</figcaption></figure><p>等待 Home Assistant 自動更新，瀏覽器重新整理後，創建帳號並登入就出現主畫面囉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LNEp47DySMTrsF6mzONyyw.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7cd00b57744e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LeetCode (2) Add to Numbers (python)]]></title>
            <link>https://medium.com/@havbgbg68/leetcode-2-add-to-numbers-python-63e4d5ba1534?source=rss-90d61e147cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/63e4d5ba1534</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[邱德旺]]></dc:creator>
            <pubDate>Sun, 11 Nov 2018 13:26:36 GMT</pubDate>
            <atom:updated>2018-11-11T13:26:36.484Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Add to Numbers(difficulty: Medium)<br></strong>這第二題雖然難度是medium就卡關了兩天….</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/1*3_M51pDgPJbu82XLlfjslw.jpeg" /></figure><p>You are given two <strong>non-empty</strong> linked lists representing two non-negative integers. The digits are stored in <strong>reverse order</strong> and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.</p><p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.</p><p>Example:</p><blockquote>Input: (2 -&gt; 4 -&gt; 3) + (5 -&gt; 6 -&gt; 4)<br>Output: 7 -&gt; 0 -&gt; 8<br>Explanation: 342 + 465 = 807.</blockquote><p>一開始在ListNode卡了很久(聽都沒聽過!!?),<strong>可以將ListNode想成List裡的每個小元素</strong></p><blockquote>其實說穿了就是一個Class, 裡面有兩個變數,<br>一個val, 另一個next<br>val就是這個ListNode存的值, next就是下一個ListNode</blockquote><p>舉個小例子,</p><pre>List_A = [1, 2, 3]<br>就等於List_A = ListNode_a 與 ListNode_b 與 ListNodec <br>其中:<br>ListNode_a的Val = 1, ListNode.next = ListNode_b,<br>ListNode_b的Val = 2, ListNode.next = ListNode_c,<br>ListNode_c的Val = 3, ListNode.next = none</pre><p>如果還是不懂可以參考下面這個網址, 寫的淺顯易懂</p><p><a href="http://alrightchiu.github.io/SecondRound/linked-list-introjian-jie.html">Linked List: Intro(簡介)</a></p><p>以下是我參考大家的解答後得到的結果:</p><pre>result = ListNode(0)<br>node = result<br>temp = 0</pre><pre>while l1 is not None or l2 is not None or temp&gt;0:<br>    if l1 is not None:<br>        temp += l1.val<br>        l1 = l1.next<br>    if l2 is not None:<br>        temp += l2.val<br>        l2 = l2.next</pre><pre>    node.next = ListNode(temp%10)   # 取個位數<br>    node = node.next</pre><pre>    temp = temp // 10    # 檢查是否要進位</pre><pre>return result.next</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63e4d5ba1534" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LeetCode (1) Two Sum (python)]]></title>
            <link>https://medium.com/@havbgbg68/leetcode-1-two-sum-python-8d77c223abd3?source=rss-90d61e147cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d77c223abd3</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[邱德旺]]></dc:creator>
            <pubDate>Sun, 14 Oct 2018 16:33:54 GMT</pubDate>
            <atom:updated>2018-10-14T16:33:54.997Z</atom:updated>
            <content:encoded><![CDATA[<p>紀錄一下刷題, 第一題<strong>Two Sum (difficulty: Easy)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*6sH449xxLqckCFHEzihQxw.png" /></figure><p>Given an array of integers, return indices of the two numbers such that they add up to a specific target.<br>You may assume that each input would have exactly one solution, and you may not use the same element twice.</p><p>Example:</p><blockquote>Given nums = [2, 7, 11, 15], target = 9,</blockquote><blockquote>Because nums[0] + nums[1] = 2 + 7 = 9,<br>return [0, 1].</blockquote><p>給一個list (list內是數個int)<br>再給一個target (一樣是int)<br>找出list內的哪兩個數加起來是target, 回傳兩個數的index<br>簡單來想,使用兩個for loop每個數組都加一次就解決了</p><pre>for i in range(len(nums)):<br>    left = nums[i+1:]<br>    for j in range(len(left)):<br>        if (nums[i] + left[j]) == target:<br>            return i, j+i+1</pre><p>在submit後,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*31h43MjA-ZC-JsrXEBtVLg.png" /></figure><p>兩個for迴圈太花時間了…</p><pre>#看到的另一種寫法<br>k = 0<br>for i in nums:<br>    k += 1<br>    if target - i in nums[k:]:<br>        return(k - 1, nums[k:].index(target - i) + k)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*JfrmNZnuLXgPdgij0ObAIQ.png" /></figure><p>看了下解答,官方給出了另外兩個答案</p><h4>Two-pass Hash Table</h4><p>To improve our run time complexity, we need a more efficient way to check if the complement exists in the array. If the complement exists, we need to look up its index. What is the best way to maintain a mapping of each element in the array to its index? A hash table.</p><p>We reduce the look up time from <em>O</em>(<em>n</em>) to <em>O</em>(1) by trading space for speed. A hash table is built exactly for this purpose, it supports fast look up in <em>near</em> constant time. I say “near” because if a collision occurred, a look up could degenerate to <em>O</em>(<em>n</em>) time. But look up in hash table should be amortized <em>O</em>(1) time as long as the hash function was chosen carefully.</p><p>A simple implementation uses two iterations. In the first iteration, we add each element’s value and its index to the table. Then, in the second iteration we check if each element’s complement (<em>target</em>−<em>nums</em>[<em>i</em>]) exists in the table. Beware that the complement must not be <em>nums</em>[<em>i</em>]itself!</p><pre>hash_table={}<br>for i in range(len(nums)):    # 先做一個hash table<br>    hash_table[nums[i]]=i</pre><pre>for i in range(len(nums)):<br>    if target-nums[i] in hash_table:<br>        if hash_table[target-nums[i]] != i:<br>            return [i, hash_table[target-nums[i]]]<br>return []</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*k6EvU07vRme3iqku68xiAQ.png" /></figure><h4>One-pass Hash Table</h4><p>It turns out we can do it in one-pass. While we iterate and inserting elements into the table, we also look back to check if current element’s complement already exists in the table. If it exists, we have found a solution and return immediately.</p><pre>hash_table = {}<br>for i, num in enumerate(nums):<br>    if target - num in hash_table:<br>        return([hash_table[target - num], i])<br>        break    # 看到有人在這加了break, 理論上不會執行到, 但時間卻會比較短<br>    hash_table[num] = i<br>return([])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*3I7_fJBISk4YlKqM8bp8aQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*6YWq5aKMHfOI49ztJ8rAAA.png" /></figure><h3>小結論</h3><p>TwoSum這個題目有三種解法</p><ol><li>兩個for 迴圈從頭加到尾</li><li>創一個Hash Table, 再使用for 迴圈去尋找答案</li><li>邊創Hash Table, 邊看是否達到解答, 達到就返回</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d77c223abd3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python語法 with … as]]></title>
            <link>https://medium.com/@havbgbg68/python%E8%AA%9E%E6%B3%95-with-as-d0ac878ce366?source=rss-90d61e147cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0ac878ce366</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[邱德旺]]></dc:creator>
            <pubDate>Sat, 01 Apr 2017 15:36:46 GMT</pubDate>
            <atom:updated>2017-04-01T15:36:46.995Z</atom:updated>
            <content:encoded><![CDATA[<p>python中講到with 最常見的就是開檔</p><pre>def opening(filename):<br>    f = open(filename)<br>    try:<br>        yield f<br>    finally:<br>        f.close()</pre><p>可改成</p><pre>with f = opening(filename):<br>    ...read data from f...</pre><ul><li><a href="http://linbo.github.io/2013/01/08/python-with">[翻译] 理解Python的With语句 ←</a></li><li><a href="https://www.python.org/dev/peps/pep-0343/">PEP 343 -- The &quot;with&quot; Statement</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0ac878ce366" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>