diff --git a/.DS_Store b/.DS_Store index c13e1a3..431c206 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/算法/前端/.DS_Store b/算法/前端/.DS_Store new file mode 100644 index 0000000..803a098 Binary files /dev/null and b/算法/前端/.DS_Store differ diff --git a/算法/前端/Readme.md b/算法/前端/Readme.md index 572e21f..c9cbc6a 100644 --- a/算法/前端/Readme.md +++ b/算法/前端/Readme.md @@ -4,7 +4,7 @@ * @Author: 【B站&公众号】Rong姐姐好可爱 * @Date: 2021-04-29 07:33:14 * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-29 07:41:15 + * @LastEditTime: 2021-04-30 23:19:21 --> @@ -48,14 +48,51 @@ - 批量改变对象的属性 - 判断是否包含数字 - 判断是否以元音字母结尾 + + ### 简单 +- 获取字符串的长度 +- 段落标识 +- 查找数组元素位置 +- 移除数组中的元素 +- 添加元素 +- 添加元素 +- 正确的函数定义 +- 返回函数 +- 使用 apply 调用函数 +- 二次封装函数 +- 二进制转换 +- 二进制转换 +- 属性遍历 +- 检查重复字符串 +- 获取指定字符串 +- 判断是否符合指定格式 + + ### 中等 +- 修改 this 指向 +- 时间格式化输出 +- 邮箱字符串判断 +- 颜色字符串转换 +- 将字符串转换为驼峰格式 +- 加粗文字 +- 移除数组中的元素 +- 查找重复元素 +- 计时器 +- 流程控制 +- 使用闭包 +- 判断是否符合 USD 格式 + ### 较难 +- 获取 url 参数 +- 数组去重 +- 设置文字颜色 +- 模块 diff --git a/算法/前端/code/.DS_Store b/算法/前端/code/.DS_Store new file mode 100644 index 0000000..628f44d Binary files /dev/null and b/算法/前端/code/.DS_Store differ diff --git a/算法/前端/code/add.js b/算法/前端/code/add.js new file mode 100644 index 0000000..60b11b8 --- /dev/null +++ b/算法/前端/code/add.js @@ -0,0 +1,46 @@ +/* + * @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/算法/前端/code/count.js b/算法/前端/code/count.js new file mode 100644 index 0000000..130b82d --- /dev/null +++ b/算法/前端/code/count.js @@ -0,0 +1,31 @@ +/* + * @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/算法/前端/code/duplicates.js b/算法/前端/code/duplicates.js new file mode 100644 index 0000000..c6a6700 --- /dev/null +++ b/算法/前端/code/duplicates.js @@ -0,0 +1,30 @@ +/* + * @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 @@ -36,10 +36,11 @@ ### 双指针 -- 【中等】和为S的两个数字 -- 和为S的连续正数序列 -- 翻转单词顺序列 -- 左旋转字符串 +- [【中等】和为S的两个数字](./双指针/FindNumbersWithSum.js) +- [【中等】和为S的连续正数序列](./双指针/FindContinuousSequence.js) +- [【中等】左旋转字符串](./双指针/LeftRotateString.js) +- [【较难】翻转单词顺序列](./双指针/ReverseSentence.js) + ### 链表 diff --git a/算法/剑指/双指针/FindContinuousSequence.js b/算法/剑指/双指针/FindContinuousSequence.js new file mode 100644 index 0000000..e59aeb6 --- /dev/null +++ b/算法/剑指/双指针/FindContinuousSequence.js @@ -0,0 +1,52 @@ +/* + * @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/算法/剑指/双指针/FindNumbersWithSum.js b/算法/剑指/双指针/FindNumbersWithSum.js new file mode 100644 index 0000000..b2c552c --- /dev/null +++ b/算法/剑指/双指针/FindNumbersWithSum.js @@ -0,0 +1,76 @@ +/* + * @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/算法/剑指/双指针/LeftRotateString.js b/算法/剑指/双指针/LeftRotateString.js new file mode 100644 index 0000000..ba826c2 --- /dev/null +++ b/算法/剑指/双指针/LeftRotateString.js @@ -0,0 +1,50 @@ +/* + * @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