{"id":20649216,"url":"https://github.com/liulinboyi/algorithm-and-data-structure-of-javascript","last_synced_at":"2025-03-09T19:16:42.228Z","repository":{"id":109594794,"uuid":"197003398","full_name":"liulinboyi/Algorithm-and-data-structure-of-JavaScript","owner":"liulinboyi","description":"通过leetcode解题来学习JavaScript 的算法与数据结构(Algorithm and data structure of JavaScript)","archived":false,"fork":false,"pushed_at":"2019-07-22T07:28:15.000Z","size":46,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-07T01:23:32.069Z","etag":null,"topics":["algorithm","javascript","leetcode","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/liulinboyi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-15T13:23:48.000Z","updated_at":"2020-04-10T14:19:15.000Z","dependencies_parsed_at":"2023-03-13T14:07:45.980Z","dependency_job_id":null,"html_url":"https://github.com/liulinboyi/Algorithm-and-data-structure-of-JavaScript","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liulinboyi%2FAlgorithm-and-data-structure-of-JavaScript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liulinboyi%2FAlgorithm-and-data-structure-of-JavaScript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liulinboyi%2FAlgorithm-and-data-structure-of-JavaScript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liulinboyi%2FAlgorithm-and-data-structure-of-JavaScript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liulinboyi","download_url":"https://codeload.github.com/liulinboyi/Algorithm-and-data-structure-of-JavaScript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242737018,"owners_count":20177092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","javascript","leetcode","typescript"],"created_at":"2024-11-16T17:13:11.011Z","updated_at":"2025-03-09T19:16:42.203Z","avatar_url":"https://github.com/liulinboyi.png","language":"TypeScript","readme":"# JavaScript 的算法与数据结构\n\n\n## 所有代码均通过LeetCode 测试\n[LeetCode](https://leetcode-cn.com/u/liu-lin-bo-yi)\n\n[![CircleCI](https://circleci.com/gh/liulinboyi/Algorithm-and-data-structure-of-JavaScript/tree/master.svg?style=svg)](https://circleci.com/gh/liulinboyi/Algorithm-and-data-structure-of-JavaScript/tree/master)\n\n## 字符串\n\n1. [翻转字符串](https://github.com/liulinboyi/Algorithm-and-data-structure-of-JavaScript/blob/master/src/%E7%BF%BB%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2.ts)\n\n\u003cdetails\u003e\n  \u003csummary\u003e代码\u003c/summary\u003e\n  \u003cpre\u003e\n  \u003ccode\u003efunction sort(str) {\n  let temp = str\n    .split(/\\s/g)\n    .map(item =\u003e {\n      return item\n        .split(\"\")\n        .reverse()\n        .join(\"\");\n    })\n    .join(\" \");\n  console.log(temp);\n  return temp;\n}\n\n// module.exports = sort;\nexport default sort;\u003c/code\u003e\n\n  \u003c/pre\u003e\n\u003c/details\u003e\n\n2. [二进制子串](https://github.com/liulinboyi/Algorithm-and-data-structure-of-JavaScript/blob/master/src/%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%90%E4%B8%B2.ts)\n\n\u003cdetails\u003e\n  \u003csummary\u003e代码\u003c/summary\u003e\n  \u003cpre\u003e\n  \u003ccode\u003eexport default s =\u003e {\n  let last = 0; // last 上一次连续的个数\n  let cur = 0; // cur  当前数字连续的个数\n  let count = 0; // 符合规则子串的数量\n  let len = s.length;\n  for (let i = 0; i \u003c len - 1; i++) {\n    cur++;\n    if (last \u003e= cur) {\n      count++;\n    }\n    if (s[i] != s[i + 1]) {\n      last = cur;\n      cur = 0;\n    }\n  }\n// 最后一位情况\n// cur ==0 \u003c=\u003e 后两位不同\nif (cur == 0) {\ncur = 1;\n} else {\ncur++;\n}\nif (last \u003e= cur) {\ncount++;\n}\nreturn count;\n};\u003c/code\u003e\n\n  \u003c/pre\u003e\n\u003c/details\u003e\n\n3. [卡牌分组](https://github.com/liulinboyi/Algorithm-and-data-structure-of-JavaScript/blob/master/src/array/%E5%8D%A1%E7%89%8C%E5%88%86%E7%BB%84new.ts)\n\n\u003cdetails\u003e\n  \u003csummary\u003e代码\u003c/summary\u003e\n  \u003cpre\u003e\n  \u003ccode\u003e/**\n * @param {number[]} deck\n * @return {boolean}\n */\n\nvar group = arr =\u003e {\n  let single:Array\u003cany\u003e = [...new Set(arr)];\n  let temp = [];\n  let count:Array\u003cany\u003e = [];\n  let a = 0;\n  single.forEach(item =\u003e {\n    count[item] = 0;\n    for (let i = 0; i \u003c arr.length; i++) {\n      a = item;\n      if (a === arr[i]) {\n        count[a]++;\n        temp.push(arr[i]);\n      }\n    }\n  });\n  count = count.filter(item =\u003e item !== \"empty\");\n  return count;\n};\n//求出任意正整数的最大公约数\nfunction gcd(a, b) {\n  if (b === 0) {\n    return a;\n  } else {\n    //两个数 a b 的最大公约数\n    return gcd(b, a % b);\n  }\n}\nvar a, b, c;\n\nvar hasGroupsSizeX = function (arr) {\n    arr = group(arr)\n    function gcdout(arr){\n    if(arr.length \u003c= 1){\n       return arr[0] % 2 === 0 ? true : false\n    }\n  //两个数才有 最大公约数\n  if (arr.length \u003e 1) {\n    a = arr[0];\n    b = arr[1];\n    c = gcd(a, b);\n    arr.splice(0, 2, c);\n    gcdout(arr);\n  } else {\n    return;\n  }\n  if (c \u003e 1) {\n    return true;\n  }\n  return false;\n    }\n    \n    return gcdout(arr)\n \n}\u003c/code\u003e\n\n  \u003c/pre\u003e\n\u003c/details\u003e\n\n4. [电话号码的字母组合](https://github.com/liulinboyi/Algorithm-and-data-structure-of-JavaScript/blob/master/src/array/%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%E7%9A%84%E5%AD%97%E6%AF%8D%E7%BB%84%E5%90%88.ts)\n\n\u003cdetails\u003e\n  \u003csummary\u003e代码\u003c/summary\u003e\n  \u003cpre\u003e\n  \u003ccode\u003e/**\n * @param {string} num\n * @return {string[]}\n */\n\nvar letterCombinations = function (num){\n    var numobj = {\n2: \"abc\",\n3: \"def\",\n4: \"ghi\",\n5: \"jkl\",\n6: \"mno\",\n7: \"pqrs\",\n8: \"tuv\",\n9: \"wxyz\"\n}\n    var a,b;\n    var res = []\n    if(num.length === 0) return []\n    if(num.length === 1){\n            let temp = numobj[num].split(\"\")\n            return temp\n        }\n    if(typeof num === \"string\"){\n        var arr = num.split(\"\")\n        var flag = arr.every((element, index, array) =\u003e {\n        return Object.keys(numobj).includes(element)\n        })\n        if(!flag){\n            throw new Error(\"请输入的字符串包含2-9\");\n            return\n        }\n        a = numobj[arr[0]];\n        b = numobj[arr[1]];\n    }else if(num instanceof Array){\n        var arr = num;\n        a = num[0];\n        b = numobj[num[1]];\n    }\n    \n    for(var i = 0;i\u003ca.length;i++){\n        for(var j = 0;j\u003cb.length;j++){\n            res.push(a[i]+b[j])\n        }\n    }\n    arr.splice(0,2,res)\n    if(arr.length\u003e1){\n        letterCombinations(arr)\n        return arr[0]\n    }else{\n        return arr[0]\n    }\n\u003c/code\u003e\n\n  \u003c/pre\u003e\n\u003c/details\u003e\n\n5. [种花问题](https://github.com/liulinboyi/Algorithm-and-data-structure-of-JavaScript/blob/master/src/array/%E7%A7%8D%E8%8A%B1%E9%97%AE%E9%A2%98.ts)\n\n\u003cdetails\u003e\n  \u003csummary\u003e代码\u003c/summary\u003e\n  \u003cpre\u003e\n  \u003ccode\u003e/**\n * @param {number[]} flowerbed\n * @param {number} n\n * @return {boolean}\n */\nvar canPlaceFlowers = function(flower, n) {\n  let max = 0;\n    if(flower.length\u003e2){\n        flower = [...flower,0]\n        for (let i = 0; i \u003c flower.length - 1; i++) {\n        if (flower[i] === 0) {\n          if (i === 0 \u0026\u0026 flower[1] === 0) {\n            max++;\n            i++;\n          } else if (flower[i - 1] === 0 \u0026\u0026 flower[i + 1] === 0) {\n            max++;\n            i++;\n          }\n        }\n      }\n    }else{\n        if(!flower.includes(1)) max++\n    }\n  if (max \u003e= n) return true;\n\n  return false;\n};\u003c/code\u003e\n\n  \u003c/pre\u003e\n\u003c/details\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliulinboyi%2Falgorithm-and-data-structure-of-javascript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliulinboyi%2Falgorithm-and-data-structure-of-javascript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliulinboyi%2Falgorithm-and-data-structure-of-javascript/lists"}