国产睡熟迷奷白丝护士系列精品,中文色字幕网站,免费h网站在线观看的,亚洲开心激情在线

      <sup id="hb9fh"></sup>
          1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

            手機(jī)站
            千鋒教育

            千鋒學(xué)習(xí)站 | 隨時隨地免費(fèi)學(xué)

            千鋒教育

            掃一掃進(jìn)入千鋒手機(jī)站

            領(lǐng)取全套視頻
            千鋒教育

            關(guān)注千鋒學(xué)習(xí)站小程序
            隨時隨地免費(fèi)學(xué)習(xí)課程

            當(dāng)前位置:首頁  >  技術(shù)干貨  > 10個算法提升你的JavaScript技能

            10個算法提升你的JavaScript技能

            來源:千鋒教育
            發(fā)布人:wjy
            時間: 2022-06-01 12:00:00 1654056000

              # 10個算法提升你的JavaScript技能

            10個算法提升你的JavaScript技能

              **1)在數(shù)組中查找缺失的數(shù)字**

              ```text

              Input: [1, 2, 3, 4, 6, 7, 8, 9, 10]

              Output: 5

              const find_missing = function(input) {

              let n = input.length + 1;

              let sum = 0;

              for (let i in input) {

              sum += input[i];

              }

              return Math.floor((n * (n + 1)) / 2) - sum;

              };

              ```

              提示:算術(shù)級數(shù)和公式:

              ![img](https://pic3.zhimg.com/80/v2-06445d8bc3e42de5a9bc9da81392d52e_720w.jpg)

              **2)反轉(zhuǎn)整數(shù)**

              ```text

              Input: num = 123

              Output: 321

              Input: num = -123

              Output: -321

              const reverse = function(num) {

              let result = 0;

              while (num !== 0) {

              result = result * 10 + num % 10;

              // Math.trunc() 方法會將數(shù)字的小數(shù)部分去掉,只保留整數(shù)部分

              num = Math.trunc(num / 10);

              }

              if (result > 2**31 || result < -(2**31)) return 0;

              return result;

              };

              ```

              **3) 數(shù)組排列**

              ```text

              Input: [1,2,3]

              Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

              const permute = function(nums) {

              let results = [];

              let go = (current) => {

              if (current.length === nums.length){

              results.push(current);

              return;

              }

              nums.forEach(n => {

              if (!current.includes(n)){

              go([...current, n]);

              }

              });

              }

              go([]);

              return results;

              };

              ```

              **4) 字符串中的排列**

              ```text

              Input: s1 = "ab", s2 = "eidbao"

              Output: true

              Input: s1 = "aa", s2 = "eidbao"

              Output: false

              const checkPermutation = function(s1, s2) {

              const len1 = s1.length, len2 = s2.length;

              if (len1 > len2) return false;

              const count = Array(26).fill(0);

              for (let i = 0; i < len1; i++) {

              count[s1.charCodeAt(i)-97]++;

              count[s2.charCodeAt(i)-97]--;

              }

              if (!count.some(e => e !== 0)) return true;

              for (let i = len1; i < len2; i++) {

              count[s2.charCodeAt(i)-97]--;

              count[s2.charCodeAt(i-len1)-97]++;

              if (!count.some(e => e !== 0)) return true;

              }

              return false;

              };

              ```

              **5) 最長有效括號**

              ```text

              Input: "(()"

              Output: 2

              Input: ")()())"

              Output: 4

              const longestValidParentheses = function(S) {

              let stack = [-1], ans = 0;

              for (let i = 0; i < S.length; i++)

              if (S[i] === '(') stack.push(i)

              else if (stack.length === 1) stack[0] = i

              else stack.pop(), ans = Math.max(ans, i - stack[stack.length-1])

              return ans

              };

              ```

              **6) 4Sum**

              ```text

              const fourSum = function(nums, target) {

              let result = [];

              let length = nums.length;

              if (length < 4) return result;

              nums = nums.sort((a, b) => a - b );

              for (let i = 0; i < length - 3; i++) {

              if (nums[i] === nums[i - 1]) continue;

              for (let j = i + 1; j < length - 2; j++) {

              if (j > i + 1 && nums[j] === nums[j - 1]) continue;

              let k = j + 1;

              let l = length - 1;

              while (k < l) {

              const sum = nums[i] + nums[j] + nums[k] + nums[l];

              if (sum === target) {

              result.push([nums[i], nums[j], nums[k], nums[l]])

              }

              if (sum <= target) {

              k += 1;

              while (nums[k] === nums[k - 1]) {

              k += 1;

              }

              }

              if (sum >= target) {

              l -= 1;

              while (nums[l] === nums[l + 1]) {

              l -= 1;

              }

              }

              }

              }

              }

              return result;

              };

              ```

              **7)字符串相乘**

              ```text

              Input: num1 = "2", num2 = "3"

              Output: "6"

              const multiply = function(num1, num2) {

              if (num1 == 0 || num2 == 0) return '0';

              const result = [];

              for (let a = num1.length - 1; a >= 0; a--) {

              for (let b = num2.length - 1; b >= 0; b--) {

              const p1 = a + b;

              const p2 = a + b + 1;

              const sum = (result[p2] ?? 0) + num1[a] * num2[b];

              result[p1] = (result[p1] ?? 0) + Math.floor(sum / 10);

              result[p2] = sum % 10;

              }

              }

              result[0] == 0 && result.shift();

              return result.join('');

              };

              ```

              **8) 最短回文**

              ```text

              Input: s = "aacecaaa"

              Output: "aaacecaaa"

              Input: s = "abcd"

              Output: "dcbabcd"

              const shortestPalindrome = function(s) {

              let index = 0;

              for (let i = s.length - 1; i >= 0; i--) {

              if (s[i] === s[index]) index++;

              }

              if (index === s.length) return s;

              let remainingRev = s.substring(index, s.length);

              console.log(remainingRev);

              remainingRev = reverse(remainingRev);

              return remainingRev + shortestPalindrome(s.substring(0, index)) + s.substring(index);

              };

              function reverse(string) {

              let myString = '';

              for (let i = string.length - 1; i >= 0; i--) {

              myString = myString + string[i];

              }

              return myString;

              };

              ```

              **9)整數(shù)到英文單詞**

              ```text

              Input: num = 123

              Output: "One Hundred Twenty Three"

              Input: num = 1234567

              Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

              const numberToWords = function(num) {

              let result = toHundreds(num % 1000);

              const bigNumbers = ["Thousand", "Million", "Billion"];

              for (let i = 0; i < 3; ++i) {

              num = Math.trunc(num / 1000);

              result = num % 1000 !== 0 ? [toHundreds(num % 1000), bigNumbers[i], result].filter(Boolean).join(" ") : result;

              }

              return result.length === 0 ? "Zero" : result;

              }

              function toHundreds(num) {

              const numbers = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",

              "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];

              const tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];

              const result = Array(3).fill("");

              let a = Math.trunc(num / 100), b = num % 100, c = num % 10;

              result[0] = a > 0 && `${numbers[a]} Hundred`;

              result[1] = b < 20 ? numbers[b] : tens[Math.trunc(b / 10)]

              result[2] = b >= 20 && `${numbers[c]}`;

              return result.filter(Boolean).join(" ");

              }

              ```

              **10) 贖金票據(jù)**

              ```text

              Input: ransomNote = "aa", magazine = "ab"

              Output: false

              Input: ransomNote = "aa", magazine = "aab"

              Output: true

              const canConstruct = function(ransomNote, magazine) {

              if (ransomNote.length > magazine.length) return false;

              let magMap = new Map();

              for(let char of magazine) {

              magMap.set(char, (magMap.get(char) || 0 ) + 1);

              }

              for(let note of ransomNote) {

              let counter = magMap.get(note);

              if (!counter) return false;

              magMap.set(note, --counter);

              }

              return true;

              };

              ```

              經(jīng)過一番研究,我得出了這 10 個算法,每個算法都有特殊的技巧,不僅可以提高

              我們的 JavaScript 技能,還可以幫助我們提高批判性思維能力。

              **- End -**

              更多關(guān)于“html5培訓(xùn)”的問題,歡迎咨詢千鋒教育在線名師。千鋒已有十余年的培訓(xùn)經(jīng)驗(yàn),課程大綱更科學(xué)更專業(yè),有針對零基礎(chǔ)的就業(yè)班,有針對想提升技術(shù)的提升班,高品質(zhì)課程助理你實(shí)現(xiàn)夢想。

            tags:
            聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
            10年以上業(yè)內(nèi)強(qiáng)師集結(jié),手把手帶你蛻變精英
            請您保持通訊暢通,專屬學(xué)習(xí)老師24小時內(nèi)將與您1V1溝通
            免費(fèi)領(lǐng)取
            今日已有369人領(lǐng)取成功
            劉同學(xué) 138****2860 剛剛成功領(lǐng)取
            王同學(xué) 131****2015 剛剛成功領(lǐng)取
            張同學(xué) 133****4652 剛剛成功領(lǐng)取
            李同學(xué) 135****8607 剛剛成功領(lǐng)取
            楊同學(xué) 132****5667 剛剛成功領(lǐng)取
            岳同學(xué) 134****6652 剛剛成功領(lǐng)取
            梁同學(xué) 157****2950 剛剛成功領(lǐng)取
            劉同學(xué) 189****1015 剛剛成功領(lǐng)取
            張同學(xué) 155****4678 剛剛成功領(lǐng)取
            鄒同學(xué) 139****2907 剛剛成功領(lǐng)取
            董同學(xué) 138****2867 剛剛成功領(lǐng)取
            周同學(xué) 136****3602 剛剛成功領(lǐng)取
            相關(guān)推薦HOT
            今日頭條展現(xiàn)量是什么?今日頭條展現(xiàn)量規(guī)則分析

            目前頭條用戶會發(fā)現(xiàn)微頭條和問答新增了“展現(xiàn)量”指標(biāo)。那么這個今日頭條展現(xiàn)量是什么意思呢?如何提高呢?下面千鋒教育小編就和大家說...詳情>>

            2023-09-19 09:15:47
            我想直播帶貨去哪里找貨源一件代發(fā)

            現(xiàn)在直播帶貨是一個非?;鸬男袠I(yè),越來越多的人進(jìn)入到這個圈子,但是,認(rèn)真地說,直播帶貨能不能賺到錢還是要看自己。雖然這一行業(yè)火,收入高,...詳情>>

            2023-09-19 08:41:02
            入駐短視頻mcn需要多少錢?有哪些費(fèi)用?

            眾所周知,現(xiàn)在短視頻成了很多人的自媒體創(chuàng)業(yè)道路之一,越來越多人喜歡在網(wǎng)上消遣時間,購物等。也有很多優(yōu)秀的博主去做出很多新的內(nèi)容,那么入...詳情>>

            2023-09-19 08:12:47
            短視頻帶貨應(yīng)該注冊什么公司?需要什么資料?

            短視頻大家應(yīng)該都非常熟悉,現(xiàn)在很多人都喜歡在閑暇的時候刷短視頻,短視頻平臺也開始了直播帶貨,商家也都開始創(chuàng)業(yè)了,那么短視頻帶貨需要營業(yè)...詳情>>

            2023-09-19 08:09:31
            怎樣投抖加不花錢?別人能看出來嗎?

            抖音一些視頻為了獲取更多點(diǎn)擊,一些博主機(jī)會投抖加。其實(shí)抖加的投放沒有絕對正確的方法,而不同賬號和視頻投放的策略都是不一樣的,不過一般情...詳情>>

            2023-09-19 08:00:10
            開班信息
            北京校區(qū)
            • 北京校區(qū)
            • 大連校區(qū)
            • 廣州校區(qū)
            • 成都校區(qū)
            • 杭州校區(qū)
            • 長沙校區(qū)
            • 合肥校區(qū)
            • 南京校區(qū)
            • 上海校區(qū)
            • 深圳校區(qū)
            • 武漢校區(qū)
            • 鄭州校區(qū)
            • 西安校區(qū)
            • 青島校區(qū)
            • 重慶校區(qū)
            • 太原校區(qū)
            • 沈陽校區(qū)
            • 南昌校區(qū)
            • 哈爾濱校區(qū)