<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>leetcode on Tony Wu&#39;s Blog</title>
    <link>https://tonywu1995.github.io/tags/leetcode/</link>
    <description>Recent content in leetcode on Tony Wu&#39;s Blog</description>
    <generator>Hugo -- gohugo.io</generator>
    <lastBuildDate>Tue, 02 May 2023 19:38:35 +0800</lastBuildDate><atom:link href="https://tonywu1995.github.io/tags/leetcode/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Leetcode217 Contains Duplicate</title>
      <link>https://tonywu1995.github.io/leetcode/217/</link>
      <pubDate>Tue, 02 May 2023 19:38:35 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/217/</guid>
      <description>題目 Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
題意: 判斷陣列中是否有重複的值 解法 解法: 透過map資料結構，找出在陣列中是否有重複的值 func containsDuplicate(nums []int) bool { m := map[int]bool{} for _, v := range nums { if _, exist := m[v]; exist { return true } m[v] = true } return false } 加些測試，看看結果是不是如預期 import ( &amp;#34;github.com/stretchr/testify/assert&amp;#34; &amp;#34;testing&amp;#34; ) func Test_containsDuplicate(t *testing.</description>
    </item>
    
    <item>
      <title>Leetcode228 Summary Ranges</title>
      <link>https://tonywu1995.github.io/leetcode/228/</link>
      <pubDate>Fri, 21 Apr 2023 18:19:27 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/228/</guid>
      <description>題目 You are given a sorted unique integer array nums.
A range [a,b] is the set of all integers from a to b (inclusive).
Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.</description>
    </item>
    
    <item>
      <title>Leetcode290 Word Pattern</title>
      <link>https://tonywu1995.github.io/leetcode/290/</link>
      <pubDate>Fri, 21 Apr 2023 18:00:24 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/290/</guid>
      <description>題目 Given a pattern and a string s, find if s follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.
解法 找出pattern字串與s字串對應，並且轉換過去，且pattern對應的s不可重複，即可解答 import ( &amp;#34;strings&amp;#34; ) func wordPattern(pattern string, s string) bool { m := make(map[string]string) splitString := strings.Split(s, &amp;#34; &amp;#34;) patternString := strings.Split(pattern, &amp;#34;&amp;#34;) if len(splitString) != len(patternString) { return false } for i := 0; i &amp;lt; len(splitString); i++ { if _, ok := m[patternString[i]]; !</description>
    </item>
    
    <item>
      <title>Leetcode557 Reverse Words in a String III</title>
      <link>https://tonywu1995.github.io/leetcode/557/</link>
      <pubDate>Tue, 18 Apr 2023 21:29:07 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/557/</guid>
      <description>題目 Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
解法 import ( &amp;#34;strings&amp;#34; ) func reverseWords(s string) string { split := strings.Split(s, &amp;#34; &amp;#34;) res := &amp;#34;&amp;#34; for _, v := range split { res = res + reverse(v) + &amp;#34; &amp;#34; } return strings.Trim(res, &amp;#34; &amp;#34;) } func reverse(s string) string { var str string for _, v := range s { str = string(v) + str } return str } 加些測試，看看結果是不是如預期 func Test_reverseWords(t *testing.</description>
    </item>
    
    <item>
      <title>Leetcode704 Binary Search</title>
      <link>https://tonywu1995.github.io/leetcode/704/</link>
      <pubDate>Sat, 15 Apr 2023 21:58:41 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/704/</guid>
      <description>題目 Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
You must write an algorithm with O(log n) runtime complexity.
題意: 在nums陣列中找出等於target的index值 ，並且時間複雜度須為O(log n) 解法 題目要求: 時間複雜度為 O(log n) ，所以可以使用Binary Search 方法來找出index func search(nums []int, target int) int { start := 0 end := len(nums) - 1 for start &amp;lt;= end { mid := start + (end-start)/2 if nums[mid] == target { return mid } else if nums[mid] &amp;gt; target { end = mid - 1 } else { start = mid + 1 } } return -1 } 加些測試，看看結果是不是如預期 import ( &amp;#34;github.</description>
    </item>
    
    <item>
      <title>Leetcode35 Search Insert Position</title>
      <link>https://tonywu1995.github.io/leetcode/35/</link>
      <pubDate>Sat, 15 Apr 2023 21:49:00 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/35/</guid>
      <description>題目 Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
題意: 找出相近的nums array中的index ，並且時間複雜度須為O(log n) 解法 題目要求: 時間複雜度為 O(log n) ，所以可以使用Binary Search 方法來找出index func searchInsert(nums []int, target int) int { start := 0 end := len(nums) - 1 for start &amp;lt;= end { mid := start + (end-start)/2 if nums[mid] == target { return mid } else if nums[mid] &amp;gt; target { end = mid - 1 } else { start = mid + 1 } } return start } 加些測試，看看結果是不是如預期 import ( &amp;#34;github.</description>
    </item>
    
    <item>
      <title>Leetcode1844 Replace All Digits with Characters</title>
      <link>https://tonywu1995.github.io/leetcode/1844/</link>
      <pubDate>Thu, 24 Nov 2022 16:49:18 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/1844/</guid>
      <description>題目 You are given a 0-indexed string s that has lowercase English letters in its even indices and digits in its odd indices.
There is a function shift(c, x), where c is a character and x is a digit, that returns the xth character after c.
For example, shift(&amp;lsquo;a&amp;rsquo;, 5) = &amp;lsquo;f&amp;rsquo; and shift(&amp;lsquo;x&amp;rsquo;, 0) = &amp;lsquo;x&amp;rsquo;. For every odd index i, you want to replace the digit s[i] with shift(s[i-1], s[i]).</description>
    </item>
    
    <item>
      <title>Leetcode1662 Check If Two String Arrays are Equivalent</title>
      <link>https://tonywu1995.github.io/leetcode/1662/</link>
      <pubDate>Thu, 24 Nov 2022 16:36:37 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/1662/</guid>
      <description>題目 Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.
A string is represented by an array if the array elements concatenated in order forms the string.
Example Input: word1 = [&amp;#34;ab&amp;#34;, &amp;#34;c&amp;#34;], word2 = [&amp;#34;a&amp;#34;, &amp;#34;bc&amp;#34;] Output: true Explanation: word1 represents string &amp;#34;ab&amp;#34; + &amp;#34;c&amp;#34; -&amp;gt; &amp;#34;abc&amp;#34; word2 represents string &amp;#34;a&amp;#34; + &amp;#34;bc&amp;#34; -&amp;gt; &amp;#34;abc&amp;#34; The strings are the same, so return true.</description>
    </item>
    
    <item>
      <title>Leetcode1832 Check if the Sentence Is Pangram</title>
      <link>https://tonywu1995.github.io/leetcode/1832/</link>
      <pubDate>Thu, 24 Nov 2022 16:31:05 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/1832/</guid>
      <description>題目 A pangram is a sentence where every letter of the English alphabet appears at least once.
Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.
Example Input: sentence = &amp;#34;thequickbrownfoxjumpsoverthelazydog&amp;#34; Output: true Explanation: sentence contains at least one of every letter of the English alphabet. 題意: sentence中至少要出現一次a-z 解法 import ( &amp;#34;strings&amp;#34; ) func checkIfPangram(sentence string) bool { if len(sentence) &amp;lt; 26 { return false } for i := &amp;#39;a&amp;#39;; i &amp;lt;= &amp;#39;z&amp;#39;; i++ { if strings.</description>
    </item>
    
    <item>
      <title>Leetcode2325 Decode the Message</title>
      <link>https://tonywu1995.github.io/leetcode/2325/</link>
      <pubDate>Thu, 24 Nov 2022 16:16:21 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/2325/</guid>
      <description>題目 You are given the strings key and message, which represent a cipher key and a secret message, respectively. The steps to decode message are as follows:
Use the first appearance of all 26 lowercase English letters in key as the order of the substitution table. Align the substitution table with the regular English alphabet. Each letter in message is then substituted using the table. Spaces &amp;rsquo; &amp;rsquo; are transformed to themselves.</description>
    </item>
    
    <item>
      <title>Leetcode1528 Shuffle String</title>
      <link>https://tonywu1995.github.io/leetcode/1528/</link>
      <pubDate>Sun, 13 Nov 2022 23:08:53 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/1528/</guid>
      <description>題目 You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
Return the shuffled string.
Example 1 Input: s = &amp;#34;codeleet&amp;#34;, indices = [4,5,6,7,0,2,1,3] Output: &amp;#34;leetcode&amp;#34; Explanation: As shown, &amp;#34;codeleet&amp;#34; becomes &amp;#34;leetcode&amp;#34; after shuffling. 題意: indices的值為字串s替換要替換的位置 字串 : 位置 c -&amp;gt; 4 o -&amp;gt; 5 d -&amp;gt; 6 依此類推 解法 func restoreString(s string, indices []int) string { res := make([]byte, len(s)) for k, v := range indices { res[v] = s[k] } return string(res) } 加個測試，看看結果是不是如預期 import ( &amp;#34;github.</description>
    </item>
    
    <item>
      <title>Leetcode2011 Final Value of Variable After Performing Operations</title>
      <link>https://tonywu1995.github.io/leetcode/2011/</link>
      <pubDate>Sun, 06 Nov 2022 22:03:35 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/2011/</guid>
      <description>題目 There is a programming language with only four operations and one variable X:
++X and X++ increments the value of the variable X by 1. &amp;ndash;X and X&amp;ndash; decrements the value of the variable X by 1. Initially, the value of X is 0.
Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.
Example Input: operations = [&amp;#34;--X&amp;#34;,&amp;#34;X++&amp;#34;,&amp;#34;X++&amp;#34;] Output: 1 Explanation: The operations are performed as follows: Initially, X = 0.</description>
    </item>
    
    <item>
      <title>Leetcode1108 Defanging an IP Address</title>
      <link>https://tonywu1995.github.io/leetcode/1108/</link>
      <pubDate>Sun, 06 Nov 2022 19:37:19 +0800</pubDate>
      
      <guid>https://tonywu1995.github.io/leetcode/1108/</guid>
      <description>題目 Given a valid (IPv4) IP address, return a defanged version of that IP address.
A defanged IP address replaces every period &amp;ldquo;.&amp;rdquo; with &amp;ldquo;[.]&amp;rdquo;.
##　Example 1:
Input: address = &amp;#34;1.1.1.1&amp;#34; Output: &amp;#34;1[.]1[.]1[.]1&amp;#34; Example 2: Input: address = &amp;#34;255.100.50.0&amp;#34; Output: &amp;#34;255[.]100[.]50[.]0&amp;#34; 題意: 將字串.替換成[.]就好了 解法 import ( &amp;#34;strings&amp;#34; ) func defangIPaddr(address string) string { return strings.Replace(address, &amp;#34;.&amp;#34;, &amp;#34;[.]&amp;#34;, -1) } 加個測試，看看結果是不是如預期 import ( &amp;#34;github.com/stretchr/testify/assert&amp;#34; &amp;#34;testing&amp;#34; ) func Test_defangIPaddr(t *testing.T) { address := &amp;#34;1.</description>
    </item>
    
  </channel>
</rss>
