mirror of
https://github.com/142vip/408CSFamily.git
synced 2026-04-10 05:59:18 +08:00
5.1 coding update
This commit is contained in:
BIN
算法/前端/.DS_Store
vendored
Normal file
BIN
算法/前端/.DS_Store
vendored
Normal file
Binary file not shown.
@@ -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 参数
|
||||
- 数组去重
|
||||
- 设置文字颜色
|
||||
- 模块
|
||||
|
||||
|
||||
|
||||
|
||||
BIN
算法/前端/code/.DS_Store
vendored
Normal file
BIN
算法/前端/code/.DS_Store
vendored
Normal file
Binary file not shown.
46
算法/前端/code/add.js
Normal file
46
算法/前端/code/add.js
Normal file
@@ -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))
|
||||
|
||||
31
算法/前端/code/count.js
Normal file
31
算法/前端/code/count.js
Normal file
@@ -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<arr.length;index++){
|
||||
const value=arr[index];
|
||||
let count=0;
|
||||
if(value!==' '){
|
||||
originArr.map(item=>{
|
||||
if(item===value){
|
||||
count++
|
||||
}
|
||||
})
|
||||
// 对象计数
|
||||
result[value]=count
|
||||
}
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
30
算法/前端/code/duplicates.js
Normal file
30
算法/前端/code/duplicates.js
Normal file
@@ -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;index<len-1;index++){
|
||||
|
||||
if(sortArr[index]===sortArr[index++]){
|
||||
result.push(sortArr[index])
|
||||
}
|
||||
}
|
||||
|
||||
// 去重
|
||||
return [...new Set(result)];
|
||||
}
|
||||
|
||||
console.log(duplicates([1, 2, 4, 4, 3, 3, 1, 5, 3]))
|
||||
46
算法/前端/code/isUSD.js
Normal file
46
算法/前端/code/isUSD.js
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-21 07:15:31
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-21 12:01:57
|
||||
*/
|
||||
|
||||
|
||||
function isUSD (str) {
|
||||
|
||||
if (!str.startsWith('$')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (str.indexOf('.')) {
|
||||
// 小数
|
||||
let arr = str.split('.');
|
||||
|
||||
if (arr[1].length !== 2) {
|
||||
return false;
|
||||
};
|
||||
|
||||
let strArr = arr[0].split(',');
|
||||
|
||||
for (let index = 0; index < strArr.length; index++) {
|
||||
if (strArr[index].length !== 3) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
let strArr = str.split(',');
|
||||
|
||||
for (let index = 0; index < strArr.length; index++) {
|
||||
if (strArr[index].length !== 3) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
console.log(isUSD('$20,933,209.93'))
|
||||
44
算法/前端/code/removeWithoutCopy.js
Normal file
44
算法/前端/code/removeWithoutCopy.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* @Description: 【FED19】 移除数组中的元素
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-13 22:47:38
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-14 10:23:19
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* 移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回
|
||||
* @param {*} arr
|
||||
* @param {*} item
|
||||
* @returns
|
||||
*/
|
||||
function removeWithoutCopy(arr, item) {
|
||||
|
||||
// const result= arr.filter(value=>value!==item)
|
||||
// // 输出
|
||||
// return result;
|
||||
|
||||
// 每次都和arr中的首个元素去比较
|
||||
|
||||
const len=arr.length;
|
||||
|
||||
for(let index=0;index<len;index++){
|
||||
if(arr[0]!==item){
|
||||
arr.push(arr[0])
|
||||
}
|
||||
|
||||
// 删除第一个
|
||||
arr.shift()
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
const test = [1, 2, 2, 3, 4, 2, 2]
|
||||
console.log(removeWithoutCopy(test,2))
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-27 08:39:46
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-29 22:09:29
|
||||
* @LastEditTime: 2021-04-30 22:15:20
|
||||
-->
|
||||
|
||||
|
||||
@@ -36,10 +36,11 @@
|
||||
|
||||
### 双指针
|
||||
|
||||
- 【中等】和为S的两个数字
|
||||
- 和为S的连续正数序列
|
||||
- 翻转单词顺序列
|
||||
- 左旋转字符串
|
||||
- [【中等】和为S的两个数字](./双指针/FindNumbersWithSum.js)
|
||||
- [【中等】和为S的连续正数序列](./双指针/FindContinuousSequence.js)
|
||||
- [【中等】左旋转字符串](./双指针/LeftRotateString.js)
|
||||
- [【较难】翻转单词顺序列](./双指针/ReverseSentence.js)
|
||||
|
||||
|
||||
|
||||
### 链表
|
||||
|
||||
52
算法/剑指/双指针/FindContinuousSequence.js
Normal file
52
算法/剑指/双指针/FindContinuousSequence.js
Normal file
@@ -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
|
||||
};
|
||||
76
算法/剑指/双指针/FindNumbersWithSum.js
Normal file
76
算法/剑指/双指针/FindNumbersWithSum.js
Normal file
@@ -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
|
||||
};
|
||||
50
算法/剑指/双指针/LeftRotateString.js
Normal file
50
算法/剑指/双指针/LeftRotateString.js
Normal file
@@ -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<n){
|
||||
return ''
|
||||
}
|
||||
// 对k以前的进行翻转
|
||||
const firstStr = str.slice(0, n);
|
||||
// 对k以后的字符串进行翻转
|
||||
const secondStr = str.slice(n);
|
||||
// 对拼接后的字符串进行翻转
|
||||
return reverseStr(`${reverseStr(firstStr)}${reverseStr(secondStr)}`)
|
||||
}
|
||||
|
||||
// 偷懒做法
|
||||
function LeftRotateString01 (str, n) {
|
||||
|
||||
// 两两翻转后,在统一翻转
|
||||
return `${str.slice(0, n).split('').reverse().join('')}${str.slice(n).split('').reverse().join('')}`.split('').reverse().join('')
|
||||
}
|
||||
|
||||
/**
|
||||
* 旋转数组,交换
|
||||
* @param {string} str
|
||||
* @returns
|
||||
*/
|
||||
function reverseStr (str) {
|
||||
let result = str.split('')
|
||||
let left = 0, right = result.length - 1
|
||||
while (left <= right) {
|
||||
// 临时值 元素交换
|
||||
[result[left], result[right]] = [result[right], result[left]]
|
||||
left++;
|
||||
right--
|
||||
}
|
||||
return result.join('');
|
||||
}
|
||||
console.log(LeftRotateString('',6))
|
||||
module.exports = {
|
||||
LeftRotateString: LeftRotateString
|
||||
};
|
||||
54
算法/剑指/双指针/ReverseSentence.js
Normal file
54
算法/剑指/双指针/ReverseSentence.js
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* @Description: 翻转单词顺序列
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-30 21:40:57
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-30 21:54:47
|
||||
*/
|
||||
|
||||
// 偷懒做法
|
||||
function ReverseSentence (str) {
|
||||
// write code here
|
||||
return str.split(' ').reverse().join(' ');
|
||||
|
||||
}
|
||||
|
||||
// 先将每个字符翻转 再将所有翻转
|
||||
// nowcoder. a am I ---> .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
|
||||
};
|
||||
Reference in New Issue
Block a user