<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string`""`. **Example 1:** ~~~ Input: ["flower","flow","flight"] Output: "fl" ~~~ **Example 2:** ~~~ Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. ~~~ **Note:** All given inputs are in lowercase letters`a-z`. ``` var longestCommonPrefix = function(strs) { if(!strs.length) return ''; var len = strs.reduce((pre,item)=>{ return Math.min(pre,item.length); },Number.MAX_VALUE); var ans=''; for(var i=0;i<len;i++){ var a = strs[0][i]; var f = strs.every((item)=>{ return item[i] === a; }); if(!f) break; ans += a; } return ans; }; ``` ``` var longestCommonPrefix = function(strs) { if (!strs.length) return "" let baseWord = strs[0]; for (let word = 1; word < strs.length; word++) { // currWord = String(strs[word]) while (String(strs[word]).indexOf(baseWord) !== 0 && baseWord !== "") { baseWord = baseWord.substring(0, baseWord.length - 1) } } return baseWord; }; ``` ``` var longestCommonPrefix = function(strs) { if (strs.length == 0) return ""; let prefix = strs[0]; for (let i = 1; i < strs.length; i++) while (strs[i].indexOf(prefix) != 0) { prefix = prefix.slice(0, prefix.length - 1); if (prefix==="") return ""; } return prefix; }; ``` reduce() 方法接收一個函數作為累加器(accumulator),數組中的每個值(從左到右)開始縮減,最終為一個值。 reduce() 為數組中的每一個元素依次執行回調函數,不包括數組中被刪除或從未被賦值的元素,接受四個參數:初始值(或者上次回調函數的返回值),當前元素值,當前索引,調用reduce的數組。 ``` arr.reduce(callback, [initialValue]) ``` callback (執行數組中每個值的函數,包含四個參數) - previousValue (上一次調用回調返回的值,或者是提供的初始值(initialValue)) - currentValue (數組中當前被處理的元素) - index (當前元素在數組中的索引) - array (調用 reduce 的數組) initialValue (作為第一次調用 callback 的第一個參數) ``` var arr = [1, 2, 3, 4]; var sum = arr.reduce(function(pre, cur, index, arr) { console.log(pre, cur, index); return pre + cur; }) console.log(arr, sum); ``` ![](https://img.kancloud.cn/3c/e5/3ce5c24799a2b9fca1068f2be67500d0_434x170.png)
                  <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>

                              哎呀哎呀视频在线观看