<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                >[success] # 1002 查找共用字符 * 題目描述 給你一個字符串數組 words ,請你找出所有在 words 的每個字符串中都出現的共用字符( 包括重復字符),并以數組形式返回。你可以按 任意順序 返回答案。 ? * 示例 1: 輸入:words = ["bella","label","roller"] 輸出:["e","l","l"] * 示例 2: 輸入:words = ["cool","lock","cook"] 輸出:["c","o"] ? * 提示: 1 <= words.length <= 100 1 <= words[i].length <= 100 words[i] 由小寫英文字母組成 來源:力扣(LeetCode) 鏈接:https://leetcode.cn/problems/find-common-characters 著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。 >[danger] ##### 第一次解題思路 用**Map** 作為記錄字符串出現次數,對比出現次數最小值即為出現的**共用字符** 舉個例子 ["bella","zabel","roller"] ![](https://img.kancloud.cn/ee/44/ee44af5b518fccc393b60c57966e4fd8_813x776.png) ![](https://img.kancloud.cn/35/a2/35a26734c884d84ad67a82838ae6bfd4_492x242.png) ~~~ var commonChars = function (words) { // 用map 計算出第一組每個字母個數 const minMap = {} const [firstGroup, ...otherGroup] = words for (let w of firstGroup) { if (!minMap[w]) { minMap[w] = 0 } minMap[w]++ } // 計算其他組單詞出現頻率 otherGroup.forEach((word) => { const currentMap = {} for (let w of word) { if (minMap[w] && !currentMap[w]) { currentMap[w] = 0 } currentMap[w]++ } // 比較最少次數 for (let k in minMap) { // 跟新最少出現次數頻率 minMap[k] = Math.min(minMap[k], currentMap[k] || 0) } }) const res = [] for (let k in minMap) { let count = minMap[k] while (count) { res.push(k) count-- } } return res } ~~~ >[info] ## 題解 題中說`由小寫英文字母組成` 因此只能有**26**個單詞,利用固定26這個條件可以用**數組形式**,創建一個長度為26的數組,將字母找到對應數組位置可利用**ASCII**碼,當想獲取對應單詞時候可以用**ASCII**反解析映射對應的字母 ![](https://img.kancloud.cn/37/35/37355e02008c516e3d912a6e97a1a504_966x754.png) 將每組收集,對比每項最小值,即可得到**共用字符** ![](https://img.kancloud.cn/c7/2c/c72ccb09f7de4ae71220ab531271bc65_902x748.png) >[danger] ##### js 題解 ~~~ var commonChars = function (words) { // 將第一組值映射記錄到對應數組上 const [firstGroup, ...otherGroup] = words // 創建長度26的數組保存對應 字母 const minWordCountsLs = new Array(26).fill(0) for (let w of firstGroup) { // 都是小寫字母 為了找到屬于數組對應映射位置使用,a的ASCII 碼為97 // 因此 減掉97 例如 a 原本應該保存數組0的位置 97-97 =0 const idx = w.charCodeAt(0) - 97 ++minWordCountsLs[idx] } // 依次比較找到 相互直接最小的出現次數替換 for (let other of otherGroup) { // 計算出當前組字符出現次數 const currentCountsLs = new Array(26).fill(0) for (let w of other) { const idx = w.charCodeAt(0) - 97 ++currentCountsLs[idx] } // 循環比較更新最小出現值數組 minWordCountsLs.forEach((item, index) => { minWordCountsLs[index] = Math.min(item, currentCountsLs[index]) }) } // 將出現此次字母記錄 return minWordCountsLs.reduce((acc, count, index) => { while (count) { acc.push(String.fromCharCode(index + 97)) count-- } return acc }, []) } ~~~ >[danger] ##### java ~~~ import java.util.List; import java.util.ArrayList; public class Solution { public List<String> commonChars(String[] words) { // 創建一個26長度數組 int[] minLs = new int[26]; // 循環第一組數組記錄到數組中 for(char c:words[0].toCharArray()){ // 減掉a從0開始可以映射對應數組 int idx = c -'a'; minLs[idx]++; } // 依次尋其他項找到最小數組 for(int i=1;i< words.length;i++){ // 記錄數組長度 int[] currentLs = new int[26]; // 循環字符串 for(char c:words[i].toCharArray()){ int idx = c -'a'; currentLs[idx]++; } // 循環找到相交出現最小次數 for(int j=0;j<26;j++){ minLs[j] = Math.min(minLs[j],currentLs[j]); } } // 求出對應字母 List<String> wLs = new ArrayList<>(); for(int i=0;i<26;i++){ int counts = minLs[i]; while (counts>0){ wLs.add(String.valueOf((char)(i+'a'))); counts--; } } return wLs; } } ~~~ >[info] ## 其他解題思路 兩個字符進行比較,如果有相同的則記錄下來,但要注意需要將已經比較后的值去處,否則會出現重復比較 ![](https://img.kancloud.cn/88/fd/88fdc91667340f345b8dbbfbda92d77c_684x359.png) ~~~ var commonChars = function (words) { return words.reduce((acc, cur) => { return [...acc].filter((item) => { const flag = cur.includes(item) cur = cur.replace(item, '') return flag }) }) } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看