<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LeetCode &#8211; 比比浪一下</title>
	<atom:link href="https://chobebe.com/tag/leetcode/feed/" rel="self" type="application/rss+xml" />
	<link>https://chobebe.com</link>
	<description>旅行、美食、寫作日常</description>
	<lastBuildDate>Thu, 12 Feb 2026 10:21:06 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://chobebe.com/wp-content/uploads/2021/03/chobebe-200x200.png</url>
	<title>LeetCode &#8211; 比比浪一下</title>
	<link>https://chobebe.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">175145175</site>	<item>
		<title>LeetCode 565. Array Nesting (PHP)</title>
		<link>https://chobebe.com/leetcode-565-array-nesting-php/</link>
		
		<dc:creator><![CDATA[Bebe]]></dc:creator>
		<pubDate>Sun, 05 Sep 2021 10:07:09 +0000</pubDate>
				<category><![CDATA[技能筆記]]></category>
		<category><![CDATA[LeetCode]]></category>
		<category><![CDATA[Medium]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">https://chobebe.com/?p=3444</guid>

					<description><![CDATA[<p><a rel="nofollow" href="https://chobebe.com">比比浪一下</a><br />
<a rel="nofollow" href="https://chobebe.com/leetcode-565-array-nesting-php/">LeetCode 565. Array Nesting (PHP)</a></p>
<p>題目 You are given an integer array&#160;nums&#160;of len [&#8230;]</p>
<p><a rel="nofollow" href="https://chobebe.com/leetcode-565-array-nesting-php/">LeetCode 565. Array Nesting (PHP)</a><br />
<a rel="nofollow" href="https://chobebe.com/author/chochoc153/">Bebe</a></p>
]]></description>
										<content:encoded><![CDATA[<p><a rel="nofollow" href="https://chobebe.com">比比浪一下</a><br />
<a rel="nofollow" href="https://chobebe.com/leetcode-565-array-nesting-php/">LeetCode 565. Array Nesting (PHP)</a></p>

<h2 class="wp-block-heading">題目</h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">You are given an integer array&nbsp;<code>nums</code>&nbsp;of length&nbsp;<code>n</code>&nbsp;where&nbsp;<code>nums</code>&nbsp;is a permutation of the numbers in the range&nbsp;<code>[0, n - 1]</code>.</p>



<p class="wp-block-paragraph">You should build a set&nbsp;<code>s[k] = {nums[k], nums[nums[k]], nums[nums[nums[k]]], ... }</code>&nbsp;subjected to the following rule:</p>



<ul class="wp-block-list"><li>The first element in&nbsp;<code>s[k]</code>&nbsp;starts with the selection of the element&nbsp;<code>nums[k]</code>&nbsp;of&nbsp;<code>index = k</code>.</li><li>The next element in&nbsp;<code>s[k]</code>&nbsp;should be&nbsp;<code>nums[nums[k]]</code>, and then&nbsp;<code>nums[nums[nums[k]]]</code>, and so on.</li><li>We stop adding right before a duplicate element occurs in&nbsp;<code>s[k]</code>.</li></ul>



<p class="wp-block-paragraph">Return&nbsp;<em>the longest length of a set</em>&nbsp;<code>s[k]</code>.</p>



<p class="wp-block-paragraph"><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted"><strong>Input:</strong> nums = [5,4,0,3,1,6,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> 
nums[0] = 5, nums[1] = 4, nums[2] = 0, nums[3] = 3, nums[4] = 1, nums[5] = 6, nums[6] = 2.
One of the longest sets s[k]:
s[0] = {nums[0], nums[5], nums[6], nums[2]} = {5, 6, 2, 0}
</pre>



<p class="wp-block-paragraph"><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted"><strong>Input:</strong> nums = [0,1,2]
<strong>Output:</strong> 1
</pre>



<p class="wp-block-paragraph"><strong>Constraints:</strong></p>



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= nums[i] &lt; nums.length</code></li><li>All the values of&nbsp;<code>nums</code>&nbsp;are&nbsp;<strong>unique</strong>.</li></ul>



<h2 class="wp-block-heading">解題</h2>



<pre class="wp-block-code"><code>class Solution {

    /**
     * @param Integer&#91;] $nums
     * @return Integer
     */
    function arrayNesting($nums) {
        
        $result = 0;
        
        foreach($nums as $key => $val) {
            if (array_key_exists($key, $nums)) {
                $k = $nums&#91;$key];
                unset($nums&#91;$key]);
                $count = 1;
                
                while(array_key_exists($k, $nums)) {
                    $k = $nums&#91;$k];
                    $count++;
                }
                
                $result = max($result, $count);
            }
        }
        
        return $result;
    }
}</code></pre>



<p class="wp-block-paragraph">Runtime:&nbsp;127 ms, faster than&nbsp;9.09%&nbsp;of&nbsp;PHP&nbsp;online submissions for&nbsp;Array Nesting.</p>



<p class="wp-block-paragraph">Memory Usage: 18.4 MB, less than 77.27% of PHP online submissions for Array Nesting.</p>



<h2 class="wp-block-heading">進階</h2>



<pre class="wp-block-code"><code>/**
 * @param Integer&#91;] $nums
 * @return Integer
 */
function arrayNesting($nums) {

    $result = 0;
    for ($i = 0; $i &lt; count($nums); ++$i) {
        if ($nums&#91;$i] != -1) {
            $count = 0;
            while($nums&#91;$i] != -1) {
                $j = $nums&#91;$i];
                $nums&#91;$i] = -1;
                $i = $j;
                $count++;
            }
            $result = max($result, $count);
        }
    }

    return $result;
}</code></pre>



<p class="wp-block-paragraph">Runtime:&nbsp;40 ms, faster than&nbsp;79.17%&nbsp;of&nbsp;PHP&nbsp;online submissions for&nbsp;Array Nesting.</p>



<p class="wp-block-paragraph">Memory Usage:&nbsp;18.5 MB, less than&nbsp;25.00%&nbsp;of&nbsp;PHP&nbsp;online submissions for&nbsp;Array Nesting.</p>
<p><a rel="nofollow" href="https://chobebe.com/leetcode-565-array-nesting-php/">LeetCode 565. Array Nesting (PHP)</a><br />
<a rel="nofollow" href="https://chobebe.com/author/chochoc153/">Bebe</a></p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3444</post-id>	</item>
		<item>
		<title>LeetCode 283. Move Zeroes (PHP)</title>
		<link>https://chobebe.com/leetcode-283-move-zeroes-php/</link>
		
		<dc:creator><![CDATA[Bebe]]></dc:creator>
		<pubDate>Tue, 31 Aug 2021 15:37:27 +0000</pubDate>
				<category><![CDATA[技能筆記]]></category>
		<category><![CDATA[Easy]]></category>
		<category><![CDATA[LeetCode]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">https://chobebe.com/?p=3433</guid>

					<description><![CDATA[<p><a rel="nofollow" href="https://chobebe.com">比比浪一下</a><br />
<a rel="nofollow" href="https://chobebe.com/leetcode-283-move-zeroes-php/">LeetCode 283. Move Zeroes (PHP)</a></p>
<p>今天刷了兩題，有夠上進～值得吃薯條慶祝！ 題目 Given an integer array&#160;num [&#8230;]</p>
<p><a rel="nofollow" href="https://chobebe.com/leetcode-283-move-zeroes-php/">LeetCode 283. Move Zeroes (PHP)</a><br />
<a rel="nofollow" href="https://chobebe.com/author/chochoc153/">Bebe</a></p>
]]></description>
										<content:encoded><![CDATA[<p><a rel="nofollow" href="https://chobebe.com">比比浪一下</a><br />
<a rel="nofollow" href="https://chobebe.com/leetcode-283-move-zeroes-php/">LeetCode 283. Move Zeroes (PHP)</a></p>

<p class="wp-block-paragraph">今天刷了兩題，有夠上進～值得吃薯條慶祝！</p>



<h2 class="wp-block-heading">題目</h2>



<p class="wp-block-paragraph">Given an integer array&nbsp;<code>nums</code>, move all&nbsp;<code>0</code>&#8216;s to the end of it while maintaining the relative order of the non-zero elements.</p>



<p class="wp-block-paragraph"><strong>Note</strong>&nbsp;that you must do this in-place without making a copy of the array.</p>



<p class="wp-block-paragraph"><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted"><strong>Input:</strong> nums = [0,1,0,3,12]
<strong>Output:</strong> [1,3,12,0,0]
</pre>



<p class="wp-block-paragraph"><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted"><strong>Input:</strong> nums = [0]
<strong>Output:</strong> [0]
</pre>



<p class="wp-block-paragraph"><strong>Constraints:</strong></p>



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li><li><code>-2<sup>31</sup>&nbsp;&lt;= nums[i] &lt;= 2<sup>31</sup>&nbsp;- 1</code></li></ul>



<p class="wp-block-paragraph"><strong>Follow up:</strong>&nbsp;Could you minimize the total number of operations done?</p>



<h2 class="wp-block-heading">解題</h2>



<p class="wp-block-paragraph">這題很簡單，自信心＋＋<br>將給定陣列的0往最後面放即可～</p>



<p class="wp-block-paragraph">我是先將值為零的元素從陣列刪除，然後計數器加一，再跑一次迴圈把零補上。<br>不過後來想到，刪掉時同時補上也行，就不用再多一個<meta charset="utf-8">while迴圈了。</p>



<pre class="wp-block-code"><code>class Solution {

    /**
     * @param Integer&#91;] $nums
     * @return NULL
     */
    function moveZeroes(&amp;$nums) {
        $i = 0;
        foreach($nums as $key =&gt; $val) {
            if($nums&#91;$key] == 0) {
                unset($nums&#91;$key]);
                $i++;
            }
        }
                    
        while($i &gt; 0) {
            $nums&#91;] = 0;
            $i--;
        }
    }
}</code></pre>



<p class="wp-block-paragraph">Runtime:&nbsp;12 ms, faster than&nbsp;81.89%&nbsp;of&nbsp;PHP&nbsp;online submissions for&nbsp;Move Zeroes.</p>



<p class="wp-block-paragraph">Memory Usage:&nbsp;17 MB, less than&nbsp;25.20%&nbsp;of&nbsp;PHP&nbsp;online submissions for&nbsp;Move Zeroes.</p>
<p><a rel="nofollow" href="https://chobebe.com/leetcode-283-move-zeroes-php/">LeetCode 283. Move Zeroes (PHP)</a><br />
<a rel="nofollow" href="https://chobebe.com/author/chochoc153/">Bebe</a></p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3433</post-id>	</item>
		<item>
		<title>LeetCode 1. Two Sum (PHP)</title>
		<link>https://chobebe.com/leetcode-1-two-sum-php/</link>
		
		<dc:creator><![CDATA[Bebe]]></dc:creator>
		<pubDate>Tue, 31 Aug 2021 09:52:44 +0000</pubDate>
				<category><![CDATA[技能筆記]]></category>
		<category><![CDATA[Easy]]></category>
		<category><![CDATA[LeetCode]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">https://chobebe.com/?p=3419</guid>

					<description><![CDATA[<p><a rel="nofollow" href="https://chobebe.com">比比浪一下</a><br />
<a rel="nofollow" href="https://chobebe.com/leetcode-1-two-sum-php/">LeetCode 1. Two Sum (PHP)</a></p>
<p>不知道吃錯什麼藥，突然上進了起來，竟然來刷題！目前程度很普通，先挑Easy的來增加自信(/ω＼) 題目 Giv [&#8230;]</p>
<p><a rel="nofollow" href="https://chobebe.com/leetcode-1-two-sum-php/">LeetCode 1. Two Sum (PHP)</a><br />
<a rel="nofollow" href="https://chobebe.com/author/chochoc153/">Bebe</a></p>
]]></description>
										<content:encoded><![CDATA[<p><a rel="nofollow" href="https://chobebe.com">比比浪一下</a><br />
<a rel="nofollow" href="https://chobebe.com/leetcode-1-two-sum-php/">LeetCode 1. Two Sum (PHP)</a></p>

<p class="wp-block-paragraph">不知道吃錯什麼藥，突然上進了起來，竟然來刷題！目前程度很普通，先挑Easy的來增加自信(/ω＼)</p>



<h2 class="wp-block-heading">題目</h2>



<p class="wp-block-paragraph">Given an array of integers&nbsp;<code>nums</code>&nbsp;and an integer&nbsp;<code>target</code>, return&nbsp;<em>indices of the two numbers such that they add up to&nbsp;<code>target</code></em>.</p>



<p class="wp-block-paragraph">You may assume that each input would have&nbsp;<strong><em>exactly</em>&nbsp;one solution</strong>, and you may not use the&nbsp;<em>same</em>&nbsp;element twice.</p>



<p class="wp-block-paragraph">You can return the answer in any order.</p>



<p class="wp-block-paragraph"><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted"><strong>Input:</strong> nums = [2,7,11,15], target = 9
<strong>Output:</strong> [0,1]
<strong>Output:</strong> Because nums[0] + nums[1] == 9, we return [0, 1].
</pre>



<p class="wp-block-paragraph"><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted"><strong>Input:</strong> nums = [3,2,4], target = 6
<strong>Output:</strong> [1,2]
</pre>



<p class="wp-block-paragraph"><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted"><strong>Input:</strong> nums = [3,3], target = 6
<strong>Output:</strong> [0,1]
</pre>



<p class="wp-block-paragraph"><strong>Constraints:</strong></p>



<ul class="wp-block-list"><li><code>2 &lt;= nums.length &lt;= 10<sup>4</sup></code></li><li><code>-10<sup>9</sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>9</sup></code></li><li><code>-10<sup>9</sup>&nbsp;&lt;= target &lt;= 10<sup>9</sup></code></li><li><strong>Only one valid answer exists.</strong></li></ul>



<p class="wp-block-paragraph"><strong>Follow-up:&nbsp;</strong>Can you come up with an algorithm that is less than&nbsp;<code>O(n<sup>2</sup>)&nbsp;</code>time complexity?</p>



<h2 class="wp-block-heading">解題</h2>



<p class="wp-block-paragraph">這題我直接用暴力解法 (/ω＼) </p>



<p class="wp-block-paragraph">題目很好懂，給訂一個陣列，找出不重複的兩個值，相加等於給定值。</p>



<pre class="wp-block-code"><code>class Solution {

    /**
     * @param Integer&#91;] $nums
     * @param Integer $target
     * @return Integer&#91;]
     */
    function twoSum($nums, $target) {
        $cal = &#91;];
        foreach ($nums as $key1 =&gt; $val1) {
            foreach ($nums as $key2 =&gt; $val2) {
                if ($key1 == $key2) continue;
                else if($target - $val1 == $val2) {
                    $cal&#91;] = $key1;
                    $cal&#91;] = $key2;
                    break 2;
                }
            }
        }
        
        return $cal;
    }
}</code></pre>



<p class="wp-block-paragraph">Runtime:&nbsp;2335 ms, faster than&nbsp;14.53%&nbsp;of&nbsp;PHP&nbsp;online submissions for&nbsp;Two Sum.</p>



<p class="wp-block-paragraph">Memory Usage:&nbsp;16.2 MB, less than&nbsp;97.47%&nbsp;of&nbsp;PHP&nbsp;online submissions for&nbsp;Two Sum.</p>



<h2 class="wp-block-heading">進階</h2>



<p class="wp-block-paragraph">果然跑兩個迴圈執行時間會很久，參考網路上大神寫的，執行速度差超級多！又長知識了<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f913.png" alt="🤓" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<pre class="wp-block-code"><code>class Solution {

    /**
     * @param Integer&#91;] $nums
     * @param Integer $target
     * @return Integer&#91;]
     */
    function twoSum($nums, $target) {
        foreach ($nums as $key =&gt; $val) {
            unset($nums&#91;$key]);
            $nextKey = array_search(($target - $val), $nums);
            if ($nextKey) {
                return &#91;$key, $nextKey];
            }
        }
        return &#91;];
    }
}</code></pre>



<p class="wp-block-paragraph">Runtime:&nbsp;270 ms, faster than&nbsp;56.82%&nbsp;of&nbsp;PHP&nbsp;online submissions for&nbsp;Two Sum.</p>



<p class="wp-block-paragraph">Memory Usage: 16.2 MB, less than 97.47% of PHP online submissions for Two Sum.</p>



<p class="wp-block-paragraph">看到才<meta charset="utf-8">56.82%不死心，又繼續測試了一下！！有差有差</p>



<pre class="wp-block-code"><code>class Solution {

    /**
     * @param Integer&#91;] $nums
     * @param Integer $target
     * @return Integer&#91;]
     */
    function twoSum($nums, $target) {
        $map = &#91;];
        for ($i=0;$i&lt;=count($nums);$i++) {
            $diff = $target - $nums&#91;$i];

            if (array_key_exists($diff, $map)) {
                return &#91;$map&#91;$diff], $i];
            } else {
                $map&#91;$nums&#91;$i]] = $i;
            }
        }
        return &#91;];
    }
}</code></pre>



<p class="wp-block-paragraph">Runtime: 28 ms, faster than 70.92% of PHP online submissions for Two Sum.</p>



<p class="wp-block-paragraph">Memory Usage: 16.6 MB, less than 27.48% of PHP online submissions for Two Sum.</p>
<p><a rel="nofollow" href="https://chobebe.com/leetcode-1-two-sum-php/">LeetCode 1. Two Sum (PHP)</a><br />
<a rel="nofollow" href="https://chobebe.com/author/chochoc153/">Bebe</a></p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3419</post-id>	</item>
	</channel>
</rss>
