From 8daf08fcb1afa665bc75a49a21ea54258c6d75bb Mon Sep 17 00:00:00 2001 From: mmdapl <2237221210@qq.com> Date: Sun, 9 May 2021 11:48:23 +0800 Subject: [PATCH] 5.9 coding update --- README.md | 26 +++--- 算法/剑指/Readme.md | 26 +++--- 算法/剑指/位运算/FindNumsAppearOnce.js | 87 ++++++++++++++++++++ 算法/剑指/位运算/NumberOf1.js | 30 +++++++ 算法/剑指/其他相关/Add.js | 32 +++++++ 算法/剑指/其他相关/IsContinuous.js | 52 ++++++++++++ 算法/剑指/其他相关/StrToInt.js | 23 ++++++ 算法/剑指/动态规划/GetUglyNumber_Solution.js | 47 +++++++++++ 8 files changed, 291 insertions(+), 32 deletions(-) create mode 100644 算法/剑指/位运算/FindNumsAppearOnce.js create mode 100644 算法/剑指/位运算/NumberOf1.js create mode 100644 算法/剑指/其他相关/Add.js create mode 100644 算法/剑指/其他相关/IsContinuous.js create mode 100644 算法/剑指/其他相关/StrToInt.js create mode 100644 算法/剑指/动态规划/GetUglyNumber_Solution.js diff --git a/README.md b/README.md index e6603ec..d50984c 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ - [【中等】字符流中的第一个不重复的字符](./算法/剑指/栈队列堆/FirstAppearingOnce.js) - [【较难】滑动窗口的最大值](./算法/剑指/栈队列堆/maxInWindows.js) - [【较难】包含min函数的栈](./算法/剑指/栈队列堆/GetMinInJSStack.js) -- 栈的压入、弹出序列 +- ~~栈的压入、弹出序列~~ @@ -202,8 +202,8 @@ - [【较难】从尾到头打印链表](./算法/剑指/链表/printListFromTailToHead.js) - ~~在O(1)时间内删除链表节点~~ - [【较难】删除链表中重复的结点](./算法/剑指/链表/deleteDuplication.js) -- 链表中环的入口结点[暂时没思路] -- 【较难】复杂链表的复制[暂时没有思路] +- ~~链表中环的入口结点[暂时没思路]~~ +- ~~【较难】复杂链表的复制[暂时没有思路]~~ @@ -266,7 +266,7 @@ - [【简单】构建乘积数组](./算法/剑指/动态规划/multiply.js) - [【中等】矩形覆盖](./算法/剑指/动态规划/rectCover.js) - [【中等】跳台阶 非递归,要么跳一阶,要么跳两阶](./算法/剑指/动态规划/jumpFloor.js) -- 丑数 +- [【较难】丑数](./算法/剑指/动态规划/GetUglyNumber_Solution.js) @@ -277,20 +277,14 @@ - [【中等】从1到n整数中1出现的次数](./算法/剑指/数学/NumberOf1Between1AndN_Solution.js) #### 位运算 -- 二进制中1的个数 -- 数组中只出现一次的数字 +- [【中等】二进制中1的个数](./算法/剑指/位运算/NumberOf1.js) +- [【中等】数组中只出现一次的数字](./算法/剑指/位运算/FindNumsAppearOnce.js) -### 其他分类 +#### 其他分类 -- 打印从 1 到最大的 n 位数 -- 正则表达式匹配 -- 表示数值的字符串 -- 数字序列中的某一位数字 -- 把数字翻译成字符串 -- 扑克牌顺子 -- 求 1+2+3+...+n -- 不用加减乘除做加法 -- 把字符串转换成整数 +- [【简单】不用加减乘除做加法](./算法/剑指/其他相关/Add.js) +- [【中等】扑克牌顺子](./算法/剑指/其他相关/IsContinuous.js) +- [【较难】把字符串转换成整数](./算法/剑指/其他相关/StrToInt.js) diff --git a/算法/剑指/Readme.md b/算法/剑指/Readme.md index b5b3ecb..bbe4fc1 100644 --- a/算法/剑指/Readme.md +++ b/算法/剑指/Readme.md @@ -4,7 +4,7 @@ * @Author: 【B站&公众号】Rong姐姐好可爱 * @Date: 2021-04-27 08:39:46 * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-05-05 12:55:06 + * @LastEditTime: 2021-05-09 11:40:55 --> @@ -116,29 +116,23 @@ - [【简单】构建乘积数组](./动态规划/multiply.js) - [【中等】矩形覆盖](./动态规划/rectCover.js) - [【中等】跳台阶 非递归,要么跳一阶,要么跳两阶](./动态规划/jumpFloor.js) -- 丑数 +- [【较难】丑数](./动态规划/GetUglyNumber_Solution.js) #### 数学问题 -- 数组中出现次数超过一半的数字 -- 圆圈中最后剩下的数 -- 从1到n整数中1出现的次数 +- [【简单】数组中出现次数超过一半的数字](./数学/MoreThanHalfNum_Solution.js) +- [【中等】圆圈中最后剩下的数 约瑟夫问题](./数学/LastRemaining_Solution.js) +- [【中等】从1到n整数中1出现的次数](./数学/NumberOf1Between1AndN_Solution.js) #### 位运算 -- 二进制中1的个数 -- 数组中只出现一次的数字 +- [【中等】二进制中1的个数](./位运算/NumberOf1.js) +- [【中等】数组中只出现一次的数字]() ### 其他分类 -- 打印从 1 到最大的 n 位数 -- 正则表达式匹配 -- 表示数值的字符串 -- 数字序列中的某一位数字 -- 把数字翻译成字符串 -- 扑克牌顺子 -- 求 1+2+3+...+n -- 不用加减乘除做加法 -- 把字符串转换成整数 \ No newline at end of file +- [【简单】不用加减乘除做加法](./其他相关/Add.js) +- [【中等】扑克牌顺子](./其他相关/IsContinuous.js) +- [【较难】把字符串转换成整数](./其他相关/StrToInt.js) \ No newline at end of file diff --git a/算法/剑指/位运算/FindNumsAppearOnce.js b/算法/剑指/位运算/FindNumsAppearOnce.js new file mode 100644 index 0000000..4d6101f --- /dev/null +++ b/算法/剑指/位运算/FindNumsAppearOnce.js @@ -0,0 +1,87 @@ +/* + * @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/算法/剑指/位运算/NumberOf1.js b/算法/剑指/位运算/NumberOf1.js new file mode 100644 index 0000000..fb4f38e --- /dev/null +++ b/算法/剑指/位运算/NumberOf1.js @@ -0,0 +1,30 @@ +/* + * @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/算法/剑指/其他相关/Add.js b/算法/剑指/其他相关/Add.js new file mode 100644 index 0000000..3c13c78 --- /dev/null +++ b/算法/剑指/其他相关/Add.js @@ -0,0 +1,32 @@ +/* + * @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/算法/剑指/其他相关/IsContinuous.js b/算法/剑指/其他相关/IsContinuous.js new file mode 100644 index 0000000..8a806ac --- /dev/null +++ b/算法/剑指/其他相关/IsContinuous.js @@ -0,0 +1,52 @@ +/* + * @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/算法/剑指/其他相关/StrToInt.js b/算法/剑指/其他相关/StrToInt.js new file mode 100644 index 0000000..745ad2d --- /dev/null +++ b/算法/剑指/其他相关/StrToInt.js @@ -0,0 +1,23 @@ +/* + * @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/算法/剑指/动态规划/GetUglyNumber_Solution.js b/算法/剑指/动态规划/GetUglyNumber_Solution.js new file mode 100644 index 0000000..d9e71c4 --- /dev/null +++ b/算法/剑指/动态规划/GetUglyNumber_Solution.js @@ -0,0 +1,47 @@ +/* + * @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