diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..81a66e1 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,7 @@ +node_modules +manuscript +.github +.idea +.dockerignore +.gitignore + diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..0710d7d --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,169 @@ +module.exports = { + env: { + browser: true, + es2021: true + }, + extends: [ + 'plugin:vue/essential', + 'standard' + ], + parserOptions: { + ecmaVersion: 12, + parser: '@typescript-eslint/parser', + sourceType: 'module' + }, + plugins: [ + 'vue', + '@typescript-eslint' + ], + rules: { + 'no-unused-vars': 0, + 'arrow-spacing': [2, { + before: true, + after: true + }], // 箭头函数中的箭头前后加空格 + 'block-spacing': [2, 'always'], // 花括号内前后加空格 + 'brace-style': [2, '1tbs', { + allowSingleLine: true + }], // 允许单行方法 + 'comma-dangle': [2, 'never'], // 在对象、数组文字中不使用尾随逗号 + 'comma-spacing': [2, { + before: false, + after: true + }], // 逗号后空格 + 'comma-style': [2, 'last'], // 对象、数组元素同一行后面加逗号 + 'constructor-super': 2, // 派生类的构造函数必须调用super() + curly: [2, 'multi-line'], // 方法体多行不允许省略花括号 + 'dot-location': [2, 'property'], // 点与属性在同一行 + 'eol-last': 2, // 在非空文件的末尾强制使用至少一个换行符 + 'generator-star-spacing': [2, { + before: true, + after: true + }], // 生成器函数前后加空格 + 'handle-callback-err': [2, '^(err|error)$'], // err的错误回调必须处理 + 'key-spacing': [2, { + beforeColon: false, + afterColon: true + }], // 对象字面亮键和冒号之间存在空格 + 'keyword-spacing': [2, { + before: true, + after: true + }], + 'new-cap': [2, { + newIsCap: true, + capIsNew: false + }], // 构造函数首字母大写 + 'no-class-assign': 2, // 禁止修改类申明的变量 + 'no-cond-assign': 2, // 禁止条件表达式中出现赋值操作符 + 'no-const-assign': 2, // 禁止修改 const 声明的变量 + 'no-delete-var': 2, // 禁止删除变量 + 'no-dupe-args': 2, // 禁止 function 定义中出现重名参数 + 'no-dupe-class-members': 2, // 禁止类成员中出现重复的名称 + 'no-dupe-keys': 2, // 禁止对象字面量中出现重复的 key + 'no-duplicate-case': 2, // 禁止出现重复的 case 标签 + 'no-empty-character-class': 2, // 禁止在正则表达式中使用空字符集 + 'no-empty-pattern': 2, // 禁止使用空解构模式 + 'no-eval': 2, // 禁用 eval() + 'no-ex-assign': 2, // 禁止对 catch 子句的参数重新赋值 + 'no-extend-native': 2, // 禁止扩展原生类型 + 'no-extra-bind': 2, // 禁止不必要的函数绑定 + 'no-extra-boolean-cast': 2, // 禁止不必要的布尔转换 + 'no-extra-parens': [2, 'functions'], // 禁止不必要的括号 + 'no-fallthrough': 2, // 禁止 case 语句落空 + 'no-func-assign': 2, // 禁止对 function 声明重新赋值 + 'no-implied-eval': 2, // 禁止使用类似 eval() 的方法 + 'no-inner-declarations': [2, 'functions'], // 禁止在嵌套的块中出现function 声明 + 'no-invalid-regexp': 2, // 禁止 RegExp 构造函数中存在无效的正则表达式字符串 + 'no-irregular-whitespace': 2, // 禁止不规则的空白 + 'no-iterator': 2, // 禁用 __iterator__ 属性 + 'no-label-var': 2, // 不允许标签与变量同名 + 'no-labels': [2, { + allowLoop: false, + allowSwitch: false + }], // 禁用标签语句 + 'no-lone-blocks': 2, // 禁用不必要的嵌套块 + 'no-multi-spaces': 2, // 禁止使用多个空格 + 'no-multi-str': 2, // 禁止使用多行字符串 + 'no-multiple-empty-lines': [2, { + max: 2 + }], // 禁止出现多行空行 + 'no-global-assign': 2, // 禁止对原生对象或只读的全局对象进行赋值 + 'no-unsafe-negation': 2, // 禁止对关系运算符的左操作数使用否定操作符 + 'no-new-object': 2, // 禁用 Object 的构造函数 + 'no-new-require': 2, // 禁止调用 require 时使用 new 操作符 + 'no-new-wrappers': 2, // 禁止对 String,Number 和 Boolean 使用 new 操作符 + 'no-obj-calls': 2, // 禁止把全局对象作为函数调用 + 'no-octal': 2, // 禁用八进制字面量 + 'no-octal-escape': 2, // 禁止在字符串中使用八进制转义序列 + 'no-path-concat': 2, // 禁止对 __dirname 和 __filename 进行字符串连接 + 'no-proto': 2, // 禁用 __proto__ 属性 + 'no-redeclare': 2, // 禁止多次声明同一变量 + 'no-regex-spaces': 2, // 禁止正则表达式字面量中出现多个空格 + 'no-return-assign': [2, 'except-parens'], // 禁止在 return 语句中使用赋值语句 + 'no-self-assign': 2, // 禁止自我赋值,禁止自身比较 + 'no-sequences': 2, // 禁用逗号操作符 + 'no-shadow-restricted-names': 2, // 禁止将标识符定义为受限的名字 + 'func-call-spacing': 2, // 要求或禁止在函数标识符和其调用之间有空格 + 'no-sparse-arrays': 2, // 禁用稀疏数组 + 'no-this-before-super': 2, // 禁止在构造函数中,在调用 super() 之前使用 this 或 super + 'no-trailing-spaces': 2, // 禁用行尾空格 + 'no-undef-init': 2, // 禁止将变量初始化为 undefined + 'no-unexpected-multiline': 2, // 禁止出现令人困惑的多行表达式 + 'no-unmodified-loop-condition': 2, // 禁用一成不变的循环条件 + 'no-unneeded-ternary': [2, { + defaultAssignment: false + }], // 禁止可以在有更简单的可替代的表达式时使用三元操作符 + 'no-unreachable': 2, // 禁止在 return、throw、continue 和 break 语句之后出现不可达代码 + 'no-unsafe-finally': 2, // 禁止在 finally 语句块中出现控制流语句 + 'no-useless-call': 2, // 禁止不必要的 .call() 和 .apply() + 'no-useless-computed-key': 2, // 禁止在对象中使用不必要计算属性 + 'no-useless-constructor': 2, // 禁用不必要的构造函数 + 'no-useless-escape': 0, // 禁用不必要的转义字符 + 'no-whitespace-before-property': 2, // 禁止属性前有空白 + 'no-with': 2, // 禁用 with 语句 + 'one-var': [2, { + initialized: 'never' + }], // 强制函数中的变量要么一起声明要么分开声明 + 'operator-linebreak': [2, 'after', { + overrides: { + '?': 'before', + ':': 'before' + } + }], // 强制操作符使用一致的换行符 + 'padded-blocks': [2, 'never'], // 要求或禁止块内填充 + quotes: [2, 'single', { + avoidEscape: true, + allowTemplateLiterals: true + }], // 强制使用一致的反勾号、双引号或单引号 + 'semi-spacing': [2, { + before: false, + after: true + }], // 强制在块之前使用一致的空格 + 'space-before-blocks': [2, 'always'], // 强制在块之前使用一致的空格 + 'space-before-function-paren': [2, 'never'], // 强制在 function的左括号之前使用一致的空格 + 'space-in-parens': [2, 'never'], // 强制在圆括号内使用一致的空格 + 'space-infix-ops': 2, // 要求操作符周围有空格 + 'space-unary-ops': [2, { + words: true, + nonwords: false + }], // 强制在一元操作符前后使用一致的空格 + 'template-curly-spacing': [2, 'never'], // 禁止模板字符串中的嵌入表达式周围空格的使用 + 'use-isnan': 2, // 要求使用 isNaN() 检查 NaN + 'valid-typeof': 2, // 强制 typeof 表达式与有效的字符串进行比较 + 'wrap-iife': [2, 'any'], // 要求 IIFE(立即执行函数) 使用括号括起来 + 'prefer-const': 2, // 要求使用 const 声明那些声明后不再被修改的变量 + 'object-curly-spacing': [2, 'always', { + objectsInObjects: false + }], // 强制在大括号中使用一致的空格 + 'array-bracket-spacing': [2, 'never'], // 强制数组方括号中使用一致的空格 + + /* 非强制 */ + camelcase: [0, { + properties: 'always' + }], // 对属性名称强制使用驼峰样式 + 'no-array-constructor': 2, // 禁止使用 Array 构造函数创建数组 + 'no-caller': 2, // 禁止使用 arguments.caller 和 arguments.callee + 'no-console': 'off', // 禁用console + 'no-control-regex': 0// 禁止在正则表达式中使用控制字符 + } +} diff --git a/code/ds/BinaryInsertSort.js b/code/ds/BinaryInsertSort.js index 7f6a267..1c0e92b 100644 --- a/code/ds/BinaryInsertSort.js +++ b/code/ds/BinaryInsertSort.js @@ -7,54 +7,48 @@ * @LastEditTime: 2021-03-27 12:50:00 */ - /** * 折半插入排序 - * @param {Array} arr 待排序数组 - * @param {int} len 数组长度 + * @param arr + * @param len */ -function binaryInsertSort(arr,len){ - +function binaryInsertSort(arr, len) { // 数组长度校验【非必须】 - - len=arr.length===len?len:arr.length + len = arr.length === len ? len : arr.length - for(let i=1;ihighIndex;--j){ - arr[j+1]=arr[j] + for (let j = i - 1; j > highIndex; --j) { + arr[j + 1] = arr[j] } - arr[highIndex+1]=temp; - + arr[highIndex + 1] = temp } // 返回经过折半插入排序处理的有序数组 - return arr; + return arr } +const dealArr = [5, 2, 7, 3, 18, 8, 12, 1] +console.log('插入排序前:', dealArr) +const sortResult = binaryInsertSort(dealArr, 7) -const dealArr=[5,2,7,3,18,8,12,1] -console.log('插入排序前:',dealArr) -const sortResult=binaryInsertSort(dealArr,7) - -console.log('插入排序后:',sortResult) +console.log('插入排序后:', sortResult) diff --git a/code/ds/BubbleSort.js b/code/ds/BubbleSort.js index 42c6d4f..fc318d7 100644 --- a/code/ds/BubbleSort.js +++ b/code/ds/BubbleSort.js @@ -8,23 +8,21 @@ */ - -function BubbleSort (arr, len) { +function BubbleSort(arr, len) { // 校正数组的长度 - len = arr.length == len ? len : arr.length + len = arr.length === len ? len : arr.length // 冒泡排序,让数组arr有序 for (let i = 0; i < len - 1; i++) { - - let isSorted = false; + let isSorted = false // len个数组,进行len-1趟,即:一趟冒泡 for (let j = len - 1; j > i; j--) { // 注意:这里的for循环倒序是有讲究的,想象一下泡泡不都是网上升的么.... if (arr[j - 1] > arr[j]) { // 交换元素,始终让最小的元素往上走(冒泡) - const temp = arr[j - 1]; - arr[j - 1] = arr[j]; + const temp = arr[j - 1] + arr[j - 1] = arr[j] arr[j] = temp // 需要冒泡 @@ -33,7 +31,7 @@ function BubbleSort (arr, len) { } // 第一趟比较后,如果本身序列是有序的,就直接跳出循环 if (isSorted === false) { - break; + break } } @@ -42,20 +40,19 @@ function BubbleSort (arr, len) { /** - * * 加减法交换元素的值 * 注意:JavaScript中使用需要考虑到作用域的问题 - * @param {int} a - * @param {int} b + * @param a + * @param b */ -function swap (a, b) { - a = a + b; - b = a - b; - a = a - b; +function swap(a, b) { + a = a + b + b = a - b + a = a - b } const initArr = [1, 5, 8, 3, 2, 9, 16] console.log(`冒泡排序前:${initArr}`) -const sortedArr = BubbleSort(initArr, 7); -console.log(`冒泡排序后:${sortedArr}`) \ No newline at end of file +const sortedArr = BubbleSort(initArr, 7) +console.log(`冒泡排序后:${sortedArr}`) diff --git a/code/ds/QuickSort.js b/code/ds/QuickSort.js index d3813d1..ab3b686 100644 --- a/code/ds/QuickSort.js +++ b/code/ds/QuickSort.js @@ -10,61 +10,58 @@ /** * 基于分治法思想,将数组进行快速排序 - * @param {Array} arr 待排序的数组 + * @param {Array} arr 待排序的数组 * @param {int} low 数组低位角标 左指针 * @param {int} high 数组高位角标 右指针 - * @returns + * @returns */ -function QuickSort(arr,low,high){ - +function QuickSort(arr, low, high) { // low=high 说明只有一个元素,理解为有序,不做处理 // low>high 说明左右指针已经重合,数组已经遍历完,需要跳出 - if(low= pivot ) --high; - arr[low]=arr[high] + while (low < high && arr[high] >= pivot) --high + arr[low] = arr[high] // 从左往右直到比pivot大跳出循环 - while(low= 1; increment = Math.floor(increment / 2)) { // 对每组数据,进行直接排序 for (let groupIndex = 0; groupIndex < increment; ++groupIndex) { - specialStraightInsertSort(arr, len, increment, groupIndex) } - } return arr @@ -36,30 +32,30 @@ function shellSort (arr, len) { /** * 根据希尔排序的步长对分组进行直接插入排序处理 - * @param {Array} arr 排序数组 + * @param {Array} arr 排序数组 * @param {int} len 数组长度 * @param {int} increment 增量步长 * @param {int} groupIndex 分组,第几个分组 */ -function specialStraightInsertSort (arr, len, increment, groupIndex) { +function specialStraightInsertSort(arr, len, increment, groupIndex) { len = arr.length === len ? len : arr.length console.log(`数组长度:${len}----->当前步长:${increment}---->分组:${groupIndex}`) - for(let eleStartIndex=groupIndex+increment;eleStartIndex=0&&arr[j]>temp;j-=increment){ - arr[j+increment]=arr[j] + for (j = eleStartIndex - increment; j >= 0 && arr[j] > temp; j -= increment) { + arr[j + increment] = arr[j] } - arr[j+increment]=temp + arr[j + increment] = temp } - console.log('specialStraightInsertSort处理后:',arr) + console.log('specialStraightInsertSort处理后:', arr) return arr } @@ -69,18 +65,17 @@ function specialStraightInsertSort (arr, len, increment, groupIndex) { * @param{Array} arr 待排序的数组 * @param{int} len 数组arr的长度,可以用arr.length()计算得到 */ -function straightInsertSort (arr, len) { +function straightInsertSort(arr, len) { // 重新确定数组长度 - len = arr.length === len ? len : arr.length; + len = arr.length === len ? len : arr.length // 从第二个元素开始循环,共len-1次 for (let i = 1; i < len; i++) { - // 后面的额元素比前面的元素小,需要把前面大于哨兵元素有序序列,移动后面一位 if (arr[i] < arr[i - 1]) { - let j; + let j // 哨兵元素 - const temp = arr[i]; + const temp = arr[i] for (j = i - 1; arr[j] > temp; --j) { // 后移 arr[j + 1] = arr[j] @@ -93,7 +88,6 @@ function straightInsertSort (arr, len) { } return arr - } @@ -105,26 +99,26 @@ console.log('插入排序后:', sortResult) /** * 简化的希尔排序 - * @param {Array} arr + * @param {Array} arr * @returns 返回已排序号的数组,从小到大 */ -function shellSortBetter (arr) { - var len = arr.length; - var increment = Math.floor(len / 2); - while (increment != 0) { - for (var i = increment; i < len; i++) { - var temp = arr[i] +function shellSortBetter(arr) { + const len = arr.length + let increment = Math.floor(len / 2) + while (increment !== 0) { + for (let i = increment; i < len; i++) { + const temp = arr[i] for (var j = i - increment; j >= 0 && temp < arr[j]; j -= increment) { arr[j + increment] = arr[j] } - arr[j + increment] = temp; + arr[j + increment] = temp } increment = Math.floor(increment / 2) } - return arr; + return arr } console.log('简化shellSortBetter希尔排序前:', dealArr) const sortResultBetter = shellSortBetter(dealArr) -console.log('简化shellSortBetter希尔排序后:', sortResultBetter) \ No newline at end of file +console.log('简化shellSortBetter希尔排序后:', sortResultBetter) diff --git a/code/ds/StraightInsertSort.js b/code/ds/StraightInsertSort.js index 333121b..76e0069 100644 --- a/code/ds/StraightInsertSort.js +++ b/code/ds/StraightInsertSort.js @@ -13,37 +13,35 @@ * @param{Array} arr 待排序的数组 * @param{int} len 数组arr的长度,可以用arr.length()计算得到 */ -function straightInsertSort(arr,len){ +function straightInsertSort(arr, len) { // 重新确定数组长度 - len=arr.length===len?len:arr.length; + len = arr.length === len ? len : arr.length // 从第二个元素开始循环,共len-1次 - for(let i=1;itemp;--j){ - // 后移 - arr[j+1]=arr[j] + const temp = arr[i] + for (j = i - 1; arr[j] > temp; --j) { + // 后移 + arr[j + 1] = arr[j] } // 跳出循环逻辑,出现arr[j] > arr[j-1] // 哨兵即待排序的 - arr[j+1]=temp + arr[j + 1] = temp } } return arr - } -const dealArr=[5,2,7,3,18,8,12,1] -console.log('插入排序前:',dealArr) -const sortResult=straightInsertSort(dealArr,7) +const dealArr = [5, 2, 7, 3, 18, 8, 12, 1] +console.log('插入排序前:', dealArr) +const sortResult = straightInsertSort(dealArr, 7) -console.log('插入排序后:',sortResult) +console.log('插入排序后:', sortResult) diff --git a/docs/manuscripts/algorithm/algorithm.sidebar.ts b/docs/manuscripts/algorithm/algorithm.sidebar.ts index 203d306..e0634d7 100644 --- a/docs/manuscripts/algorithm/algorithm.sidebar.ts +++ b/docs/manuscripts/algorithm/algorithm.sidebar.ts @@ -1,7 +1,7 @@ export const algorithmSidebar = [ { text: '基础入门', - link: "/ds/basic_introduction", + link: '/ds/basic_introduction', collapsible: false, children: [{ text: '1.1 基本概念', @@ -12,5 +12,5 @@ export const algorithmSidebar = [ }, { text: '1.3 算法和算法评价', link: '/ds/basic_introduction/3.algorithm_and_algorithm_evaluation.md' - }], - }] \ No newline at end of file + }] + }] diff --git a/docs/manuscripts/ccp/cpp.sidebar.ts b/docs/manuscripts/ccp/cpp.sidebar.ts index 6a2b42f..2fa0fad 100644 --- a/docs/manuscripts/ccp/cpp.sidebar.ts +++ b/docs/manuscripts/ccp/cpp.sidebar.ts @@ -1,6 +1,6 @@ -export const cppSidebar=[ - { - text:"计算机组成原理", - children:[] - } -] \ No newline at end of file +export const cppSidebar = [ + { + text: '计算机组成原理', + children: [] + } +] diff --git a/docs/manuscripts/cn/cn.sidebar.ts b/docs/manuscripts/cn/cn.sidebar.ts index 40803dc..ffa20b4 100644 --- a/docs/manuscripts/cn/cn.sidebar.ts +++ b/docs/manuscripts/cn/cn.sidebar.ts @@ -1,6 +1,6 @@ -export const cnSidebar=[ - { - text:"计算机网络", - children:[] - } -] \ No newline at end of file +export const cnSidebar = [ + { + text: '计算机网络', + children: [] + } +] diff --git a/docs/manuscripts/ds/ds.sidebar.ts b/docs/manuscripts/ds/ds.sidebar.ts index e92b01d..b616be0 100644 --- a/docs/manuscripts/ds/ds.sidebar.ts +++ b/docs/manuscripts/ds/ds.sidebar.ts @@ -1,7 +1,7 @@ export const dsSidebar = [ { text: '基础入门', - prefix: "basic-introduction", + prefix: 'basic-introduction', collapsible: false, children: [ { @@ -16,12 +16,12 @@ export const dsSidebar = [ text: '1.3 算法和算法评价', link: '3.algorithm_and_algorithm_evaluation.md' } - ], + ] }, { text: '线性表', collapsible: false, - prefix: "linear-table", + prefix: 'linear-table', children: [ { text: '2.1 基础概念和操作', @@ -58,11 +58,11 @@ export const dsSidebar = [ text: '2.9 零碎知识补充', link: '9.piecemeal_knowledge_supplement.md' } - ], + ] }, { - text: "栈和队列", - prefix: "栈和队列", + text: '栈和队列', + prefix: '栈和队列', collapsible: false, children: [ { @@ -98,4 +98,4 @@ export const dsSidebar = [ } ] } -] \ No newline at end of file +] diff --git a/docs/manuscripts/note-map/note-map.sidebar.ts b/docs/manuscripts/note-map/note-map.sidebar.ts index 3189867..e104fef 100644 --- a/docs/manuscripts/note-map/note-map.sidebar.ts +++ b/docs/manuscripts/note-map/note-map.sidebar.ts @@ -1,23 +1,23 @@ -export const noteMapSidebar=[{ - text: '考研相关', - children: [{ - text: '测试', - link: '/333' - }] +export const noteMapSidebar = [{ + text: '考研相关', + children: [{ + text: '测试', + link: '/333' + }] }, { - text: "思维导图", - children: [{ - text: '数据结构', - link: 'ds-map.md' - }, { - text: '操作系统', - link: 'os-map.md' - }, { - text: '计算机组成原理', - link: 'ccp-map.md' - }, { - text: '计算机网络', - link: 'cn-map.md' - }] -}] \ No newline at end of file + text: '思维导图', + children: [{ + text: '数据结构', + link: 'ds-map.md' + }, { + text: '操作系统', + link: 'os-map.md' + }, { + text: '计算机组成原理', + link: 'ccp-map.md' + }, { + text: '计算机网络', + link: 'cn-map.md' + }] +}] diff --git a/docs/manuscripts/os/os.sidebar.ts b/docs/manuscripts/os/os.sidebar.ts index 92f6f13..de36300 100644 --- a/docs/manuscripts/os/os.sidebar.ts +++ b/docs/manuscripts/os/os.sidebar.ts @@ -1,6 +1,6 @@ export const osSidebar = [ - { - text: "操作系统", - children: [] - } -] \ No newline at end of file + { + text: '操作系统', + children: [] + } +] diff --git a/manuscripts/数据结构/code/BinaryInsertSort.cpp b/manuscripts/数据结构/code/BinaryInsertSort.cpp deleted file mode 100644 index c55dbe8..0000000 --- a/manuscripts/数据结构/code/BinaryInsertSort.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * @Description: 折半插入算法【伪代码】 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-04-15 18:27:59 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-03-27 12:19:13 - */ - -void BinaryInsertSort(ElemType Arr[],int n){ - - int i,j,lowIndex,heightIndex,midIndex; - - for(i=2;j<=n;i++){ - // 将待排序的元素暂存在Arr[0]上 - Arr[0]=Arr[i]; - - lowIndex=1; // 左侧子表 折半查找起始位置 - heightIndex=i-1; // 左侧子表 折半查找结束位置 - - while(lowIndex<=heightIndex){ - - // 左侧有序子表的中间位置角标 - midIndex=(lowIndex+hightIndex)/2; - - if(Arr[midIndex].key>Arr[0].key){ - // 小于中间元素,插入位置在子表左侧 - hightIndex=mid-1 - }else{ - // 大于或者等于中间元素,插入位置在子表右侧 - lowIndex=midIndex+1; - } - } - - // 跳出循环需要(lowIndex>hightIndex),说明待插入位置的角标在hightIndex之后,为 hightIndex+1,此时需要将(hightIndex,i)之间的所有元素后移 - - for(j=i-1;j>heightIndex;--j){ - Arr[j+1]=Arr[j] - } - - // 后移完成后,将元素Arr[0]赋值到位置(hightIndex+1)上 - Arr[heightIndex+1]=Arr[0] - } -} diff --git a/manuscripts/数据结构/code/BinaryInsertSort.js b/manuscripts/数据结构/code/BinaryInsertSort.js deleted file mode 100644 index 7f6a267..0000000 --- a/manuscripts/数据结构/code/BinaryInsertSort.js +++ /dev/null @@ -1,60 +0,0 @@ -/* - * @Description: 折半插入排序【JavaScript版本】 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-03-27 12:35:17 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-03-27 12:50:00 - */ - - -/** - * 折半插入排序 - * @param {Array} arr 待排序数组 - * @param {int} len 数组长度 - */ -function binaryInsertSort(arr,len){ - - // 数组长度校验【非必须】 - - len=arr.length===len?len:arr.length - - - for(let i=1;ihighIndex;--j){ - arr[j+1]=arr[j] - } - arr[highIndex+1]=temp; - - } - - // 返回经过折半插入排序处理的有序数组 - return arr; -} - - - -const dealArr=[5,2,7,3,18,8,12,1] -console.log('插入排序前:',dealArr) -const sortResult=binaryInsertSort(dealArr,7) - -console.log('插入排序后:',sortResult) - diff --git a/manuscripts/数据结构/code/BubbleSort.cpp b/manuscripts/数据结构/code/BubbleSort.cpp deleted file mode 100644 index 9eafd90..0000000 --- a/manuscripts/数据结构/code/BubbleSort.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * @Description: 冒泡排序 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-03-31 08:24:18 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-06 07:26:15 - */ - - -void BubbleSwapSort(ElemType A[], int n){ - - for(i=0;ii;j--){ - if(A[j-1].key>A[j].key){ - - // 将两个元素A[j-1]、A[j]进行交换,有多种方法 - swap(A[j-1],A[j]) - // 确认已发生交换 - flag=true - } - } - - // 本趟遍历后没有发生交换,说明表已经有序 - if(flag==false){ - return ; - } - } -} - -/** - * 加减法实现两个元素值互换 - * - */ -void swap(int a, int b){ - // 此时a为两值的和 - a=a+b; - // 此时b的值为a - b=a-b - // 如何实现让a的值为b呢??此时a的值为b - a=a-b; -} - -/** - * 临时变量实现两个元素值的互换 - * - */ -void swap(int a,int b){ - int temp; - temp=a; - a=b; - b=temp -} - diff --git a/manuscripts/数据结构/code/BubbleSort.js b/manuscripts/数据结构/code/BubbleSort.js deleted file mode 100644 index 42c6d4f..0000000 --- a/manuscripts/数据结构/code/BubbleSort.js +++ /dev/null @@ -1,61 +0,0 @@ -/* - * @Description: 冒泡排序【JavaScript版本】 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-06 07:26:59 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-06 08:01:19 - */ - - - -function BubbleSort (arr, len) { - // 校正数组的长度 - len = arr.length == len ? len : arr.length - - // 冒泡排序,让数组arr有序 - for (let i = 0; i < len - 1; i++) { - - let isSorted = false; - - // len个数组,进行len-1趟,即:一趟冒泡 - for (let j = len - 1; j > i; j--) { - // 注意:这里的for循环倒序是有讲究的,想象一下泡泡不都是网上升的么.... - if (arr[j - 1] > arr[j]) { - // 交换元素,始终让最小的元素往上走(冒泡) - const temp = arr[j - 1]; - arr[j - 1] = arr[j]; - arr[j] = temp - - // 需要冒泡 - isSorted = true - } - } - // 第一趟比较后,如果本身序列是有序的,就直接跳出循环 - if (isSorted === false) { - break; - } - } - - return arr -} - - -/** - * - * 加减法交换元素的值 - * 注意:JavaScript中使用需要考虑到作用域的问题 - * @param {int} a - * @param {int} b - */ -function swap (a, b) { - a = a + b; - b = a - b; - a = a - b; -} - - -const initArr = [1, 5, 8, 3, 2, 9, 16] -console.log(`冒泡排序前:${initArr}`) -const sortedArr = BubbleSort(initArr, 7); -console.log(`冒泡排序后:${sortedArr}`) \ No newline at end of file diff --git a/manuscripts/数据结构/code/LinkList.cpp b/manuscripts/数据结构/code/LinkList.cpp deleted file mode 100644 index 1d050fc..0000000 --- a/manuscripts/数据结构/code/LinkList.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/* - * @Description: 单链表 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-03-04 23:38:04 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-03-05 21:30:58 - */ - - - -/* - * @Description: 单链表头插法 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-03-04 23:38:04 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2020-03-04 23:39:16 - */ -LinkList CreateListWithStartNode(LinkList &L){ - - LNode *s; - int x; - L=(LinkList)malloc(sizeof(LNode)); // 创建头结点L - L->next=NULL; // 初始化空链表 - - // 控制台输入值 - scanf("%d",&x); - - // 输入9999 表示结束 - while(x!==9999){ - // 开辟新结点存储空间 - s=(LNode*)malloc(sizeof(LNode)); - // 结点数据域赋值 - s->data=x; - // 修改指针,新结点插入表中【注意:L->next为头结点的指针域】 - s->next=L->next; - L->next=s; - scanf("%d",&x); - } - - // 返回单链表 - return L; -} - - -/* - * @Description: 单链表尾插法 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-03-04 23:38:04 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2020-03-04 23:39:16 - */ -LinkList CreateListWithEndNode(LinkList &L){ - - - int x; // 输入结点值 - L=(LinkList)malloc(sizeof(LNode)); - LNode *s; // 新结点s - LNode *r=L; // r为尾指针 - - // 控制台输入值 - scanf("%d",&x); - - while(x!==9999){ - // 开辟新结点存储空间 - s=(LNode *)malloc(sizeof(LNode)); - - // 新结点s的数据域赋值为x - s->data=x; - // 单链表L的尾指针指向新的结点s - r->next=s; - - // 指针r指向新的表尾结点 - r=s; - - scanf("%d",&x); - } - - // 表尾指针置空【重要】 - r->next=NULL; - - // 返回单链表 - return L; - -} - -/* - * @Description: 单链表按序号查找 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-03-04 23:38:04 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2020-03-04 23:39:16 - */ -LNode *GetElem(LinkList L,int i){ - int j=1; // 查询计数,初始为1 - LNode *p=L->next; // 单链表头结点指针赋值给指针p - - - // 第0个元素,则指向头结点,返回头结点 - if(i==0){ - // 头结点包含数据域和指针域 - return L; - } - - // 不等于0,却小于1,则i为负数无效,直接返回NULL,查询结果空; - if(i<1){ - return NULL; - } - - // p存在且计数没有走到初始i的位置 - while(p&&jnext; - - // 计数标记+1 - j++; - } - - // 注意: 当p不存在时, 跳出循环,p=NULL; 当p存在但是j大于等于i,跳出循环,返回查找的结果,返回p - // 从跳出循环上来分析,p要么存在即:找到的结点元素,要么为空即NULL - - // 跳出循环,返回第i个结点的指针 - return p; - -} - -/* - * @Description: 单链表按值查找 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-03-04 23:38:04 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2020-03-04 23:39:16 - */ -LNode *LocateElem(LinkList L,ElemType e){ - - // 指针【哨兵】 - LNode *p=L->next; - // 从第1个结点开始查找数据域(data)为e的结点 - while(p!=NULL&&p->data!=e){ - // 无法匹配,指针后移 - p=p->next; - } - - // 注意:p为NULL的时候,说明单链表已经遍历的尾结点了,跳出循环,没有找到目标结点; - - // 查找到第1个匹配的结点,跳出循环,返回结点指针 - return p; - // -} - diff --git a/manuscripts/数据结构/code/LinkStack.cpp b/manuscripts/数据结构/code/LinkStack.cpp deleted file mode 100644 index d13e89e..0000000 --- a/manuscripts/数据结构/code/LinkStack.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * @Description: 链栈的相关操作 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-01-15 9:19:56 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-03-13 12:27:36 - */ - -// 链栈类型定义【基础】 -typedef struct LinkNode{ - ElemType data; // 栈元素结点数据域 - struct LinkNode *next; // 栈元素结点指针域 -} *LinkStack; - -// 更为详细的定义 - -typedef struct StackNode -{ - int data;//结点数据域 - struct StackNode* next;//结点指针域 -}StackNode,* Linktop; - -//链栈的数据结构 -typedef struct LinkStack -{ - Linktop top; //栈顶结点,定义了一个指向上个结构体的指针 - int count;//元素个数 -}LinkStack; - - -/* - * @Description: 基于单链表链栈的进栈操作 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-03-04 07:36:04 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2020-03-04 11:39:16 - */ -bool linkStackPushNode(LinkStack* linkStack,int e){ - - // 判断链栈是否存在 - if (!linkStack){ - //链栈不存在,无法进栈操作,返回false - return false; - } - // 开辟栈结点元素内存控件 - StackNode* node = (StackNode*)malloc(sizeof(StackNode)); - // 新结点指针域指向链表,即栈顶指针位置,元素加入链表 - node->next = linkStack->top; - // 新结点数据域赋值 - node->data = e; - // 元素进栈,移动栈顶指针,指向新入栈的元素 - linkStack->top = node; - // 链栈元素总数+1 - linkStack->count++; - //链栈入栈成功,返回true - return true; -} - - - - -/* - * @Description: 基于单链表链栈的出栈操作 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-03-04 23:38:04 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2020-03-04 23:39:16 - */ -bool linkStackPopNode(LinkStack* linkStack,int *e){ - // 判断链栈是否存在及是否为空 - if (!linkStack || linkStack->count==0){ - //出栈失败,返回false - return false; - } - // 获取栈顶元素结点 - StackNode* node = stack->top; - - // 结点元素数据域赋值给变量e - *e = linkStack->data; - // 移动栈顶指向,栈顶指针指向待出栈结点的后继结点 - linkStack->top = node->next; - // 变量e已被赋值,释放链栈出栈元素的内存控件 - free(node); - // 链栈元素个数-1 - linkStack->count--; - // 出栈成功,返回true. - return true; -} diff --git a/manuscripts/数据结构/code/LoopQueue.cpp b/manuscripts/数据结构/code/LoopQueue.cpp deleted file mode 100644 index 02dc1d6..0000000 --- a/manuscripts/数据结构/code/LoopQueue.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * @Description: 循环队列操作 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2019-09-27 14:17:28 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-03-18 23:52:10 - */ - - - -// 队列最大存储元素个数 -#define MaxSize 50 - -// 结构体定义 -typedef struct { - // 存放队列元素 - ElemType data[MaxSize]; - // 队头指针和队尾指针 - int front,rear; -} SqQueue; - - - - -// 入队算法 -// 尾插法:Q.data[Q.rear]=x;Q.rear=(Q.rear+1)%Maxsize;Q.tag=1 -// 队空条件:Q.front== Q.rear且Q.tag==0 -int EnLoopQueue(SqQueue &Q, ElemType x){ - if(Q.front==Q.rear&&Q.tag==1){ - return 0; - } - Q.data[Q.rear]=x; - Q.rear=(Q.rear+1)%MaxSize; - Q.tag=1; - return 1; -} - - - -// 出队算法 -// 头结点删除:x=Q.data[Q.front];Q.front=(Q.front +1)%Maxsize;Q.tag=0 -// 队满条件:Q.front == Q.rear且Q.tag=1 -// 注意:当删除之后链表为空时,还需增加一步,将尾指针指向头结点 -int DeLoopQueue(SqQueue &Q, ElemType &x){ - if (Q.front==Q.rear&&Q.tag==0){ - return 0; - } - x=Q.data[Q.front]; - Q.front=(Q.front+1)%MaxSize; - Q.tag=0; - return 1; -} - - - diff --git a/manuscripts/数据结构/code/QuickSort.cpp b/manuscripts/数据结构/code/QuickSort.cpp deleted file mode 100644 index 7eb0232..0000000 --- a/manuscripts/数据结构/code/QuickSort.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * @Description: 快速排序【伪代码】 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-03-23 08:23:20 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-08 21:51:28 - */ - - -void QuickSort(ElemType A[] , int low , int high){ - - // low > high 表角标越界,low=high 子表只有一个元素,不需要进行快排,已经有序 - if(low=pivot) --high - A[low]=A[high] // 比pivot小的都移到左表 注意--high 从后往前遍历 - - while(lowhigh 跳出循环后即找到能将当前表一分为二的pivotKey值 - A[low]=pivot - // 基准元素pivot对应最终的位置角标 - return low -} \ No newline at end of file diff --git a/manuscripts/数据结构/code/QuickSort.js b/manuscripts/数据结构/code/QuickSort.js deleted file mode 100644 index d3813d1..0000000 --- a/manuscripts/数据结构/code/QuickSort.js +++ /dev/null @@ -1,70 +0,0 @@ -/* - * @Description: 快速排序【JavaScript版本】 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-08 08:20:35 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-08 21:50:12 - */ - - -/** - * 基于分治法思想,将数组进行快速排序 - * @param {Array} arr 待排序的数组 - * @param {int} low 数组低位角标 左指针 - * @param {int} high 数组高位角标 右指针 - * @returns - */ -function QuickSort(arr,low,high){ - - // low=high 说明只有一个元素,理解为有序,不做处理 - // low>high 说明左右指针已经重合,数组已经遍历完,需要跳出 - if(low= pivot ) --high; - arr[low]=arr[high] - - // 从左往右直到比pivot大跳出循环 - while(low=1;k=k/2){ - - // 增量子表进行直接插入排序 - for(i=k+1;i<=n;++i){ - - if(Arr[i].key0&&Arr[0].key=1;n/=2){ - - // // 步长为k,则对应分为k个组,分别对其进行 直接插入排序 - - for(i=1,i<=k;i++){ - - // 第一步: 对应组的元素找出来,组成新的待排序的数列 - // 第二步: 对待排序数列进行 直接插入排序 - - specialStraightInsertSort(ElemType Arr[], int n , int k , int i) - - } - - } - // 返回 - return Arr; -} - \ No newline at end of file diff --git a/manuscripts/数据结构/code/ShellSort.js b/manuscripts/数据结构/code/ShellSort.js deleted file mode 100644 index 7a2af34..0000000 --- a/manuscripts/数据结构/code/ShellSort.js +++ /dev/null @@ -1,130 +0,0 @@ -/* - * @Description: 希尔排序 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-02-21 08:07:13 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-03-28 11:37:29 - */ - - - -/** - * - * 数组的希尔排序 - * @param {Array} arr 待排序数组 - * @param {int} len 数组长度,可校验 - * @returns 返回已排序的数组,从小到大 - */ -function shellSort (arr, len) { - - // 校对数组长度 - len = arr.length === len ? len : arr.length - - // 注意处理浮点【向上取整】 防止空指针 - for (let increment = Math.floor(len / 2); increment >= 1; increment = Math.floor(increment / 2)) { - // 对每组数据,进行直接排序 - for (let groupIndex = 0; groupIndex < increment; ++groupIndex) { - - specialStraightInsertSort(arr, len, increment, groupIndex) - } - - } - - return arr -} - -/** - * 根据希尔排序的步长对分组进行直接插入排序处理 - * @param {Array} arr 排序数组 - * @param {int} len 数组长度 - * @param {int} increment 增量步长 - * @param {int} groupIndex 分组,第几个分组 - */ -function specialStraightInsertSort (arr, len, increment, groupIndex) { - len = arr.length === len ? len : arr.length - console.log(`数组长度:${len}----->当前步长:${increment}---->分组:${groupIndex}`) - - for(let eleStartIndex=groupIndex+increment;eleStartIndex=0&&arr[j]>temp;j-=increment){ - arr[j+increment]=arr[j] - } - arr[j+increment]=temp - } - - console.log('specialStraightInsertSort处理后:',arr) - return arr -} - - -/** - * 插入排序 - * @param{Array} arr 待排序的数组 - * @param{int} len 数组arr的长度,可以用arr.length()计算得到 - */ -function straightInsertSort (arr, len) { - // 重新确定数组长度 - len = arr.length === len ? len : arr.length; - - // 从第二个元素开始循环,共len-1次 - for (let i = 1; i < len; i++) { - - // 后面的额元素比前面的元素小,需要把前面大于哨兵元素有序序列,移动后面一位 - if (arr[i] < arr[i - 1]) { - let j; - // 哨兵元素 - const temp = arr[i]; - for (j = i - 1; arr[j] > temp; --j) { - // 后移 - arr[j + 1] = arr[j] - } - // 跳出循环逻辑,出现arr[j] > arr[j-1] - - // 哨兵即待排序的 - arr[j + 1] = temp - } - } - - return arr - -} - - -const dealArr = [5, 8, 2, 16, 3, 9, 1] -console.log('插入排序前:', dealArr) -const sortResult = shellSort(dealArr, 7) -console.log('插入排序后:', sortResult) - - -/** - * 简化的希尔排序 - * @param {Array} arr - * @returns 返回已排序号的数组,从小到大 - */ -function shellSortBetter (arr) { - var len = arr.length; - var increment = Math.floor(len / 2); - while (increment != 0) { - for (var i = increment; i < len; i++) { - var temp = arr[i] - for (var j = i - increment; j >= 0 && temp < arr[j]; j -= increment) { - arr[j + increment] = arr[j] - } - arr[j + increment] = temp; - } - increment = Math.floor(increment / 2) - } - return arr; -} - - -console.log('简化shellSortBetter希尔排序前:', dealArr) -const sortResultBetter = shellSortBetter(dealArr) -console.log('简化shellSortBetter希尔排序后:', sortResultBetter) \ No newline at end of file diff --git a/manuscripts/数据结构/code/SqList.cpp b/manuscripts/数据结构/code/SqList.cpp deleted file mode 100644 index 7b4a15f..0000000 --- a/manuscripts/数据结构/code/SqList.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * @Description: 顺序表的基础操作 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-02-23 07:48:26 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-02-23 07:48:26 - */ - - - - -// 基础结构体 -define MaxSize 50; -typedef struct{ - ElemType data[MaxSize]; // ElemType 代表元素类型 int、string..... - int length; -}SqList - - - -bool ListInsert(SqList &L, int i, ElemType e){ - - // i非法 i=1 表头 i=L.length+1 表尾巴 - if(i<1||i>L.length+1){ - return false; - } - - // 存储空间满,无法插入 - if(L.length >= MaxSize){ - return false; - } - - // 遍历,将位置元素往后移动,注意从后往前循环,避免值被覆盖 - for(int j=L.length; j>=i;j--){ - L.data[j]=L.data[j-1]; - } - - // 此时,表L中的第i个元素和第i+1元素素值一样,将新元素存入i位置即可 - - // 第i个元素,对应的位置角标为i-1 - L.data[i-1]=e; - - // 表长度加1 - L.length++; - - // 返回插入成功 - return true; -} - - -bool ListDelete(SqList &L, int i, ElemType &e){ - - // i非法 i=1 表头 i=L.length+1 表尾巴 - if(i<1||i>L.length+1){ - return false; - } - - // 存储空间满,无法插入 - if(L.length >= MaxSize){ - return false; - } - - // 引用变量e赋值 - e=L.data[i-1] - - // 遍历,第i个元素后面的往前移动 - for(int j=i; j<=L.length;j++){ - // 从第i个元素开始,角标从i-1开始 - L.data[j-1]=L.data[j]; - } - - // 此时,表L中的表尾元素和倒数第二个元素值一样,将表的长度-1 - - // 表长度减1 - L.length--; - - // 返回删除成功 - return true; -} \ No newline at end of file diff --git a/manuscripts/数据结构/code/SqStack.cpp b/manuscripts/数据结构/code/SqStack.cpp deleted file mode 100644 index eec974c..0000000 --- a/manuscripts/数据结构/code/SqStack.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * @Description: 顺序栈的相关操作 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2020-03-07 11:15:04 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-03-13 12:30:18 - */ - - -// 定义栈中元素的最大个数 -# define MaxSize 50 - -// 结构体定义 -typedef struct{ - ElemType data[MaxSize]; // 存放栈中元素 - int top; // 栈顶指针 -}SqStack; - - -// 初始化 -void InitStack(&S){ - // 栈顶指针-1 - s.top=-1; -} - -// 栈空判断 -bool StackEmpty(S){ - if(S.top==-1){ - // 栈空 - return true; - }else{ - // 栈非空 - return false; - } -} - -// 进栈 -bool Push(SqStack &S,ElemType x){ - if(S.top==MaxSize-1){ - // 栈满,返回false,元素无法进行进栈操作 - return false; - }else{ - // 可进栈,栈顶指针+1,再元素入栈 - S.data[++S.top]=x; - - // 入栈成功 - return true; - } -} - - -// 出栈 -bool Pop(SqStack &S,ElemType &x){ - if(S.top==-1){ - // 栈空,无栈顶元素可出栈,返回false - return false; - }else{ - // 栈非空,先元素出栈,再进行指针-1 - x=S.data[S.top--]; - - // 出栈成功,返回true - return true; - } -} - -// 读(获取)栈顶元素 -bool GetTop(SqStack S,ElemType &x){ - - if(S.top==-1){ - // 栈空,无栈顶元素,返回false - return false; - }else{ - - // 通过栈顶指针,获取栈顶元素,赋值给变量x - x=S.data[S.top]; - - // 读取栈顶元素成功,返回true - return true; - } -} diff --git a/manuscripts/数据结构/code/StraightInsertSort.cpp b/manuscripts/数据结构/code/StraightInsertSort.cpp deleted file mode 100644 index 1827725..0000000 --- a/manuscripts/数据结构/code/StraightInsertSort.cpp +++ /dev/null @@ -1,26 +0,0 @@ -/* - * @Description: 直接插入排序【伪代码】 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-03-25 08:07:23 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-03-26 07:29:00 - */ -void straightInsertSort(ElemType A[], int n){ - int i,j; - - // 依次将前面的第2到第n个元素插入到前面的有序序列 - for(i=2;i<=n;i++){ - if(A[i].key< A[i-1].key){ - // 哨兵元素 - A[0]=A[i]; - // 循环向后挪动 - for(j=i-1;A[0].keytemp;--j){ - // 后移 - arr[j+1]=arr[j] - } - // 跳出循环逻辑,出现arr[j] > arr[j-1] - - // 哨兵即待排序的 - arr[j+1]=temp - } - } - - return arr - -} - -const dealArr=[5,2,7,3,18,8,12,1] -console.log('插入排序前:',dealArr) -const sortResult=straightInsertSort(dealArr,7) - -console.log('插入排序后:',sortResult) - - diff --git a/manuscripts/算法/DeepClone.js b/manuscripts/算法/DeepClone.js deleted file mode 100644 index 73c833a..0000000 --- a/manuscripts/算法/DeepClone.js +++ /dev/null @@ -1,44 +0,0 @@ - - -// 判断数据类型 -function checkedType(target){ - // 判断类型 -typeof -instanceof -toString.call() -Array.isArray() - return Object.prototype.toString.call(target).slice(8,-1); -} - - -// 基于Json序列化的深拷贝【不能处理函数】 -function DeepCloneByJSON(target){ - return JSON.parse(Json.stringify(target)) -} - -// 基于递归思想的深拷贝 -function DeepClone(target){ - - let result; - const targetType=checkType(target); - - if(targetType === 'object'){ - // 处理对象 - result={}; - }else if(targetType === 'Array'){ - // 处理数组 - result=[]; - }else{ - // 其他数据类型或者函数 - return result; - } - - // 遍历目标数据 - for(let key in target){ - // 获取每一项值 - const value=target[key]; - - // 递归处理 - result[key]=DeepClone(value) - } - - - // 处理完成后,返回 - return result; -} \ No newline at end of file diff --git a/manuscripts/算法/GetNumberOfK.js b/manuscripts/算法/GetNumberOfK.js deleted file mode 100644 index f61642b..0000000 --- a/manuscripts/算法/GetNumberOfK.js +++ /dev/null @@ -1,105 +0,0 @@ -function GetNumberOfK(data, k) { - // write code here - // 分两次二分查找,知道重复元素首次和最后一次出现位置,相减就能拿到重复次数了. - - // 左侧二分查找,重复元素第一次出现的索引位置 - const left = leftBinarySearch(data, k); - - - // 右侧二分查找,重复元素最后一次出现的索引位置 - const right = rightBinarySearch(data, k); - - console.log(`left:${left}----> right:${right}`) - return left === -1 && right === -1 ? 0 : right - left + 1 -} - -function rightBinarySearch(data, target) { - if (!data.length) { - return -1 - } - let left = 0; - let right = data.length - 1; - - while (left <= right) { - - let mid = left + Math.floor((right - left) / 2); - - if (target === data[mid]) { - left = mid + 1 - } else if (target < data[mid]) { - // 左侧 - right = mid - 1 - } else if (target > data[mid]) { - // 右侧; - left = mid + 1; - } - } - - // left = right+1; 判断出界 - if (right < 0 || data[right] !== target) { - return -1 - } - return right -} - - -// [left,right] -function leftBinarySearch(data, target) { - if (!data.length) { - return -1 - } - let left = 0; - let right = data.length - 1; - - while (left <= right) { - - let mid = left + Math.floor((right - left) / 2); - - if (target === data[mid]) { - // 左侧收缩 - right = mid - 1; - } else if (target < data[mid]) { - // 左侧 - right = mid - 1 - } else if (target > data[mid]) { - // 右侧; - left = mid + 1; - } - } - - - - // left = right+1; 判断出界 - if (left > data.length || data[left] !== target) { - return -1 - } - - - return left -} - - - - - - - - - - -console.log(GetNumberOfK([1, 2, 3, 3, 3, 3, 4, 5], 3)) -console.log(GetNumberOfK([3, 3, 3, 3, 4, 5], 3)) -console.log(GetNumberOfK([1, 2, 3, 3, 3, 3], 3)) -console.log(GetNumberOfK([1, 3, 3, 3, 3, 4, 5], 2)) -console.log(GetNumberOfK([1, 3, 3, 3, 3, 4, 5], 0)) -console.log(GetNumberOfK([1, 3, 3, 3, 3, 4, 5], 6)) -console.log(GetNumberOfK([3, 3, 3, 3], 3)) -console.log(GetNumberOfK([3, 3, 3, 3], 4)) -console.log(GetNumberOfK([3], 3)) -console.log(GetNumberOfK([3], 4)) -console.log(GetNumberOfK([1, 3, 3, 3, 3, 4, 5], 2)) - - -module.exports = { - GetNumberOfK: GetNumberOfK -}; \ No newline at end of file diff --git a/manuscripts/算法/LeftRotateString.js b/manuscripts/算法/LeftRotateString.js deleted file mode 100644 index b8b9c5d..0000000 --- a/manuscripts/算法/LeftRotateString.js +++ /dev/null @@ -1,44 +0,0 @@ -// -function LeftRotateString(str, n) { - - if (!str) { - return str; - } - let arr = str.split('') - - const leftReverseArr = reverse(arr, 0, n - 1); - console.log(`left:${leftReverseArr}`) - const rightReverseArr = reverse(arr, n, arr.length - 1) - console.log(`right:${rightReverseArr}`) - - // 翻转后,通过join连接起来 - return reverse(arr, 0, arr.length - 1).join('') -} - - -// 数组中翻转 -function reverse(arr, left, right) { - while (left < right) { - // 元素交换 - arr = swap(arr, left, right) - // left右移 - left++ - // right左移 - right-- - } - return arr; -} - - -// 元素交换 -function swap(arr, left, right) { - - const temp = arr[left]; - arr[left] = arr[right]; - arr[right] = temp - - return arr; -} - - -console.log(LeftRotateString('', 6)) \ No newline at end of file diff --git a/manuscripts/算法/QuickSort.js b/manuscripts/算法/QuickSort.js deleted file mode 100644 index 1ef8677..0000000 --- a/manuscripts/算法/QuickSort.js +++ /dev/null @@ -1,61 +0,0 @@ -/** - * 非递归实现快速排序 - * @param {*} data - * @param {*} low - * @param {*} high - */ -function quickSort(data, low, high) { - console.log(data, low, high) - const pivot = partition(data, low, high); - let result = []; - - if (pivot + 1 < high) { - result.push(pivot + 1, high); - } else if (pivot > low + 1) { - result.push(low, pivot - 1); - } - - - - // 数组不为空 - while (result.length > 0) { - console.log(1, result) - const temp_high = result.pop(); - const temp_low = result.pop(); - if (temp_low >= temp_high) { - break; - } - const temp_pivot = partition(data, temp_low, temp_high); - - if (temp_pivot + 1 < high) { - result.push(temp_pivot + 1, temp_high) - } else if (temp_pivot - 1 > low) { - result.push(temp_low, temp_pivot - 1); - } - - } - - return data; - -} - - -// 严版获取快排pivot -function partition(data, low, high) { - - let pivot = data[low]; - - while (low < high) { - // 高位 - while (low < high && pivot <= data[high]) --high; - data[low] = data[high] - // 低位 - while (low < high && pivot >= data[low]) ++low; - data[high] = data[low] - } - data[low] = pivot - - return low -} - -console.log(quickSort([1, 8, 9, 2], 0, 3)) \ No newline at end of file diff --git a/manuscripts/算法/debounce.js b/manuscripts/算法/debounce.js deleted file mode 100644 index 8cd054e..0000000 --- a/manuscripts/算法/debounce.js +++ /dev/null @@ -1,24 +0,0 @@ - - -function debounce(func,time){ - let timeout - return ()=>{ - clearTimeout(timeout) - timeout=setTimeout(()=>{ - func.apply(this,arguments) - // func() - },time) - } -} - -function test(){ - console.log(new Date().getTime()) -} - -const debounce_test= debounce(test,1000) -debounce_test() - -debounce_test() -// setTimeout(()=>{ -// dou(test,1000) -// },1500) \ No newline at end of file diff --git a/manuscripts/算法/find.js b/manuscripts/算法/find.js deleted file mode 100644 index f22ac13..0000000 --- a/manuscripts/算法/find.js +++ /dev/null @@ -1,56 +0,0 @@ -function Find(target, array) { - // write code here - if (!array) { - return false; - } - - // 行 - const rows = array.length; - // 列 - const cols = array[0].length; - console.log(rows, cols) - if (rows === 0 || cols === 0) { - return false; - } - let r = 0, - c = cols - 1; - - while (r < rows && c >= 0) { - - if (array[r][c] === target) { - return true; - } else if (array[r][c] < target) { - // 比目标值小,往下 - r++ - } else if (array[r][c] > target) { - // 比目标值大 往左 - c-- - } - } - - return false; - -} - - -console.log(Find(7, [ - [1, 2, 8, 9], - [2, 4, 9, 12], - [4, 7, 10, 13], - [6, 8, 11, 15] -])) - -// 投机方案 -function Find01() { - const len = array.length; - let result = [] - for (let index = 0; index < len; index++) { - result = result.concat(array[index]); - } - console.log(result, result.indexOf(target)); - // 查询 - return result.indexOf(target) !== -1 -} -module.exports = { - Find: Find -}; \ No newline at end of file diff --git a/manuscripts/算法/global_variables.js b/manuscripts/算法/global_variables.js deleted file mode 100644 index 78f975c..0000000 --- a/manuscripts/算法/global_variables.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict'; - -global.config = require('config'); -global.fse = require('fs-extra'); -global.fs = require('fs'); -global.path = require('path'); -global.errorHandler = require('./middleware/errorHandler').errorHandler; -// global.utils = require('./tools/utils'); - -global.mainlogger = require('./logger/logger').mainlogger; -global.weblogger = require('./logger/logger').weblogger; - -global.strCache = require('./cache').caches.strCache; - -global.hashCache = require('./cache').caches.hashCache; -global.helper = require('./controller/helper'); - -global.requests = require('./tools/request'); - - - - diff --git a/manuscripts/算法/isUSD.js b/manuscripts/算法/isUSD.js deleted file mode 100644 index dda9aa0..0000000 --- a/manuscripts/算法/isUSD.js +++ /dev/null @@ -1,43 +0,0 @@ -const { - lstat -} = require("fs-extra"); - -function isUSD(str) { - if (!str.startsWith("$")) { - return false; - } - str = str.slice(1); - if (str.indexOf(".") !== -1) { - // 小数 - let arr = str.split("."); - if (arr[1].length !== 2) { - return false; - } - - let strArr = arr[0].split(","); - if (strArr[0].length > 3) { - return false; - } - for (let index = 1; index < strArr.length; index++) { - if (strArr[index].length !== 3) { - return false; - } - } - return true; - } - - let strArr = str.split(","); - if (strArr[0].length > 3) { - return false; - } - for (let index = 1; index < strArr.length; index++) { - if (strArr[index].length !== 3) { - return false; - } - } - - return true; - lstat -} - -console.log(isUSD("$,344.34")); \ No newline at end of file diff --git a/manuscripts/算法/merge.js b/manuscripts/算法/merge.js deleted file mode 100644 index 4656524..0000000 --- a/manuscripts/算法/merge.js +++ /dev/null @@ -1,40 +0,0 @@ -/** - * - * @param A int整型一维数组 - * @param B int整型一维数组 - * @return void - */ -function merge(A, m, B, n) { - // write code here - - let pre = 0, - next = 0; - let result = [] - while (pre < m && next < n) { - if (A[pre] < B[next]) { - result.push(A[pre]) - pre++ - } else if (A[pre] >= B[next]) { - result.push(B[next]) - next++ - } - } - console.log(result, pre, next) - // A的元素都遍历完了,B没有遍历完,next0;j--){ - // if(arr[j-1]>arr[j]){ - // const temp=arr[j] - // arr[j]=arr[j-1] - // arr[j-1]=temp; - // } - // } - - // 插入排序 - const temp=arr[i]; - let j; - // 哨兵元素小于前面的倒叙元素 - for(j=i-1;arr[j]>temp;--j){ - arr[j+1]=arr[j] - } - arr[j+1]=temp - // console.log(arr) - } - } - - return arr; -} - -const straightInsertSortResult= straightInsertSort([3,16,5,7,1,9,40,18,24],9) -console.log(straightInsertSort([3,16,5,7,1],5)) - -console.log(straightInsertSortResult) - diff --git a/manuscripts/算法/test.js b/manuscripts/算法/test.js deleted file mode 100644 index 8cd054e..0000000 --- a/manuscripts/算法/test.js +++ /dev/null @@ -1,24 +0,0 @@ - - -function debounce(func,time){ - let timeout - return ()=>{ - clearTimeout(timeout) - timeout=setTimeout(()=>{ - func.apply(this,arguments) - // func() - },time) - } -} - -function test(){ - console.log(new Date().getTime()) -} - -const debounce_test= debounce(test,1000) -debounce_test() - -debounce_test() -// setTimeout(()=>{ -// dou(test,1000) -// },1500) \ No newline at end of file diff --git a/manuscripts/算法/twoNums.js b/manuscripts/算法/twoNums.js deleted file mode 100644 index 0af463c..0000000 --- a/manuscripts/算法/twoNums.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * 给出一个整数数组, 请在数组中找出两个加起来等于目标值的数, - 你给出的函数twoSum 需要返回这两个数字的下标( index1, index2), 需要满足 index1 小于index2.。注意: 下标是从1开始的 - 假设给出的数组中只存在唯一解 - */ - -function twoSum(numbers, target) { - // write code here - for (let i = 0; i = numbers.length - 1; i++) { - const num = numbers[i]; - const value = target - num - if (numbers.indexOf(target - num, i)) { - return [i + 1, numbers.indexOf(target - num) + 1] - } - } -} -module.exports = { - twoSum: twoSum -}; - - -function captureThreeNumbers(str) { - // 切割转化 - const arr = str.split(''); - console.log(arr) - const num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] - - for (let index = 0; index < arr.length - 1; index++) { - if (num.includes(arr[index]) && num.includes(arr[index + 1]) && num.includes(arr[index + 2])) { - return parseInt(`${arr[index]}${arr[index+1]}${arr[index+2]}`) - } - } - - return false; -} - - -console.log(captureThreeNumbers('abc123')) - - -function matchesPattern(str) { - - - if (str.length !== 12) { - return false; - } - // console.log(str.slice(2, 3), str.slice(6, 7)) - if (str.slice(3, 4) !== '-' || str.slice(7, 8) !== '-') { - return false; - } - - const arr = str.split('-'); - console.log(arr) - if (arr.length !== 3) { - return false; - } - - const [low, mid, high] = arr; - - const rule = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; - - const lowArr = low.split(''); - const midArr = mid.split(''); - const highArr = high.split(''); - - console.log(lowArr, midArr, highArr) - - // 逐一比对 - - for (let index = 0; index < lowArr.length; index++) { - if (!rule.includes(lowArr[index])) { - return false; - } - } - - for (let index = 0; index < midArr.length; index++) { - if (!rule.includes(midArr[index])) { - return false; - } - } - - for (let index = 0; index < highArr.length; index++) { - if (!rule.includes(highArr[index])) { - return false; - } - } - - return true; -} - -console.log(matchesPattern('800-555-1212')) \ No newline at end of file diff --git a/manuscripts/算法/前端/Readme.md b/manuscripts/算法/前端/Readme.md deleted file mode 100644 index c9cbc6a..0000000 --- a/manuscripts/算法/前端/Readme.md +++ /dev/null @@ -1,102 +0,0 @@ - - - -## 前端算法刷题 - -> 相比后端涉及到的算法,前端的算法较为基础,常常是用来解决某个场景的问题 - -- 刷题平台: [牛客网](https://www.nowcoder.com/ta/front-end) -- 语言版本:JavaScript - - - -### 入门 - - -- dom 节点查找 -- 根据包名,在指定空间中创建对象 -- 斐波那契数列 -- 字符串字符统计 -- 数组求和 -- 删除数组最后一个元素 -- 添加元素 -- 删除数组第一个元素 -- 数组合并 -- 计数 -- 求二次方 -- 查找元素位置 -- 避免全局变量 -- 正确的使用 -- 完全等同 -- 函数传参 -- 函数的上下文 -- 二次封装函数 -- 使用 arguments -- 柯里化 -- 或运算 -- 且运算 -- 二进制转换 -- 乘法 -- 改变上下文 -- 批量改变对象的属性 -- 判断是否包含数字 -- 判断是否以元音字母结尾 - - -### 简单 - -- 获取字符串的长度 -- 段落标识 -- 查找数组元素位置 -- 移除数组中的元素 -- 添加元素 -- 添加元素 -- 正确的函数定义 -- 返回函数 -- 使用 apply 调用函数 -- 二次封装函数 -- 二进制转换 -- 二进制转换 -- 属性遍历 -- 检查重复字符串 -- 获取指定字符串 -- 判断是否符合指定格式 - - - -### 中等 - -- 修改 this 指向 -- 时间格式化输出 -- 邮箱字符串判断 -- 颜色字符串转换 -- 将字符串转换为驼峰格式 -- 加粗文字 -- 移除数组中的元素 -- 查找重复元素 -- 计时器 -- 流程控制 -- 使用闭包 -- 判断是否符合 USD 格式 - - -### 较难 - -- 获取 url 参数 -- 数组去重 -- 设置文字颜色 -- 模块 - - - -### 困难 - -在牛客网题库——前端大挑战中暂时没有标记为`困难`的题目,个人觉得按照常用的场景,把前面的几个分类刷完就ok了,没事多刷刷呗 - diff --git a/manuscripts/算法/前端/code/add.js b/manuscripts/算法/前端/code/add.js deleted file mode 100644 index 60b11b8..0000000 --- a/manuscripts/算法/前端/code/add.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-23 07:50:21 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-23 22:05:34 - */ - - - -function add(){ - console.log(arguments); - var args=Array.prototype.slice.call(arguments) - console.log(args) - - - var _add=function(){ - console.log('add',arguments) - args.push(...arguments); - - // 返回函数 - return _add; - } - - console.log(args) - - // 对参数数组做求和处理 - - _add.toString=function(){ - // 设置sum的起始值为0 - return args.reduce((sum,item)=>{ - console.log(sum,item) - return sum+item; - }) - } - // 返回函数 - return _add -} - -let str=add(1,6)(2)(3) -console.log(str) -// console.log(String(add(1,6)(2)(3)) ) -// console.log(add(1)(2)(3)) -// console.log(add(1)(2,3,4)) - diff --git a/manuscripts/算法/前端/code/count.js b/manuscripts/算法/前端/code/count.js deleted file mode 100644 index 130b82d..0000000 --- a/manuscripts/算法/前端/code/count.js +++ /dev/null @@ -1,31 +0,0 @@ -/* - * @Description: 字符串中字符出现频率计数 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-14 10:21:39 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-14 10:22:19 - */ - - -function count(str) { - // 转换为数组后去重 - const originArr=str.split('') - const arr=[...new Set(originArr)]; - let result={}; - for(let index=0;index{ - if(item===value){ - count++ - } - }) - // 对象计数 - result[value]=count - } - - } - return result; -} \ No newline at end of file diff --git a/manuscripts/算法/前端/code/duplicates.js b/manuscripts/算法/前端/code/duplicates.js deleted file mode 100644 index c6a6700..0000000 --- a/manuscripts/算法/前端/code/duplicates.js +++ /dev/null @@ -1,30 +0,0 @@ -/* - * @Description: 找出数组 arr 中重复出现过的元素 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-14 10:22:51 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-14 10:22:51 - */ - - -// 找出数组 arr 中重复出现过的元素 -function duplicates(arr) { - const sortArr=arr.sort(); - - let result=new Array() - - const len=sortArr.length; - - for(let index=0;indexvalue!==item) - // // 输出 - // return result; - - // 每次都和arr中的首个元素去比较 - - const len=arr.length; - - for(let index=0;index 0) { - return result.pop(); - } -} -function top () { - // write code here - if (result.length > 0) { - return result[result.length - 1] - } -} - - -// 对数组排序 -function min () { - // write code here - // 对result数组进行排序 - return Math.min(...result) -} -module.exports = { - push: push, - pop: pop, - top: top, - min: min -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/JZ21_IsPopOrder.js b/manuscripts/算法/剑指/JZ21_IsPopOrder.js deleted file mode 100644 index 89986fc..0000000 --- a/manuscripts/算法/剑指/JZ21_IsPopOrder.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-23 22:56:17 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-23 22:57:59 - */ - - -// 明确一点,栈是后进先出,或者说是先进后出 -function IsPopOrder (pushV, popV) { - // write code here -} -module.exports = { - IsPopOrder: IsPopOrder -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/JZ27_Permutation.js b/manuscripts/算法/剑指/JZ27_Permutation.js deleted file mode 100644 index 1697b30..0000000 --- a/manuscripts/算法/剑指/JZ27_Permutation.js +++ /dev/null @@ -1,25 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-23 23:02:58 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-23 23:03:10 - */ - -function Permutation (str) { - // write code here - - let result=[]; - let i=0; - while(i max ? sum : max; - - // 注意,重新计数 - sum = sum > 0 ? sum : 0 - index++ - } - return max; - -} - - -function FindGreatestSumOfSubArray (array) { - // write code here - - // 首位指针 - let i = 0; - // 从第一个元素开始,假设最大 - let max = array[0] - let sum = 0; - - while (i < array.length) { - // 替换 - - // 和下一个元素求和 - sum += array[i]; - // 获取最大值 - max = sum > max ? sum : max; - - // 小于sum值,则说明累加和变小了,下一个模块重新计数 - sum = sum < 0 ? 0 : sum; - - - // 标记指针后移 - i++ - } - - return max; - - -} -console.log(FindGreatestSumOfSubArray01([1, -2, 3, 10, -4, 7, 2, -5])) -console.log(FindGreatestSumOfSubArray([-2, -8, -1, -5, -9])) - - -module.exports = { - FindGreatestSumOfSubArray: FindGreatestSumOfSubArray -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/JZ40_FindNumsAppearOnce.js b/manuscripts/算法/剑指/JZ40_FindNumsAppearOnce.js deleted file mode 100644 index 0664e81..0000000 --- a/manuscripts/算法/剑指/JZ40_FindNumsAppearOnce.js +++ /dev/null @@ -1,95 +0,0 @@ -/* - * @Description: 除了先排序,也是利用对象进行标记处理的; - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-24 18:19:20 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-24 18:52:58 - */ -/** - * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 - * - * - * @param array int整型一维数组 - * @return int整型一维数组 - */ -function FindNumsAppearOnce (array) { - // write code here - - // 数组中元素要么出现一次,要么出现两次,可以先对元素进行排序 Math.sort((a,b)=>a-b) 从小到大 有点偷懒的样子 - - - array=quickSort(array, 0, array.length - 1); - - - // 此时的 数组已经进过排序 - - - let result=[] - // 出现一次的元素,在中间 - for(let index=1;indexa-b) - -} - - - -function quickSort (arr, left, right) { - if (left < right) { - const pivot = partition(arr, left, right); - // 左侧 - quickSort(arr, left, pivot - 1) - - // 右侧 - quickSort(arr, pivot + 1, right) - } - - - return arr; -} - -function partition (arr, left, right) { - - - let pivot = arr[left]; - while (left < right) { - - while (left < right && pivot <= arr[right]) --right; - arr[left] = arr[right]; - - while (left < right && pivot >= arr[left]) ++left; - arr[right] = arr[left] - } - - arr[left] = pivot; - - return left - - - -} - -console.log(FindNumsAppearOnce([1, 4, 1, 6, 7])) - -module.exports = { - FindNumsAppearOnce: FindNumsAppearOnce -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/JZ51_multiply.js b/manuscripts/算法/剑指/JZ51_multiply.js deleted file mode 100644 index c2f2342..0000000 --- a/manuscripts/算法/剑指/JZ51_multiply.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-24 11:51:20 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-24 12:02:23 - */ - - -function multiply (array) { - let result = []; - - for (let index = 0; index < array.length; index++) { - // result.push(array.slice(0, index).reduce((res, item) => res * item, 1) * array.slice(index + 1).reduce((res, item) => res * item, 1)) - result.push([...array.slice(0, index), ...array.slice(index + 1)].reduce((res, item) => { - return res * item; - },1)) // 给res的初始值为1 - } - return result; -} - -console.log(multiply([1, 2, 3, 4, 5])) \ No newline at end of file diff --git a/manuscripts/算法/剑指/JZ6_minNumberInRotateArray.js b/manuscripts/算法/剑指/JZ6_minNumberInRotateArray.js deleted file mode 100644 index 100524e..0000000 --- a/manuscripts/算法/剑指/JZ6_minNumberInRotateArray.js +++ /dev/null @@ -1,44 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-24 17:53:01 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-24 18:14:24 - */ - - -function minNumberInRotateArray (rotateArray) { - // write code here - - const len=rotateArray.length; - if (rotateArray.length < 0) { - return 0 - } - - // 重新整理思路 旋转数组: 非递减 基于分治的思路,对数组进行切割; - - let index=0; - // 直接二分查找 - while(indexarr[index+1]){ - return true;; - } - } - return false; - -} - - -console.log(minNumberInRotateArray([3,4,5,1,2])) -module.exports = { - minNumberInRotateArray: minNumberInRotateArray -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/Power.js b/manuscripts/算法/剑指/Power.js deleted file mode 100644 index 793fcb4..0000000 --- a/manuscripts/算法/剑指/Power.js +++ /dev/null @@ -1,48 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-23 22:14:37 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-23 22:23:46 - */ - -// -/** - * Math.power(base,exponent) - * 保证base和exponent不同时为0。不得使用库函数,同时不需要考虑大数问题,也不用考虑小数点后面0的位数。 - * @param {double} base - * @param {int} exponent - */ -function Power (base, exponent) { - // write code here - - let result = 1; - - if (exponent > 0) { - // 正数 - while (exponent > 0) { - result *= base; - exponent-- - } - return result; - } else if (exponent < 0) { - // 负数 取反 绝对值 abs - // let abs=Math.abs(exponent) - let abs = -exponent; - while (abs > 0) { - result *= base; - abx-- - } - return 1 / result; - - } else { - // 0 返回1 - return result; - } -} - -console.log(Power(2.10000, 3)) -module.exports = { - Power: Power -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/Readme.md b/manuscripts/算法/剑指/Readme.md deleted file mode 100644 index 009cae2..0000000 --- a/manuscripts/算法/剑指/Readme.md +++ /dev/null @@ -1,138 +0,0 @@ - - - -## 剑指Offer算法 - -> 刷题平台:牛客网 -> 语言版本:JavaScript - -### 数组和矩阵 - - - [【简单】数组中重复的数字](./数组和矩阵/duplicate.js) - - [【中等】二维数组中的查找](./数组和矩阵/Find.js) - - [【较难】替换空格](./数组和矩阵/replaceSpace.js) - - [【较难】顺时针打印矩阵](./数组和矩阵/printMatrix.js) - - [【简单】第一个只出现一次的字符位置](./数组和矩阵/FirstNotRepeatingChar.js) - - -### 栈队列堆 - -- [【简单】两个栈实现队列](./栈队列堆/JSStackToQueue.js) -- [【中等】最小的k个数](./栈队列堆/GetLeastNumbers_Solution.js) -- [【中等】数据流中的中位数](./栈队列堆/InsertAndGetMedian.js) -- [【中等】字符流中的第一个不重复的字符](./栈队列堆/FirstAppearingOnce.js) -- [【较难】滑动窗口的最大值](./栈队列堆/maxInWindows.js) -- [【较难】包含min函数的栈](./栈队列堆/GetMinInJSStack.js) -- 栈的压入、弹出序列 - - - -### 双指针 - -- [【中等】和为S的两个数字](./双指针/FindNumbersWithSum.js) -- [【中等】和为S的连续正数序列](./双指针/FindContinuousSequence.js) -- [【中等】左旋转字符串](./双指针/LeftRotateString.js) -- [【较难】翻转单词顺序列](./双指针/ReverseSentence.js) - - - -### 链表 - -- [【简单】合并两个排序的链表](./链表/Merge.js) -- [【简单】两个链表的第一个公共结点](./链表/FindFirstCommonNode.js) -- [【中等】链表中倒数第K个结点](./链表/FindKthToTail.js) -- [【中等】反转链表](./链表/ReverseList.js) -- [【较难】从尾到头打印链表](./链表/printListFromTailToHead.js) -- ~~在O(1)时间内删除链表节点~~ -- [【较难】删除链表中重复的结点](./链表/deleteDuplication.js) -- 链表中环的入口结点[暂时没思路] -- 【较难】复杂链表的复制[暂时没有思路] - - - -### 树 - -- [【中等】重建二叉树](./树/reConstructBinaryTree.js) -- [【中等】二叉树的下一个结点](./树/GetNext.js) -- [【较难】树的子结构](./树/HasSubtree.js) -- [【简单】二叉树的镜像](./树/Mirror.js) -- [【困难】对称的二叉树](./树/isSymmetrical.js) -- 从上往下打印二叉树 -- 把二叉树打印成多行 -- 二叉搜索树的后续遍历序列 -- 【较难】二叉树中和为某一值的路径 -- 二叉搜索树和双向链表 -- 序列化二叉树 -- 二叉查找树的第K个结点 -- 二叉树的深度 -- 平衡二叉树 -- 树中两个节点的最低公共祖先 - - -### 综合类型 - -#### 贪心思想 - -- 剪绳子 -- [【LeetCode题目】股票的最大利润](./贪心思想/maxProfit.js) - - -#### 二分查找 - -- [【简单】旋转数组的最小数字](./二分查找/minNumberInRotateArray.js) -- [【中等】数字在排序数组中出现的次数](./二分查找/GetNumberOfK.js) - - -#### 分治 - -- [【中等】数值的整数次方](./分治/Power.js) - - -#### 搜索 - -- 矩阵中的路径 有难度 -- 机器人的运动范围 -- 字符串的排列 - - -#### 排列 - -- [【中等】调整数组顺序使奇数位于偶数前面](./排列/reOrderArray.js) -- [【较难】把数组排成最小的数](./双指针/ReverseSentence.js) -- 数组中的逆序对 困难 - -#### 动态规划 - -- [【入门】斐波拉契数列](./动态规划/Fibonacci.js) -- [【简单】变态跳台阶 找规律 可跳任意阶](./动态规划/jumpFloorII.js) -- [【简单】连续子数组的最大和 时间复杂度O(n)](./动态规划/FindGreatestSumOfSubArray.js) -- [【简单】构建乘积数组](./动态规划/multiply.js) -- [【中等】矩形覆盖](./动态规划/rectCover.js) -- [【中等】跳台阶 非递归,要么跳一阶,要么跳两阶](./动态规划/jumpFloor.js) -- [【较难】丑数](./动态规划/GetUglyNumber_Solution.js) - - - -#### 数学问题 - -- [【简单】数组中出现次数超过一半的数字](./数学/MoreThanHalfNum_Solution.js) -- [【中等】圆圈中最后剩下的数 约瑟夫问题](./数学/LastRemaining_Solution.js) -- [【中等】从1到n整数中1出现的次数](./数学/NumberOf1Between1AndN_Solution.js) - -#### 位运算 - -- [【中等】二进制中1的个数](./位运算/NumberOf1.js) -- [【中等】数组中只出现一次的数字]() - -#### 其他分类 - -- [【简单】不用加减乘除做加法](./其他相关/Add.js) -- [【中等】扑克牌顺子](./其他相关/IsContinuous.js) -- [【较难】把字符串转换成整数](./其他相关/StrToInt.js) \ No newline at end of file diff --git a/manuscripts/算法/剑指/reOrderArray.js b/manuscripts/算法/剑指/reOrderArray.js deleted file mode 100644 index d9a0846..0000000 --- a/manuscripts/算法/剑指/reOrderArray.js +++ /dev/null @@ -1,50 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-23 22:26:26 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-23 22:40:59 - */ - - -// odd 奇数 even: 偶数 - -/** - * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 - * - * - * @param array int整型一维数组 - * @return int整型一维数组 - */ -/** - * - * 借用临时变量 遍历 【可通过测试】 - * @param {Array} array - * @returns - */ -function reOrderArray( array ) { - // write code here - - // 奇数,偶数; - let odd_arr=[],even_arr=[]; - for(let index=0;index right:${right}`) - return left === -1 && right === -1 ? 0 : right - left + 1 -} - -function rightBinarySearch (data, target) { - if (!data.length) { - return -1 - } - let left = 0; - let right = data.length - 1; - - while (left <= right) { - - let mid = left + Math.floor((right - left) / 2); - - if (target === data[mid]) { - left = mid + 1 - } else if (target < data[mid]) { - // 左侧 - right = mid - 1 - } else if (target > data[mid]) { - // 右侧; - left = mid + 1; - } - } - - // left = right+1; 判断出界 - if (right < 0 || data[right] !== target) { - return -1 - } - return right -} - - -// [left,right] -function leftBinarySearch (data, target) { - if (!data.length) { - return -1 - } - let left = 0; - let right = data.length - 1; - - while (left <= right) { - - let mid = left + Math.floor((right - left) / 2); - - if (target === data[mid]) { - // 左侧收缩 - right = mid - 1; - } else if (target < data[mid]) { - // 左侧 - right = mid - 1 - } else if (target > data[mid]) { - // 右侧; - left = mid + 1; - } - } - - - - // left = right+1; 判断出界 - if (left > data.length || data[left] !== target) { - return -1 - } - return left -} - -module.exports = { - GetNumberOfK: GetNumberOfK -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/二分查找/minNumberInRotateArray.js b/manuscripts/算法/剑指/二分查找/minNumberInRotateArray.js deleted file mode 100644 index 135c099..0000000 --- a/manuscripts/算法/剑指/二分查找/minNumberInRotateArray.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - * @Description: 【简单】旋转数组的最小数字 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-03 15:12:26 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-03 15:31:34 - */ - - -// 找出前者大于后者的数,立即返回 -function minNumberInRotateArray (rotateArray) { - // write code here - - if (rotateArray.length < 0) { - return 0 - } - - for (let index = 0; index < rotateArray.length - 1; index++) { - if (rotateArray[index] > rotateArray[index + 1]) { - return rotateArray[index + 1] - } - } - - // 返回 - return rotateArray[0] -} - -// 考虑二分法 -function minNumberInRotateArray01(array){ - -} -module.exports = { - minNumberInRotateArray: minNumberInRotateArray -}; diff --git a/manuscripts/算法/剑指/位运算/FindNumsAppearOnce.js b/manuscripts/算法/剑指/位运算/FindNumsAppearOnce.js deleted file mode 100644 index 4d6101f..0000000 --- a/manuscripts/算法/剑指/位运算/FindNumsAppearOnce.js +++ /dev/null @@ -1,87 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-05 23:31:16 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 23:31:26 - */ - -function FindNumsAppearOnce (array) { - // write code here - - // 数组中元素要么出现一次,要么出现两次,可以先对元素进行排序 Math.sort((a,b)=>a-b) 从小到大 有点偷懒的样子 - - - array = quickSort(array, 0, array.length - 1); - - - // 此时的 数组已经进过排序 - - - let result = [] - // 出现一次的元素,在中间 - for (let index = 1; index < array.length - 1; index++) { - - if (array[index - 1] !== array[index] && array[index] !== array[index + 1]) { - result.push(array[index]) - } - - } - - // 假设在开头 - if (array[0] !== array[1]) { - result.push(array[0]) - } - - // 假设在结尾 - - if (array[array.length - 1] !== array[array.length - 2]) { - result.push(array[array.length - 1]) - } - - // 过滤掉只出现一次的元素, 进行排序从小到大; - return result.sort((a, b) => a - b) - -} - - - -function quickSort (arr, left, right) { - if (left < right) { - const pivot = partition(arr, left, right); - // 左侧 - quickSort(arr, left, pivot - 1) - - // 右侧 - quickSort(arr, pivot + 1, right) - } - - - return arr; -} - -function partition (arr, left, right) { - - - let pivot = arr[left]; - while (left < right) { - - while (left < right && pivot <= arr[right]) --right; - arr[left] = arr[right]; - - while (left < right && pivot >= arr[left]) ++left; - arr[right] = arr[left] - } - - arr[left] = pivot; - - return left - - - -} - -module.exports = { - FindNumsAppearOnce: FindNumsAppearOnce -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/位运算/NumberOf1.js b/manuscripts/算法/剑指/位运算/NumberOf1.js deleted file mode 100644 index fb4f38e..0000000 --- a/manuscripts/算法/剑指/位运算/NumberOf1.js +++ /dev/null @@ -1,30 +0,0 @@ -/* - * @Description: 【中等】二进制中1的个数 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-09 11:27:14 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-09 11:40:28 - */ - -function NumberOf1(n){ - - // 记录1的个数 - let count=0; - - // 循环验证 - while(n!==0){ - //加+1 记录 - count++; - // 取与 - n=n & (n-1) - - } - - return count; -} - -// 当然也是可以转化为字符串,进行校验 -module.export={ - NumberOf1:NumberOf1 -} \ No newline at end of file diff --git a/manuscripts/算法/剑指/其他相关/Add.js b/manuscripts/算法/剑指/其他相关/Add.js deleted file mode 100644 index 3c13c78..0000000 --- a/manuscripts/算法/剑指/其他相关/Add.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - * @Description: 【简单】不用加减乘除做加法 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-05 21:29:19 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 23:25:36 - */ - -function Add (num1, num2) { - // write code here - let count = num1; - - if (num2 > 0) { - for (let index = 0; index < num2; index++) { - count++ - } - return count; - } else if (num2 < 0) { - let temp = Math.abs(num2); - for (let index = 0; index < temp; index++) { - count-- - } - return count; - } else { - return count; - } - -} -module.exports = { - Add: Add -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/其他相关/IsContinuous.js b/manuscripts/算法/剑指/其他相关/IsContinuous.js deleted file mode 100644 index 8a806ac..0000000 --- a/manuscripts/算法/剑指/其他相关/IsContinuous.js +++ /dev/null @@ -1,52 +0,0 @@ -/* - * @Description: 【中等】扑克牌顺子 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-05 21:26:28 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 21:27:09 - */ - - -function IsContinuous (numbers) { - // write code here - // 先对数据进行从小到大排列 - - numbers = numbers.sort((a, b) => a - b); - - // 判断0的个数 - let count = 0; - for (let index = 0; index < numbers.length; index++) { - if (numbers[index] === 0) { - count++; - } - } - // 不允许5个0 - if (count > 4) { - return false; - } - - // 4个0的时候,一定成立 - if (count === 4) { - return true; - } - - // 判断每个的间隔 - for (let index = 0; index < numbers.length - 1; index++) { - if (numbers[index] !== 0 && numbers[index + 1] - numbers[index] > 1) { - // console.log(count,(numbers[index+1]-numbers[index])) - count -= (numbers[index + 1] - numbers[index] - 1) - // console.log(count) - } - } - - if (count === 0) { - return true; - } - - return false; - -} -module.exports = { - IsContinuous: IsContinuous -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/其他相关/StrToInt.js b/manuscripts/算法/剑指/其他相关/StrToInt.js deleted file mode 100644 index 745ad2d..0000000 --- a/manuscripts/算法/剑指/其他相关/StrToInt.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * @Description: 【较难】把字符串转换成整数 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-05 21:40:03 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 23:28:22 - */ - -function StrToInt (str) { - // write code here - if (!str) return 0 - let bool = false, firstLetter = str[0] - if (firstLetter == '-') bool = true - if (firstLetter == '-' || firstLetter == '+') str = str.substring(1) - const n = Number(str) - if (n === 0) return 0 - return n !== n ? 0 : bool ? -n : n -} -// console.log(StrToInt(-123)) -module.exports = { - StrToInt: StrToInt -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/分治/Power.js b/manuscripts/算法/剑指/分治/Power.js deleted file mode 100644 index 0931321..0000000 --- a/manuscripts/算法/剑指/分治/Power.js +++ /dev/null @@ -1,38 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-03 15:43:06 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-03 15:44:38 - */ -function Power (base, exponent) { - // write code here - let result = 1; - - if (exponent > 0) { - // 正数 - while (exponent > 0) { - result *= base; - exponent-- - } - return result; - } else if (exponent < 0) { - // 负数 取反 绝对值 abs - // let abs=Math.abs(exponent) - let abs = -exponent; - while (abs > 0) { - result *= base; - abs-- - } - // 分数处理 - return 1 / result; - - } else { - // 0 返回1 - return result; - } -} -module.exports = { - Power: Power -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/动态规划/Fibonacci.js b/manuscripts/算法/剑指/动态规划/Fibonacci.js deleted file mode 100644 index d4e6ca2..0000000 --- a/manuscripts/算法/剑指/动态规划/Fibonacci.js +++ /dev/null @@ -1,41 +0,0 @@ -/* - * @Description: 【入门】斐波拉契数列 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-05 11:00:05 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 11:10:56 - */ - -function Fibonacci (n) { - // write code here - // 递归调用 - return n < 2 ? n : Fibonacci(n - 1) + Fibonacci(n - 2) -} - - -// 非递归实现 -function Fibonacci01 (n) { - - let a = 0, b = 1; - - if (n < 2) { - return n; - } - let result = 0 - for (let index = 2; index <= n; index++) { - // 求和 - result = a + b; - a = b; - b = result; - } - // 返回结果 - return result; -} - - - - -module.exports = { - Fibonacci: Fibonacci01 -}; diff --git a/manuscripts/算法/剑指/动态规划/FindGreatestSumOfSubArray.js b/manuscripts/算法/剑指/动态规划/FindGreatestSumOfSubArray.js deleted file mode 100644 index a52fc51..0000000 --- a/manuscripts/算法/剑指/动态规划/FindGreatestSumOfSubArray.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - * @Description: 【简单】连续子数组的最大和 时间复杂度O(n) - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-05 11:34:21 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 11:45:11 - */ - -function FindGreatestSumOfSubArray (array) { - - // 标记指针 - let index = 0; - // 子串的累计结果 - let sum = 0; - // 子串的累加和最大值,当标记位从 - let max = array[index]; - - - // 循环 - while (index < array.length) { - sum += array[index]; - max = sum > max ? sum : max; - - // 注意,重新计数,累计求和,从0开始 - sum = sum > 0 ? sum : 0 - index++ - } - return max; - -} - -module.exports = { - FindGreatestSumOfSubArray: FindGreatestSumOfSubArray -}; diff --git a/manuscripts/算法/剑指/动态规划/GetUglyNumber_Solution.js b/manuscripts/算法/剑指/动态规划/GetUglyNumber_Solution.js deleted file mode 100644 index d9e71c4..0000000 --- a/manuscripts/算法/剑指/动态规划/GetUglyNumber_Solution.js +++ /dev/null @@ -1,47 +0,0 @@ -/* - * @Description: 【较难】丑数 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-06 21:33:38 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-08 23:53:19 - */ - - -function GetUglyNumber_Solution (index) { - - // 由于丑数只包含因子 2 3 5 且,下一个丑数是通过上一个丑数,去x2 x3 或 x5 取最小得到的,要保证丑数的顺序递增的 - - // 定义存放丑数的数组,且第一个丑数为1=2^0 * 3^0 * 5^0 - let result=[1]; - // 定义丑数对应的因子个数 即 丑数= 2^f2 * 3^f3 * 5^f5 - let f2=0,f3=0,f5=0; - - // 基于丑数的上下关系,可以去动态规划来做 - - for(let i=1;i res * item, 1) * array.slice(index + 1).reduce((res, item) => res * item, 1)) - result.push([...array.slice(0, index), ...array.slice(index + 1)].reduce((res, item) => { - return res * item; - },1)) // 给res的初始值为1 - } - return result; -} - -console.log(multiply([1, 2, 3, 4, 5])) \ No newline at end of file diff --git a/manuscripts/算法/剑指/动态规划/rectCover.js b/manuscripts/算法/剑指/动态规划/rectCover.js deleted file mode 100644 index d3d531f..0000000 --- a/manuscripts/算法/剑指/动态规划/rectCover.js +++ /dev/null @@ -1,36 +0,0 @@ -/* - * @Description: 【中等】矩形覆盖 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-05 11:17:02 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 11:18:08 - */ - -function rectCover (number) { - // write code here - // 应该也是个递归 斐波拉契变形 - // n=1 ---> 1 - // n=2 ---> 2 - // n=3 ---> 3 <---- 1+2 - // n=4 ---> 5 <---- 2+3 - - let a = 1; // n=1的情况 - let b = 2; // n=2的情况 - - if (number < 3) { - return number; - } - - let result = 0; - for (let index = 3; index <= number; index++) { - result = a + b; - a = b; - b = result; - } - return result; - -} -module.exports = { - rectCover: rectCover -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/双指针/FindContinuousSequence.js b/manuscripts/算法/剑指/双指针/FindContinuousSequence.js deleted file mode 100644 index e59aeb6..0000000 --- a/manuscripts/算法/剑指/双指针/FindContinuousSequence.js +++ /dev/null @@ -1,52 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-30 21:05:44 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-30 21:36:15 - */ - - -// 注意是连续递增数列,间距为1 那么可以将序列开始、结束元素看做 a 、 b -// 按照数列求和公式sum=(首项+尾项)* 项数 /2 即可 -function FindContinuousSequence (sum) { - - // 按照sum值,先预估大概最多满足条件的序列 - // sum=(b+a)(b-a+1)/2 - let left = 1, right = 2; - - let result = []; - while (left < right) { - // 从left....right的序列求和 - const temp_sum = (right + left) * (right - left + 1) - if (2 * sum === temp_sum) { - // 满足条件 - let count = left; - let temp_arr = []; - while (count <= right) { - temp_arr.push(count) - count++ - } - result.push(temp_arr) - // 向前寻找 - left++ - } else if (2 * sum > temp_sum) { - // 右边向右 - right++ - } else { - // 左边向右 - left++ - } - - } - - return result; - - -} - -// console.log(FindContinuousSequence(9)) -module.exports = { - FindContinuousSequence: FindContinuousSequence -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/双指针/FindNumbersWithSum.js b/manuscripts/算法/剑指/双指针/FindNumbersWithSum.js deleted file mode 100644 index b2c552c..0000000 --- a/manuscripts/算法/剑指/双指针/FindNumbersWithSum.js +++ /dev/null @@ -1,76 +0,0 @@ -/* - * @Description: 【中等】和为S的两个数字 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-30 20:29:08 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-30 21:04:46 - */ - - -// 根据x+y=sum 求xy最小 由于array是递增的,则x y 距离越远 xy值越小,x y距离越近xy值越大 -// x+y=(x+m)+(y-m)=sum 假设x是左边元素,y是右边元素 即:y>x -// 可以理解乘积(x+m)(y-m)=xy-(y-x)*m-m^2 其中y-x>0 m^2 -// 所以m值越大,其实(x+m)(y-m)越小,也就是x与y间隔也大 xy越小 ,由于array是递增的,所以只需要找到第一个满足和为sum的即可 -function FindNumbersWithSum (array, sum) { - - let left = 0, right = array.length - 1; - - while (left < right) { - if (array[left] + array[right] === sum) { - // 第一个就返回 - return [array[left], array[right]] - } else if (array[left] + array[right] > sum) { - // 向左 - right-- - } else { - // 向右 - left++ - } - } - - // 不存在满足添加,就返回空数组 - return [] - -} - -// 注意数组array是递增的 -// 不存在的时候,返回空数组 -function FindNumbersWithSum01 (array, sum) { - - - let left = 0, right = array.length - 1; - - // 将最小值标记设置成最大 - let min = array[right] * array[right] - let result = []; - while (left < right) { - if (array[left] + array[right] === sum) { - // 符合条件 - if (min > array[left] * array[right]) { - // 最小值 - min = array[left] * array[right] - result = [array[left], array[right]] - } - // 移动指针 - left++; - right-- - } else if (array[left] + array[right] < sum) { - // 左边向右 - left++ - } else { - // 右边向左 - right-- - } - - } - - // 跳出循环 - return result; - -} - -console.log(FindNumbersWithSum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 21)) -module.exports = { - FindNumbersWithSum: FindNumbersWithSum -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/双指针/LeftRotateString.js b/manuscripts/算法/剑指/双指针/LeftRotateString.js deleted file mode 100644 index ba826c2..0000000 --- a/manuscripts/算法/剑指/双指针/LeftRotateString.js +++ /dev/null @@ -1,50 +0,0 @@ -/* - * @Description: 【中等】左旋转字符串 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-30 22:00:13 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-30 22:16:13 - */ - -function LeftRotateString (str, n) { - // write code here - // 临界条件 - if(!str||str.length .redocwon a ma I -----> I am a nowcoder. -function ReverseSentence01 (str) { - // write code here - let arr = str.split(' '); - - for (let index = 0; index < arr.length; index++) { - // 翻转字符串 - arr[index] = reverseStr(arr[index]) - } - // 翻转数组 拼接 - return reverseArr(arr).join(' ') - -} - -function reverseStr (str) { - let result = ''; - for (let index = str.length - 1; index >= 0; index--) { - result += str[index] - } - return result; -} - -function reverseArr (arr) { - let result = []; - for (let index = arr.length - 1; index >= 0; index--) { - result.push(reverseStr(arr[index])) - } - - return result; -} - - - -console.log(ReverseSentence01('nowcoder. a am I')) -module.exports = { - ReverseSentence: ReverseSentence -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/排列/PrintMinNumber.js b/manuscripts/算法/剑指/排列/PrintMinNumber.js deleted file mode 100644 index 9e4b32b..0000000 --- a/manuscripts/算法/剑指/排列/PrintMinNumber.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - * @Description: 【较难】把数组排成最小的数 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-05 07:58:03 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 07:59:26 - */ - - -// 比较s1,s2的时候,注意比较s1+s2 0) { - for (let index = 0; index < numbers.length - 1; index++) { - - if (parseInt(`${numbers[index]}${numbers[index + 1]}`) >= parseInt(`${numbers[index + 1]}${numbers[index]}`)) { - - // 元素交换 - [numbers[index], numbers[index + 1]] = [numbers[index + 1], numbers[index]] - } - } - len-- - } - - - - return numbers.join('') - -} -module.exports = { - PrintMinNumber: PrintMinNumber -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/排列/reOrderArray.js b/manuscripts/算法/剑指/排列/reOrderArray.js deleted file mode 100644 index 9ba1f5a..0000000 --- a/manuscripts/算法/剑指/排列/reOrderArray.js +++ /dev/null @@ -1,34 +0,0 @@ -/* - * @Description: 【中等】调整数组顺序使奇数位于偶数前面 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-03 15:56:06 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 07:54:38 - */ - -// 利用空间 -function reOrderArray (array) { - // write code here - // 奇数,偶数; - let odd_arr = [], even_arr = []; - for (let index = 0; index < array.length; index++) { - - if (array[index] % 2 !== 0) { - // 奇数 - odd_arr.push(array[index]) - } else { - // 偶数 - even_arr.push(array[index]) - } - } - - // 拼接 - return odd_arr.concat(even_arr) -} - -// 利用冒泡,遇见偶数往最后放,统一前移 - -module.exports = { - reOrderArray: reOrderArray -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/数学/LastRemaining_Solution.js b/manuscripts/算法/剑指/数学/LastRemaining_Solution.js deleted file mode 100644 index 0d6a431..0000000 --- a/manuscripts/算法/剑指/数学/LastRemaining_Solution.js +++ /dev/null @@ -1,72 +0,0 @@ -/* - * @Description: 【中等】圆圈中最后剩下的数 约瑟夫问题 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-05 14:48:28 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 15:11:02 - */ - - - -// 把n个人的编号改为0~n-1,然后对删除的过程进行分析。 -// 第一个删除的数字是(m-1)%n,几位k,则剩余的编号为(0,1,...,k-1,k+1,...,n-1),下次开始删除时,顺序为(k+1,...,n-1,0,1,...k-1)。 -// 用f(n,m)表示从(0~n-1)开始删除后的最终结果。 -// 用q(n-1,m)表示从(k+1,...,n-1,0,1,...k-1)开始删除后的最终结果。 -// 则f(n,m)=q(n-1,m)。 - -// 下面把(k+1,...,n-1,0,1,...k-1)转换为(0~n-2)的形式,即 -// k+1对应0 -// k+2对于1 -// ... -// k-1对应n-2 -// 转化函数设为p(x)=(x-k-1)%n, p(x)的逆函数为p^(x)=(x+k+1)%n。 -// 则f(n,m)=q(n-1,m)=p^(f(n-1,m))=(f(n-1,m)+k+1)%n,又因为k=(m-1)%n。 -// 取余 -// f(n,m)=(f(n-1,m)+m)%n; - -// 最终的递推关系式为 -// f(1,m) = 0; (n=1) -// f(n,m)=(f(n-1,m)+m)%n; (n>1) - -function LastRemaining_Solution (n, m) { - // write code here - // 递推公式: f(0)=-1 f(1)=0 f(i)={f(i-1)+m}%i - - if (n === 0) { - return -1; - } - - if (n === 1) { - return 0; - } - - // 递归 - return (LastRemaining_Solution(n - 1, m) + m) % n; - -} - -// 非递归实现 -function LastRemaining_Solution01 (n, m) { - - // 当然,这里也可以添加上负数的校验情况 - if (n === 0) { - return -1; - } - - if (n === 1) { - return 0 - } - - // 循环处理 - let result = 0 - for (let index = 2; index <= n; index++) { - // f(n,m)=[f(n-1,m)+m]%n - result = (result + m) % index - } - // 返回 - return result; -} -module.exports = { - LastRemaining_Solution: LastRemaining_Solution -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/数学/MoreThanHalfNum_Solution.js b/manuscripts/算法/剑指/数学/MoreThanHalfNum_Solution.js deleted file mode 100644 index 21de050..0000000 --- a/manuscripts/算法/剑指/数学/MoreThanHalfNum_Solution.js +++ /dev/null @@ -1,40 +0,0 @@ -/* - * @Description: 【 简单】数组中出现次数超过一半的数字 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-05 14:28:36 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 14:29:22 - */ - -// 借助map计数即可 -function MoreThanHalfNum_Solution (numbers) { - // write code here - let map = new Map(); - - numbers.forEach(item => { - if (map.has(item)) { - map.set(item, map.get(item) + 1) - } else { - map.set(item, 1) - } - }) - - const arr = [...new Set(numbers)]; - - // console.log(map,arr) - let result = 0; - - arr.map(item => { - if (2 * map.get(item) > numbers.length) { - result = item; - } - }) - - - return result; - -} -module.exports = { - MoreThanHalfNum_Solution: MoreThanHalfNum_Solution -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/数学/NumberOf1Between1AndN_Solution.js b/manuscripts/算法/剑指/数学/NumberOf1Between1AndN_Solution.js deleted file mode 100644 index e927a1d..0000000 --- a/manuscripts/算法/剑指/数学/NumberOf1Between1AndN_Solution.js +++ /dev/null @@ -1,34 +0,0 @@ -/* - * @Description: 【中等】从1到n整数中1出现的次数 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-26 22:23:17 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 15:15:56 - */ - - -// 转化 -function NumberOf1Between1AndN_Solution(n) -{ - // write code here - let count=0; - while(n>0){ - // 转化为字符串 - count += (n).toString().split('').reduce((sum,item)=>{ - if(item==='1'){ - sum++; - } - return sum; - },0) - - // 递减 - n-- - } - return count; -} - -// console.log(NumberOf1Between1AndN_Solution(13)) -module.exports = { - NumberOf1Between1AndN_Solution : NumberOf1Between1AndN_Solution -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/数组和矩阵/Find.js b/manuscripts/算法/剑指/数组和矩阵/Find.js deleted file mode 100644 index f3a55db..0000000 --- a/manuscripts/算法/剑指/数组和矩阵/Find.js +++ /dev/null @@ -1,100 +0,0 @@ -/* - * @Description: 二维数组中的查找 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-27 08:52:21 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-27 10:00:41 - */ - -/** - * - * 每一行都按照从左到右递增的顺序排序 - * 每一列都按照从上到下递增的顺序排序 - * - * 直接循环找 选取右上或者左下元素为起点 - * - */ - - -// 右上角元素为起点,确保每个元素都能遍历到,都能按照方向走即可 -function Find (target, array) { - // write code here - - // 行数 - let row = array.length; - if (row === 0) { - return false; - } - - // 列数 - let col = array[0].length - // 此时选择右上为起点 - let r = 0, c = col - 1 - while (r < row && c >= 0) { - if (target === array[r][c]) { - // 命中目标,返回 - return true; - } else if (target < array[r][c]) { - // 目标元素比右上小,target往左找 - c-- - } else if (target > array[r][c]) { - // 目标元素比当前元素大,target往下找 - r++ - } - } - return false; - -} - - -// 比较蠢的方法,通过遍历二维数组进行比较 -// 这里可以将第二层循环改成二分查找【用到从左到右递增的特点】;降低时间复杂度 -// 这里的时间复杂度为O(nlogn) -function Find01(target,array){ - - let row = array.length; - if (row === 0) { - return false; - } - - // 列数 - let col = array[0].length - - // 从第0行开始 - for(let r=0;rtarget){ - // 在左侧,从左边找 - high=mid-1; - }else if(array[r][mid]{ - return arr.some(item=>item===target) - }) -} - -console.log(Find02(19,[[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]])) - -module.exports = { - Find: Find -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/数组和矩阵/FirstNotRepeatingChar.js b/manuscripts/算法/剑指/数组和矩阵/FirstNotRepeatingChar.js deleted file mode 100644 index a0d7d0c..0000000 --- a/manuscripts/算法/剑指/数组和矩阵/FirstNotRepeatingChar.js +++ /dev/null @@ -1,41 +0,0 @@ -/* - * @Description: 第一个只出现一次的字符位置 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-28 22:23:51 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-28 22:24:20 - */ - - -function FirstNotRepeatingChar (str) { - // write code here - // 转换为数组 - let arr = str.split(''); - // 数组里面去去重 - for (let index = 0; index < arr.length; index++) { - if (arr.indexOf(arr[index]) === arr.lastIndexOf(arr[index])) { - return index - } - } - return -1 - -} - -function FirstNotRepeatingChar01 (str) { - // write code here - const len = str.length; - - for (let index = 0; index < len - 1; index++) { - const s = str.slice(index, index + 1); - if (`${str.slice(0, index)}${str.slice(index + 1)}`.indexOf(s) === -1) { - return index; - } - } - return -1 - -} - -module.exports = { - FirstNotRepeatingChar: FirstNotRepeatingChar -}; diff --git a/manuscripts/算法/剑指/数组和矩阵/duplicate.js b/manuscripts/算法/剑指/数组和矩阵/duplicate.js deleted file mode 100644 index bef9d10..0000000 --- a/manuscripts/算法/剑指/数组和矩阵/duplicate.js +++ /dev/null @@ -1,54 +0,0 @@ -/* - * @Description: 数组中重复的数字 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-27 08:41:26 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-27 08:51:03 - */ - - -/** - * - * 方案一:将所有数据从小打到排序,如果存在左右相等的情况就是重复了,输出一个即可; - * 方案二:借助空数组,循环遍历目标数组按照index的值放入 如果存在则重复 - * - * - */ - - -function duplicate (numbers) { - // write code here - - let arr = []; - for (let i = 0; i < numbers.length; i++) { - // 这部分依赖的是排序后,如果不重复理想情况下:numbers[index]===index的情况 - if (!arr[numbers[i]]) { - arr[numbers[i]] = 1; - } else { - return numbers[i] - } - } - return -1 -} - - -// 存在问题:时间复杂度依赖sort()函数 -function duplicate01 (numbers) { - - // 从小到大排列 - numbers = numbers.sort((a, b) => a - b) - - for (let index = 0; index < numbers.length - 1; index++) { - if (numbers[index] === numbers[index + 1]) { - // 存在两个元素重复 - return numbers[index] - } - } - - return -1; -} - -module.exports = { - duplicate: duplicate -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/数组和矩阵/printMatrix.js b/manuscripts/算法/剑指/数组和矩阵/printMatrix.js deleted file mode 100644 index 16bddaa..0000000 --- a/manuscripts/算法/剑指/数组和矩阵/printMatrix.js +++ /dev/null @@ -1,78 +0,0 @@ -/* - * @Description: 顺时针打印矩阵 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-27 14:22:13 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-28 22:25:16 - */ - - -function printMatrix (matrix) { - // write code here - - // 行 角标 - let row = matrix.length - 1; - // 列角标 - if (matrix[0].length === 0) { - // 返回空数组 - return [] - } - let col = matrix[0].length - 1 - - let r = 0, - c = 0; - - // 这里需要在脑海里想象顺时针旋转的时候,r-->left c-->top row-->right col-->bottom - - - - // 数组结果 - let result = [] - while (r <= row && c <= col) { - - // 上 - for (let index = c; index <= col; index++) { - // 当前在上测,行为r 不为 row - result.push(matrix[r][index]) - } - - // 跳出后,到右侧 - for (let index = r + 1; index <= row; index++) { - result.push(matrix[index][col]) - } - // 跳出来,就是下测 判断右下角拐角 - if (r !== row) { - // 从右到左边 - for (let index = col - 1; index >= c; index--) { - // 当前在底部,所以行为row - result.push(matrix[row][index]) - } - } - // 左侧,注意判断条件 - if (c !== col) { - // 从下到上 - for (let index = row - 1; index > r; index--) { - // 当前在左侧,所以列表为c 部位col - result.push(matrix[index][c]) - } - } - - // 指针移动 转完一圈 - r++; - row--; - c++; - col--; - } - - return result; -} - - -console.log(printMatrix([ - [1, 2], - [3, 4] -])) -module.exports = { - printMatrix: printMatrix -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/数组和矩阵/replaceSpace.js b/manuscripts/算法/剑指/数组和矩阵/replaceSpace.js deleted file mode 100644 index 72e5b27..0000000 --- a/manuscripts/算法/剑指/数组和矩阵/replaceSpace.js +++ /dev/null @@ -1,31 +0,0 @@ -/* - * @Description: 替换空格 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-27 10:07:45 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-27 10:08:59 - */ - - -// 先转化为数组,再进行拼接 -function replaceSpace( s ) { - // write code here - return s.split(' ').join('%20') -} - -// 利用循环拼接,然后slice剪切 -function replaceSpace01( s ) { - // write code here - const arr=s.split(' '); - let str=''; - - for(let index=0;index input.length ? [] : input.sort((a, b) => a - b).slice(0, k) -} - -// 基于冒泡实现 -function GetLeastNumbers_Solution01 (input, k) { - // write code here - - // 添加参数校验 - if (k > input.length) { - return [] - } - // 先将输入的数组进行排序从小到大 只拍前面几个即可 - // 这里首先想到的是冒泡或者插入排序里面的特性 --- 每次都有一个元素在最终的位置上 - - // 循环k次,跑k趟 - for (let index = 0; index < k; index++) { - for (let j = input.length - 1; j > index; j--) { - if (input[index] > input[j]) { - // 找到比它的,位置交换 - [input[index], input[j]] = [input[j], input[index]] - } - } - } - - // 排序完毕,输入前k个 - return input.slice(0, k) -} -module.exports = { - GetLeastNumbers_Solution: GetLeastNumbers_Solution -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/栈队列堆/GetMinInJSStack.js b/manuscripts/算法/剑指/栈队列堆/GetMinInJSStack.js deleted file mode 100644 index 6b61d6c..0000000 --- a/manuscripts/算法/剑指/栈队列堆/GetMinInJSStack.js +++ /dev/null @@ -1,41 +0,0 @@ -/* - * @Description: 【较难】包含min函数的栈 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-29 22:04:17 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-29 22:04:36 - */ - - - -let result = []; -function push (node) { - // write code here - if (result) { - return result.push(node) - } -} -function pop () { - // write code here - if (result.length > 0) { - return result.pop(); - } -} -function top () { - // write code here - if (result.length > 0) { - return result[result.length - 1] - } -} -function min () { - // write code here - // 对result数组进行排序 - return Math.min(...result) -} -module.exports = { - push: push, - pop: pop, - top: top, - min: min -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/栈队列堆/InsertAndGetMedian.js b/manuscripts/算法/剑指/栈队列堆/InsertAndGetMedian.js deleted file mode 100644 index 38e2ce8..0000000 --- a/manuscripts/算法/剑指/栈队列堆/InsertAndGetMedian.js +++ /dev/null @@ -1,40 +0,0 @@ -/* - * @Description: 【中等】数据流中的中位数 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-29 21:38:01 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-29 21:39:13 - */ - - - -// 存储数据 -let result = []; -function Insert (num) { - // write code here - result.push(num) - // 当然,也可以在push之后去排序 -} -function GetMedian () { - // write code here - // 先排序 - result = result.sort((a, b) => a - b); - let len = result.length; - - // 奇数 - if (len % 2 !== 0) { - return result[Math.floor((len - 1) / 2)] - } else { - // 分别获取低位 和高位 - const low = result[Math.floor((len - 2) / 2)] - const high = result[Math.floor((len) / 2)] - - // 去平均值 - return (high + low) / 2 - } -} -module.exports = { - Insert: Insert, - GetMedian: GetMedian -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/栈队列堆/JSStackToQueue.js b/manuscripts/算法/剑指/栈队列堆/JSStackToQueue.js deleted file mode 100644 index ed5f82c..0000000 --- a/manuscripts/算法/剑指/栈队列堆/JSStackToQueue.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * @Description: 【简单】用两个栈实现队列 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-29 22:06:51 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-29 22:07:22 - */ - -let result = [] -function push (node) { - // write code here - // 尾部进栈 - result.push(node) -} -function pop () { - // write code here - // 队列 先进先出 头部出去 - return result.shift() -} -module.exports = { - push: push, - pop: pop -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/栈队列堆/maxInWindows.js b/manuscripts/算法/剑指/栈队列堆/maxInWindows.js deleted file mode 100644 index 99f5cb2..0000000 --- a/manuscripts/算法/剑指/栈队列堆/maxInWindows.js +++ /dev/null @@ -1,28 +0,0 @@ -/* - * @Description: 【较难】滑动窗口的最大值 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-29 21:49:42 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-29 21:58:32 - */ - -function maxInWindows (num, size) { - // write code here - let len = num.length; - let result = []; - if (len < size || size === 0) { - // 窗口大于数组长度 || 窗口大小为0 - return [] - } - - for (let index = 0; index <= len - size; index++) { - - // 获取最大值 【这里可以抽一个多数求最大的函数出来】 - result.push(Math.max(...num.slice(index, index + size))) - } - return result; -} -module.exports = { - maxInWindows: maxInWindows -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/树/FindPath.js b/manuscripts/算法/剑指/树/FindPath.js deleted file mode 100644 index f75aa7f..0000000 --- a/manuscripts/算法/剑指/树/FindPath.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - * @Description: 【较难】二叉树中和为某一值的路径 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-12 22:01:00 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-12 22:01:45 - */ - - -/* function TreeNode(x) { - this.val = x; - this.left = null; - this.right = null; -} */ - -function FindPath (root, expectNumber) { - // write code here - let result = []; - function dfs (root, target, temp_res) { - // 处理空树 - if (root === null) { - return; - } - // 根结点,进数组 - temp_res.push(root.val); - - // 当前结点为叶子结点 - if (root.left === null && root.right === null && target === root.val) { - result.push(temp_res) - } - - // 不是叶子结点,向左向右子树递归 - dfs(root.left, target - root.val, [...temp_res]) - dfs(root.right, target - root.val, [...temp_res]) - } - dfs(root, expectNumber, []) - return result; - -} - - - -module.exports = { - FindPath: FindPath -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/树/GetNext.js b/manuscripts/算法/剑指/树/GetNext.js deleted file mode 100644 index 7f51650..0000000 --- a/manuscripts/算法/剑指/树/GetNext.js +++ /dev/null @@ -1,52 +0,0 @@ -/* - * @Description: 【中等】二叉树的下一个结点 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-11 15:38:02 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-11 15:40:20 - */ - -/*function TreeLinkNode(x){ - this.val = x; - this.left = null; - this.right = null; - this.next = null; -}*/ -function GetNext (pNode) { - // write code here - - // 中序遍历 ---> 左-根(pNode)-右 - // 所以这里pNode的下一个结点,要么是pNode右子树的第一个最左结点(左边的叶子结点) - // 但是如果pNode的右子树为空,则pNode的下个结点,则为第一个左子树指向pNode的祖先结点 - - if (pNode.right !== null) { - // 存在右子树,寻找右子树的最左结点 - let right_node = pNode.right; - while (right_node.left !== null) { - // 一直向左找 - right_node = right_node.left; - } - // 跳出循环的时候,right_node.left==null;即:right_node为叶子结点 - return right_node - } else { - // 右子树为空,则向上找 - while (pNode.next !== null) { - let parent_node = pNode.next; - if (parent_node.left === pNode) { - return parent_node; - } - // 父结点的左子树不是指向给定的结点的话,则继续往上寻找父结点 - pNode = pNode.next; - } - - } - - // 都找不到,则返回null - return null; - - -} -module.exports = { - GetNext: GetNext -}; diff --git a/manuscripts/算法/剑指/树/HasSubtree.js b/manuscripts/算法/剑指/树/HasSubtree.js deleted file mode 100644 index 4664d2e..0000000 --- a/manuscripts/算法/剑指/树/HasSubtree.js +++ /dev/null @@ -1,54 +0,0 @@ -/* - * @Description: 【较难】树的子结构 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-11 16:07:54 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-11 16:08:40 - */ - -/* function TreeNode(x) { - this.val = x; - this.left = null; - this.right = null; -} */ -function HasSubtree (pRoot1, pRoot2) { - // write code here - // 空树的情况 - if (pRoot1 === null || pRoot2 === null) { - return false; - } - // 1.假设子结构从根结点开始; - const is_root = isSubTreeWithRoot(pRoot1, pRoot2); - // 2 子结构从左、右子树开始; - const in_left = HasSubtree(pRoot1.left, pRoot2); - const in_right = HasSubtree(pRoot1.right, pRoot2); - - // 三种满足一种,就是子结构了 - return is_root || in_left || in_right; -} - -// 判断root2是否为root1的子集【递归实现】 -function isSubTreeWithRoot (root1, root2) { - - // root2 自己为叶子结点,此时递归来看,就是子集 - if (root2 === null) { - return true; - } - // 递归来看,root1为空了,就是非子集 - if (root1 === null) { - return false; - } - - // 判断两者元素是否匹配 - if (root1.val !== root2.val) { - // 其中某个元素不配,递归断了 - return false; - } - // 当前结点相同,继续递归校验后续左右子树结点,两树同时向左或者向右 - return isSubTreeWithRoot(root1.left, root2.left) && isSubTreeWithRoot(root1.right, root2.right) -} - -module.exports = { - HasSubtree: HasSubtree -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/树/KthNode.js b/manuscripts/算法/剑指/树/KthNode.js deleted file mode 100644 index bc2d535..0000000 --- a/manuscripts/算法/剑指/树/KthNode.js +++ /dev/null @@ -1,75 +0,0 @@ -/* - * @Description: 【简单】二叉搜索树的第K个结点 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-12 19:51:41 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-12 20:26:21 - */ - -function ListNode (x) { - this.val = x; - this.left = null; - this.right = null; -} - -// {8,6,10,5,7,9,11},1 -const root = { - val: 8, - left: { - val: 6, - left: { - val: 5, - left: null, - right: null - }, - right: { - val: 7, - left: null, - right: null - } - }, - right: { - val: 10, - left: { - val: 9, - left: null, - right: null - }, - right: { - val: 11, - left: null, - right: { - val: 12, - left: null, - right: null - } - } - } -} - -/* function TreeNode(x) { - this.val = x; - this.left = null; - this.right = null; -} */ - -// 注意,是返回结点 -function KthNode (pRoot, k) { - // console.log(inOrder(pRoot)) - return inOrder(pRoot)[k - 1] -} - -// 中序遍历 -function inOrder (root) { - // console.log(root) - if (root === null) { - return [] - } - - return inOrder(root.left).concat([root]).concat(inOrder(root.right)) - -} -module.exports = { - KthNode: KthNode -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/树/Mirror.js b/manuscripts/算法/剑指/树/Mirror.js deleted file mode 100644 index 4ed6118..0000000 --- a/manuscripts/算法/剑指/树/Mirror.js +++ /dev/null @@ -1,47 +0,0 @@ -/* - * @Description: 【简单】二叉树的镜像 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-11 16:42:42 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-11 16:43:17 - */ - -/* - * function TreeNode(x) { - * this.val = x; - * this.left = null; - * this.right = null; - * } - */ -/** - * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 - * - * - * @param pRoot TreeNode类 - * @return TreeNode类 - */ -function Mirror (pRoot) { - // write code here - // 空树 - if (pRoot === null) { - return pRoot; - } - - // 处理根节点,交换左右子树【建议封装函数】 - [pRoot.left, pRoot.right] = [pRoot.right, pRoot.left] - - - // 左子树镜像 - Mirror(pRoot.left) - // 右子树镜像 - Mirror(pRoot.right) - - return pRoot; - -} - - -module.exports = { - Mirror: Mirror -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/树/Print.js b/manuscripts/算法/剑指/树/Print.js deleted file mode 100644 index dba29c0..0000000 --- a/manuscripts/算法/剑指/树/Print.js +++ /dev/null @@ -1,74 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-12 21:37:07 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-12 21:40:15 - */ - - -/* function TreeNode(x) { - this.val = x; - this.left = null; - this.right = null; -} */ -function Print (pRoot) { - // write code here - // 层序遍历的 进阶 - - // 处理空树的情况 - if (pRoot === null) { - return []; - } - - // 临时队列 存放二叉树 - let temp_queue = [pRoot]; - // 存放结果 - let result = [] - - while (temp_queue.length > 0) { - - // 记录当前层有几个子结点 ---> 子树 - let count = temp_queue.length; - - // 记录一层的数据,先不考虑从左到右还是从右到左 - let level_arr = []; - while (count > 0) { - const currentTree = temp_queue.shift(); - - // 根结点存在 - if (currentTree !== null) { - // 直接放入根结点 - level_arr.push(currentTree.val) - // 处理左子树 - if (currentTree.left !== null) { - // 当前层的结点,进入队列 - temp_queue.push(currentTree.left) - // level_arr.push(currentTree.left.val) - } - - if (currentTree.right !== null) { - temp_queue.push(currentTree.right) - // level_arr.push(currentTree.right.val) - } - // 每次只能处理一个 - count-- - } - - } - result.push(level_arr) - } - - // 这里已经按照层序遍历输出了,后面可以对数组进行奇数|偶数处理 - // 当然,也可以在数组push的时候进行奇数|偶数计数处理 - result.map((item, index) => { - return (index + 1) % 2 === 1 ? item : item.reverse() - }) - return result; -} - - -module.exports = { - Print: Print -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/树/isSymmetrical.js b/manuscripts/算法/剑指/树/isSymmetrical.js deleted file mode 100644 index a344367..0000000 --- a/manuscripts/算法/剑指/树/isSymmetrical.js +++ /dev/null @@ -1,45 +0,0 @@ -/* - * @Description: 【困难】对称的二叉树 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-11 17:52:52 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-11 17:53:26 - */ - -/* function TreeNode(x) { - this.val = x; - this.left = null; - this.right = null; -} */ -function isSymmetrical (pRoot) { - // write code here - if (pRoot === null) { - // 子树为空,判断子树为对称 - return true; - } - - // 不为空则判断左右子树是否对称,依据:左右子树位置互换,依旧对称 【调用一次】 - return isSameTree(pRoot.left, pRoot.right) -} - -// 判断左右子树是是否对称 -function isSameTree (leftTree, rightTree) { - if (leftTree === null && rightTree === null) { - return true; - } else if (leftTree !== null && rightTree !== null) { - // 左右子树不为空,则分别比对左右子树 - if (leftTree.val === rightTree.val) { - // 根结点相同,比对左右子树 - return isSameTree(leftTree.left, rightTree.right) && isSameTree(rightTree.left, leftTree.right); - } - - } else { - return false; - } -} - - -module.exports = { - isSymmetrical: isSymmetrical -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/树/reConstructBinaryTree.js b/manuscripts/算法/剑指/树/reConstructBinaryTree.js deleted file mode 100644 index 70615fe..0000000 --- a/manuscripts/算法/剑指/树/reConstructBinaryTree.js +++ /dev/null @@ -1,37 +0,0 @@ -/* - * @Description: 【中等】重建二叉树 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-11 12:57:40 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-11 12:58:33 - */ - -/* function TreeNode(x) { - this.val = x; - this.left = null; - this.right = null; -} */ -function reConstructBinaryTree (pre, vin) { - // write code here - if (pre.length === 0) { - return null; - } - // 获取根节点 ,新建二叉树结点 - const rootNode = new TreeNode(pre[0]); - - const rootIndex = vin.indexOf(pre[0]); - console.log(rootIndex, pre[0], vin.slice(0, rootIndex), vin.slice(rootIndex + 1)); - console.log(rootIndex, pre[0], pre.slice(1, rootIndex), pre.slice(rootIndex + 1)); - // 注意,找中序结点的时候,需要去除根结点,先序的时候,要 - rootNode.left = reConstructBinaryTree(pre.slice(1, rootIndex + 1), vin.slice(0, rootIndex)); - rootNode.right = reConstructBinaryTree(pre.slice(rootIndex + 1), vin.slice(rootIndex + 1)) - - // - - return rootNode; - -} -module.exports = { - reConstructBinaryTree: reConstructBinaryTree -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/贪心思想/maxProfit.js b/manuscripts/算法/剑指/贪心思想/maxProfit.js deleted file mode 100644 index f655482..0000000 --- a/manuscripts/算法/剑指/贪心思想/maxProfit.js +++ /dev/null @@ -1,62 +0,0 @@ -/* - * @Description: 买卖股票的最好时机 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-29 23:27:18 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-03 15:00:53 - */ -/** - * 暴力 - * @param prices int整型一维数组 - * @return int整型 - */ -function maxProfit (prices) { - // write code here - // 最低点买入,最高点卖出,收益最大 实际求的是一个子序列,最大和最小的差值 最小在前,最大在后 - - // 最大收益为0 其他都不算收益 - let max = 0; - - for (let index = 0; index < prices.length; index++) { - let start = prices[index]; - let end = Math.max(...prices.slice(index + 1)); - if (end - start > max) { - max = end - start; - } - } - - // 返回最大值 - return max; -} - -/** - * 处理买点,卖点 - * @param {number[]} prices - * @return {number} - */ -function maxProfit (prices) { - - // 最大收益为0 其他都不算收益 - let max = 0; - // 定义最小的值为买入 - let minPrice = Infinity - for (let index = 0; index < prices.length; index++) { - let start = prices[index]; - // 处理买点 - if (start < minPrice) { - minPrice = start - } - // 处理卖点 - if (start - minPrice > max) { - // 获取最大收益 - max = start - minPrice - } - } - - // 返回最大值 - return max; -}; -module.exports = { - maxProfit: maxProfit -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/链表/EntryNodeOfLoop.js b/manuscripts/算法/剑指/链表/EntryNodeOfLoop.js deleted file mode 100644 index af06c47..0000000 --- a/manuscripts/算法/剑指/链表/EntryNodeOfLoop.js +++ /dev/null @@ -1,21 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-02 15:58:48 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-02 15:58:58 - */ - - -/*function ListNode(x){ - this.val = x; - this.next = null; -}*/ -function EntryNodeOfLoop (pHead) { - // write code here - -} -module.exports = { - EntryNodeOfLoop: EntryNodeOfLoop -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/链表/FindFirstCommonNode.js b/manuscripts/算法/剑指/链表/FindFirstCommonNode.js deleted file mode 100644 index e296125..0000000 --- a/manuscripts/算法/剑指/链表/FindFirstCommonNode.js +++ /dev/null @@ -1,48 +0,0 @@ -/* - * @Description: 【简单】两个链表的第一个公共结点 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-02 21:33:19 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-02 21:44:24 - */ - - -/*function ListNode(x){ - this.val = x; - this.next = null; -}*/ -function FindFirstCommonNode (pHead1, pHead2) { - // write code here - // 双重循环,时间复杂度过大,考虑优化 - // while(pHead1!==null){ - // let pHead2_back=pHead2 - // while(pHead2_back!==null){ - // if(pHead1===pHead2_back){ - // return pHead1; - // }else{ - // pHead2=pHead2_back.next; - // } - // } - // // 对pHead2从头开始 - // pHead2_back=pHead2 - // pHead1=pHead1.next; - // } - // return null; - - let p1 = pHead1, p2 = pHead2; - - // 其实,这里有个死循环的问题 - while (p1 !== p2) { - // 不相等,则向前,如果后继结点为空,则回到头结点,重复 - p1 = p1 === null ? pHead1 : p1.next; - p2 = p2 === null ? pHead2 : p2.next; - } - - // 返回公共结点 - return p1; - -} -module.exports = { - FindFirstCommonNode: FindFirstCommonNode -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/链表/FindKthToTail.js b/manuscripts/算法/剑指/链表/FindKthToTail.js deleted file mode 100644 index 2ded71b..0000000 --- a/manuscripts/算法/剑指/链表/FindKthToTail.js +++ /dev/null @@ -1,74 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-02 15:47:20 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-02 15:55:43 - */ - - -/* - * function ListNode(x){ - * this.val = x; - * this.next = null; - * } - */ -/** - * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 - * - * - * @param pHead ListNode类 - * @param k int整型 - * @return ListNode类 - */ -function FindKthToTail (pHead, k) { - // write code here - // 结点不存在返回空 - if (pHead === null) { - return null; - } - // 遍历链表, - let count = 0; - let pre = pHead; - while (pHead) { - // 将数组进行头插 - count++ - pHead = pHead.next; - } - // 链表长度小于k,返回空 - if (count < k) { - return null; - } - // 再对链表进行遍历 - while (count > k) { - count-- - pre = pre.next; - } - // 返回倒数k个结点 - return pre; -} - -// 上面是有两次while一次计数,一次移动指针找到目标结点 -// 可以利用数组,不能是头插入,还是尾插都行,存储的元素为链表的结点,也就是链表的子链表的头指针 -function FindKthToTail01 (pHead, k) { - - let result = [] - while (pHead !== null) { - // 头插 - result.unshift(pHead); - - // 指针后移 - pHead = pHead.next; - } - // k不合法 - if (result.length < k) { - return null - } - // 用尾插的话,返回 result[result.length-k] - return result[k - 1] -} - -module.exports = { - FindKthToTail: FindKthToTail01 -}; \ No newline at end of file diff --git a/manuscripts/算法/剑指/链表/Merge.js b/manuscripts/算法/剑指/链表/Merge.js deleted file mode 100644 index b526302..0000000 --- a/manuscripts/算法/剑指/链表/Merge.js +++ /dev/null @@ -1,76 +0,0 @@ -/* - * @Description: 【简单】合并两个排序的链表 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-02 17:18:36 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-02 17:30:18 - */ - - -function ListNode (x) { - this.val = x; - this.next = null; -} - -// 注意 两个链表都是单调递增 -function Merge (pHead1, pHead2) { - // write code here - let pre = new ListNode(-1); - let result = pre - while (pHead1 !== null && pHead2 !== null) { - if (pHead1.val >= pHead2.val) { - // 前者大,用后者 - result.next = pHead2; - pHead2 = pHead2.next; - } else if (pHead1.val < pHead2.val) { - // 后者大,用前者 - result.next = pHead1; - pHead1 = pHead1.next; - } - // 后移指针 - result = result.next; - } - if (pHead1 !== null) { - // 链表1没有走完 - result.next = pHead1 - } - - if (pHead2 !== null) { - // 链表2没有走完 - result.next = pHead2; - } - // 返回 注意去掉头结点 - return pre.next; -} - -// 思考递归的做法 没有上面迭代好理解 -function Merge01(pHead1,pHead2){ - - - if(pHead1===null){ - return pHead2 - } - - if(pHead2===null){ - return pHead1 - } - - // 前者大于后者, - if(pHead1.val>=pHead2.val){ - // 记住,pHead2向后, - pHead2.next=Merge01(pHead1,pHead2.next) - return pHead2 - } - - // 前者小于后者 - if(pHead1.val xsum){ - // 向左 - } - } - - // 跳出循环 - return result; - -} - -console.log(FindContinuousSequence(9)) -module.exports = { - FindContinuousSequence: FindContinuousSequence -}; \ No newline at end of file diff --git a/manuscripts/算法/后端/FindKthToTail.js b/manuscripts/算法/后端/FindKthToTail.js deleted file mode 100644 index 815867a..0000000 --- a/manuscripts/算法/后端/FindKthToTail.js +++ /dev/null @@ -1,43 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-18 21:59:54 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-18 22:00:33 - */ - - -/* - * function ListNode(x){ - * this.val = x; - * this.next = null; - * } - */ -/** - * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 - * - * - * @param pHead ListNode类 - * @param k int整型 - * @return ListNode类 - */ -function FindKthToTail (pHead, k) { - // write code here - // 结点不存在返回空 - if (!pHead) { - return null; - } - - let arr = []; - // 结点遍历,将结点存放在数组中 - while (pHead) { - arr.push(pHead) - pHead = pHead.next; - } - - return arr[arr.length - k] -} -module.exports = { - FindKthToTail: FindKthToTail -}; \ No newline at end of file diff --git a/manuscripts/算法/后端/FindNumbersWithSum.js b/manuscripts/算法/后端/FindNumbersWithSum.js deleted file mode 100644 index 9f1a8e2..0000000 --- a/manuscripts/算法/后端/FindNumbersWithSum.js +++ /dev/null @@ -1,51 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-21 21:48:10 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-21 22:11:21 - */ - - -// 注意array是递增的 -function FindNumbersWithSum (array, sum) { - // write code here - - let left = 0; - let right = array.length - 1; - - let sumReuslt = []; - // 定义无穷大Infinity 或者直接sum值的平方 - let min = Math.pow(sum, 2) - // let min=Infinity - while (left < right) { - let temp_sum = array[left] + array[right]; - if (temp_sum < sum) { - // 向右 - left++ - } else if (temp_sum > sum) { - // 向左 - right-- - } else if (temp_sum === sum) { - // 已找到符合条件的元素,需要对齐进行乘积比较 - if (min > array[left] * array[right]) { - // 假定为最小值 - min= array[left] * array[right] - sumReuslt = [array[left], array[right]] - } - // left向右 - left++ - // right向左 - right-- - } - } - // left===right 退出 - return sumReuslt - -} - -console.log(FindNumbersWithSum([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],21)) -module.exports = { - FindNumbersWithSum: FindNumbersWithSum -}; \ No newline at end of file diff --git a/manuscripts/算法/后端/GetLeastNumbers_Solution.js b/manuscripts/算法/后端/GetLeastNumbers_Solution.js deleted file mode 100644 index 63e56b8..0000000 --- a/manuscripts/算法/后端/GetLeastNumbers_Solution.js +++ /dev/null @@ -1,82 +0,0 @@ -/* - * @Description: 给定一个数组,找出其中最小的K个数。例如数组元素是4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。如果K>数组的长度,那么返回一个空的数组 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-17 23:00:02 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-17 23:27:03 - */ - - -// 基于冒泡排序 -function GetLeastNumbers_Solution (input, k) { - // write code here - - const len = input.length - - if (k > len) { - return [] - } - - // 冒泡,跑k趟,每趟就有一个元素在位置上 需要输出前k,就跑k趟 - for (let i = 0; i < k; i++) { - for (let j = len; j >i; j--) { - if (input[i] >= input[j]) { - // 元素换位置 - const temp = input[j] - input[j] = input[i]; - input[i] = temp; - } - } - } - - return input.slice(0, k) -} - -// 基于简单选择排序 -function GetLeastNumbers_Solution02 (input, k) { - // write code here - - const len = input.length - - if (k > len) { - return [] - } - - // 冒简单选择排序,每趟就有一个元素在位置上 需要输出前k,就跑k趟 - for (let i = 0; i < k; i++) { - for (let j = i + 1; j < len; j++) { - if (input[i] >= input[j]) { - // 元素换位置 - const temp = input[j] - input[j] = input[i]; - input[i] = temp; - } - } - } - - return input.slice(0, k) -} - -// 基于sort函数 -function GetLeastNumbers_Solution03(input, k) { - // write code here - // if (k > input.length) return [] - - // // 排序 - // input.sort((a,b)=>a-b) - // return input.slice(0, k) - - return k>input.length?[]: input.sort((a,b)=>a-b).slice(0,k) -} - - -console.log(GetLeastNumbers_Solution([4, 5, 1, 6, 2, 7, 3, 8], 4)) -console.log(GetLeastNumbers_Solution02([4, 5, 1, 6, 2, 7, 3, 8], 4)) -console.log(GetLeastNumbers_Solution03([4, 5, 1, 6, 2, 7, 3, 8], 4)) - -module.exports = { - GetLeastNumbers_Solution: GetLeastNumbers_Solution, - GetLeastNumbers_Solution02: GetLeastNumbers_Solution02, - GetLeastNumbers_Solution03: GetLeastNumbers_Solution03 -}; diff --git a/manuscripts/算法/后端/MoreThanHalfNum_Solution.js b/manuscripts/算法/后端/MoreThanHalfNum_Solution.js deleted file mode 100644 index 2f88fcf..0000000 --- a/manuscripts/算法/后端/MoreThanHalfNum_Solution.js +++ /dev/null @@ -1,41 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-18 11:58:01 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-18 12:07:04 - */ - - -function MoreThanHalfNum_Solution (numbers) { - - // write code here - let map = new Map(); - - numbers.forEach(item => { - if (map.has(item)) { - map.set(item, map.get(item) + 1) - } else { - map.set(item, 1) - } - }) - - const arr = [...new Set(numbers)]; - - // console.log(map,arr) - let result = 0; - - arr.map(item => { - if (2 * map.get(item) > numbers.length) { - result = item; - } - }) - - - return result; - - -} - -console.log() \ No newline at end of file diff --git a/manuscripts/算法/后端/binary_search.js b/manuscripts/算法/后端/binary_search.js deleted file mode 100644 index 017d968..0000000 --- a/manuscripts/算法/后端/binary_search.js +++ /dev/null @@ -1,110 +0,0 @@ -/* - * @Description: 二分查找 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-18 19:01:13 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-18 21:29:48 - */ - -// 参考:https://github.com/labuladong/fucking-algorithm/blob/master/%E7%AE%97%E6%B3%95%E6%80%9D%E7%BB%B4%E7%B3%BB%E5%88%97/%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E8%AF%A6%E8%A7%A3.md - -/** - * @param {number[]} nums - * @param {number} target - * @return {number} - */ - var search = function(nums, target) { - - // 投机 - // return nums.indexOf(target) - - // 二分查找 - return binary_search(nums,target) - -}; - -// 二分查找 -function binary_search(nums,target){ - let left=0,right=nums.length; - while(lefttarget){ - // 左侧 - right=mid - }else if(nums[mid]target){ - // 左侧 - right=mid-1 - } - - // 跳出循环,left=right+1 索引 - - return left===nums.length?left:-1 - - } - - - - - -} - -// 右侧部分【最后一个相同元素】,[left,right) 情况 -function right_bound(nums,target){ - let left=0,right=nums.length - // [left,right) 情况 - while(lefttarget){ - // 左侧 - right=mid - } - } - console.log(left,right); - - // left===right 跳出循环 - return nums[left-1]===target?left-1:-1 -} - - - -// console.log(search([-1,0,3,5,9,12],9)) - -// console.log(search([5,7,7,8,8,8,10],8)) - -// console.log(left_bound([5,7,7,8,8,8,10],8)) -console.log(right_bound([5,7,7,8,8,8,10],8)) \ No newline at end of file diff --git a/manuscripts/算法/后端/findKth.js b/manuscripts/算法/后端/findKth.js deleted file mode 100644 index 9329b23..0000000 --- a/manuscripts/算法/后端/findKth.js +++ /dev/null @@ -1,78 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-19 20:27:28 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-19 21:46:21 - */ - - -/** - * - * @param a int整型一维数组 - * @param n int整型 - * @param K int整型 - * @return int整型 - */ -function findKth (a, n, K) { - // write code here - - - const result = quickSort(a, 0, n - 1); - - console.log(result) - return result[K - 1] - - - -} - -function quickSort (arr, low, high) { - - // - while (low < high) { - - // 中间区分值 - let pivot = getPivot(arr, low, high); - console.log(`pivot:${pivot}`) - - // 左侧 - quickSort(arr, low, pivot - 1) - // 右侧 - quickSort(arr, pivot + 1, high) - } - return arr; -} - -function getPivot (arr, low, high) { - - let pivot = arr[low]; - - while (low < high) { - - // pivot右侧元素都比arr[pivot]值大 - while (low < high && pivot <= arr[high]) { - --high; - } - arr[low] = arr[high] - - // pivot左侧元素都比arr[pivot]值小 - while (low < high && pivot >= arr[low]) { - ++low - } - arr[high] = arr[low] - - } - arr[low] = pivot - - return low; - -} - - -console.log(findKth([1, 3, 5, 2, 2], 5, 3)) - -module.exports = { - findKth: findKth -}; \ No newline at end of file diff --git a/manuscripts/算法/后端/getLongestPalindrome.js b/manuscripts/算法/后端/getLongestPalindrome.js deleted file mode 100644 index 3bf36f2..0000000 --- a/manuscripts/算法/后端/getLongestPalindrome.js +++ /dev/null @@ -1,42 +0,0 @@ -/* - * @Description: 最长回文字符串 - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-05-20 21:06:00 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-20 21:06:17 - */ - - -/** - * 求给定字符的最大回文字符串 - * @param {string} str 字符串 - * @param {int} len 给定字符串的长度 - */ -function getLongestPalindrome(str,len){ - - - // 直接暴力 - - // 最大计数 - let max=0; - for(let i=0;iitem!==val) - -}; - - -console.log(removeElement([3,2,2,3],3)) \ No newline at end of file diff --git a/manuscripts/算法/后端/threeNum.js b/manuscripts/算法/后端/threeNum.js deleted file mode 100644 index 81bdda1..0000000 --- a/manuscripts/算法/后端/threeNum.js +++ /dev/null @@ -1,51 +0,0 @@ -/* - * @Description: - * @Version: Beta1.0 - * @Author: 【B站&公众号】Rong姐姐好可爱 - * @Date: 2021-04-21 21:33:14 - * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-21 21:38:32 - */ - - - -function threeNum(num){ - - - -} - - - -// 暴力枚举,无法通过 -function threeSum01( num ) { - // write code here - // 从小排序 - num=num.sort((a,b)=>a-b); - const len=num.length; - let result=[]; - for(let mid=1;mid left; right--) { - if (numbers[left] + numbers[right] === target) { - return [left + 1, right + 1] - } - } - } -} - -// 利用map对象来存储已经遍历的数据 - -function twoSum02 (numbers, target) { - - // 从左到右循环,进入map - let map = new Map(); - - for (let left = 0; left < numbers.length; left++) { - - - if (map.has(target - numbers[left]) && map.get(target - numbers[left]) !== (left + 1)) { - - // 则在左边找到元素 - const right = map.get(target - numbers[left]); - - console.log(map) - return [right, left + 1] - } - // 有点倒排索引的意思 - map.set(numbers[left], left + 1) - } - - -} - - -console.log(twoSum([3,2,4],6)) -console.log(twoSum02([0, 4, 3, 0], 0)) -module.exports = { - twoSum: twoSum -}; \ No newline at end of file diff --git a/package.json b/package.json index 66afe90..2246bdd 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "version": "0.0.0", "main": "index.js", "scripts": { + "prepare": "husky install && npx husky add .husky/pre-commit \"npm run lintfix\" && chmod +x .husky/pre-commit", "dev": "vuepress dev docs", "build": "vuepress build docs", "build-proxy": "PROXY_DOMAIN=true vuepress build docs", @@ -11,7 +12,9 @@ "faster-image": "bash scripts/build_image.sh $npm_package_version faster", "ali": "bash scripts/deploy.sh ali $npm_package_version", "github": "bash scripts/page_deploy.sh", - "clean": "find . -name \"node_modules\" -type d -exec rm -rf '{}' +" + "clean": "find . -name \"node_modules\" -type d -exec rm -rf '{}' +", + "lint": "eslint --ext .js,.vue,.ts --ignore-path .gitignore .", + "lintfix": "eslint --fix --ext .js,.ts,.vue --ignore-path .gitignore ." }, "author": { "name": "Chu Fan",