private前端常见算法
1.数组
合并两个有序数组(简单-5)
https://leetcode.cn/problems/merge-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150
移除元素(简单-4)
https://leetcode.cn/problems/remove-element/description/?envType=study-plan-v2&envId=top-interview-150
删除有序数组重复项(简单-5)
https://leetcode.cn/problems/remove-duplicates-from-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150
删除有序数组重复项2(中等-3)
https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/description/?envType=study-plan-v2&envId=top-interview-150
买股票最佳时机(简单-4)
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-interview-150
买股票最佳时机2(中等-3)
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/?envType=study-plan-v2&envId=top-interview-150
罗马数字转整数(中等-4)
https://leetcode.cn/problems/roman-to-integer/description/?envType=study-plan-v2&envId=top-interview-150
最后一个单词的长度(简单-5)
https://leetcode.cn/problems/length-of-last-word/description/?envType=study-plan-v2&envId=top-interview-150
最长公共前缀(简单-5)
https://leetcode.cn/problems/longest-common-prefix/description/?envType=study-plan-v2&envId=top-interview-150
找出字符串中第一个匹配项的下标
https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/description/?envType=study-plan-v2&envId=top-interview-150
2.双指针
验证回文串 (中等-5)
https://leetcode.cn/problems/valid-palindrome/description/?envType=study-plan-v2&envId=top-interview-150

//方法1
var isPalindrome = function(s) {let exp=[' ',',',':',]let str=''for (const e of s) {if(!exp.includes(e)){str+=e}}str=str.toLowerCase()let revers=str.split('').reverse().join('')console.log(str,revers);if(str===revers){return true}else{return false}
};
console.log(isPalindrome("A man, a plan, a canal: Panama"));/双指针
//方法2
var isPalindrome = function(s) {s = s.replace(/([^a-zA-Z0-9])/g, '');for (let i = 0, j = s.length - 1; i < j; i++,j--) {if (s[i].toLocaleLowerCase() !== s[j].toLocaleLowerCase()) {return false;}}return true;
}//方法3
var isPalindrome = function (s) {s = s.replace(/[\W|_]/g, "").toLowerCase();if (s.length < 2) {return true;}let left = 0;let right = s.length - 1;while (left < right) {if (s[left] !== s[right]) {//对撞指针判断左右两边是否是相同的字符return false;}left++;right--;}return true;
}
三数之和(中等-5)
https://leetcode.cn/problems/3sum/description/?envType=study-plan-v2&envId=top-interview-150

var threeSum = function(nums) {if(!nums) return []let arr=[]let sort=nums.sort((a,b)=>a-b)for(var i=0;i<sort.length;i++){var L=i+1var R=sort.length-1while(L<R){var sum=sort[i]+sort[L]+sort[R]if(sum==0){//去重判断if(!arr.includes([sort[i],sort[L],sort[R]])){arr.push([sort[i],sort[L],sort[R]]) }L++R--}if(sum<0){L++}if(sum>0){R--}}}return arr
};console.log(threeSum([1,2,3,4,-2,-3,0]));
盛水最多的容器(中等-5)
https://leetcode.cn/problems/container-with-most-water/description/?envType=study-plan-v2&envId=top-interview-150
3.滑动窗口
长度最小的子数组(中等-4)
https://leetcode.cn/problems/minimum-size-subarray-sum/description/?envType=study-plan-v2&envId=top-interview-150
无重复字符的最长子串(中等-4)
https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/?envType=study-plan-v2&envId=top-interview-150
4.矩阵
旋转图像 (中等-3)
https://leetcode.cn/problems/rotate-image/description/?envType=study-plan-v2&envId=top-interview-150
5.哈希表
两数之和(简单-5)
https://leetcode.cn/problems/two-sum/description/?envType=study-plan-v2&envId=top-interview-150
最长连续序列(中等-5)
https://leetcode.cn/problems/longest-consecutive-sequence/description/?envType=study-plan-v2&envId=top-interview-150
6. 栈
有效括号(简单-5)
https://leetcode.cn/problems/valid-parentheses/description/?envType=study-plan-v2&envId=top-interview-150
简化路径(中等-4)
https://leetcode.cn/problems/simplify-path/description/?envType=study-plan-v2&envId=top-interview-150
7. 链表
环形链表(简单-5)
https://leetcode.cn/problems/linked-list-cycle/description/?envType=study-plan-v2&envId=top-interview-150
旋转链表(中等-4)
https://leetcode.cn/problems/rotate-list/description/?envType=study-plan-v2&envId=top-interview-150
K 个一组翻转链表(中等-4)
https://leetcode.cn/problems/reverse-nodes-in-k-group/description/?envType=study-plan-v2&envId=top-interview-150
8.二叉树
二叉树最大深度(中等-3)
https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/?envType=study-plan-v2&envId=top-interview-150
翻转二叉树(中等-4)
https://leetcode.cn/problems/invert-binary-tree/description/?envType=study-plan-v2&envId=top-interview-150
二叉树层序遍历(中等-4)
https://leetcode.cn/problems/binary-tree-level-order-traversal/description/?envType=study-plan-v2&envId=top-interview-150
9.回溯
电话号码的字母组合(中等-3)
https://leetcode.cn/problems/letter-combinations-of-a-phone-number/description/?envType=study-plan-v2&envId=top-interview-150
10.二分查找
搜索插入位置(中等-3)
https://leetcode.cn/problems/search-insert-position/description/?envType=study-plan-v2&envId=top-interview-150
11.堆
数组中的第 K 个最大元素(中等-3)
https://leetcode.cn/problems/xx4gT2/description/
12.位运算
二进制求和(简单-4)
https://leetcode.cn/problems/add-binary/description/?envType=study-plan-v2&envId=top-interview-150
只出现一次的数字(简单-3)
https://leetcode.cn/problems/single-number/description/?envType=study-plan-v2&envId=top-interview-150
13. 数学
回文数(简单-5)
https://leetcode.cn/problems/palindrome-number/description/?envType=study-plan-v2&envId=top-interview-150
x的平方根(简单-3)
https://leetcode.cn/problems/sqrtx/description/?envType=study-plan-v2&envId=top-interview-150
14.动态规划
爬楼梯 (简单-5)
https://leetcode.cn/problems/climbing-stairs/description/?envType=study-plan-v2&envId=top-interview-150
零钱兑换 (中等-4)
https://leetcode.cn/problems/coin-change/description/?envType=study-plan-v2&envId=top-interview-150
最长回文串(中等-4)
https://leetcode.cn/problems/longest-palindromic-substring/description/?envType=study-plan-v2&envId=top-interview-150
15.其他
扁平化转树形结构
const data = [// 每项主要由id和name组成,而pid指向其父节点的id// 如果是树节点则对应的pid为空{ id: '01', name: '中国', pid: ''},{ id: '02', name: '北京市', pid: '01'},{ id: '03', name: '海淀区', pid: '02'},{ id: '04', name: '丰台区', pid: '02'},{ id: '05', name: '朝阳区', pid: '02'},{ id: '06', name: '重庆市', pid: '01'},{ id: '07', name: '渝中区', pid: '06'},{ id: '08', name: '江北区', pid: '06'},{ id: '09', name: '四川省', pid: '01'},{ id: '10', name: '成都市', pid: '09'},{ id: '11', name: '成华区', pid: '10'},{ id: '12', name: '武侯区', pid: '10'}
];function transNormalToTree(array) {let result = [] //最终存储的结果// 对数组中的每个对象进行遍历array.forEach( item => {// 判断当前节点是否为根节点,如果是则pushif (!item.pid) {result.push(item)}// 将所有pid等于当前id的对象储存在children数组中let childArray = array.filter(data => data.pid === item.id)// 判断当前节点是否为叶节点,如果是则退出if (!childArray.length) {return }// 将符合条件的子数组赋值给当前项的child属性item.child = childArray})return result
}console.log(transNormalToTree(data))
实现事件订阅机制
/**
说明:简单实现一个事件订阅机制,具有监听on和触发emit方法
示例:
const event = new EventEmitter();
event.on('someEvent', (...args) => {
console.log('some_event triggered', ...args);
});
event.emit('someEvent', 'abc', '123');
// class EventEmitter { /* 功能实现 */
实现
class EventEmitter {constructor() {this.events = {}; // 存储事件及其对应的回调函数}// 监听事件on(eventName, callback) {if (!this.events[eventName]) {this.events[eventName] = []; // 如果事件不存在,创建一个空数组}this.events[eventName].push(callback); // 将回调函数添加到事件的回调数组中}// 触发事件emit(eventName, ...args) {if (this.events[eventName]) {this.events[eventName].forEach(callback => {callback(...args); // 调用每个回调函数,并传递参数});}}
}// 示例使用
const event = new EventEmitter();event.on('someEvent', (...args) => {console.log('some_event triggered', ...args);
});event.emit('someEvent', 'abc', '123');
实现一个休眠函数
/** :实现一个函数 使当前运行的异步操作(promise 或者 async)停止等待若干秒 */
const sleep = (ms) => {// 请补充
}(async () => { console.log('hello');// 等待两秒 await sleep(20000);console.log('world');
})()
const sleep = (ms: number) => {return new Promise(resolve => setTimeout(resolve, ms));
};(async () => {console.log('hello');await sleep(2000); // 等待两秒console.log('world');
})();
解析url
/** * 编辑试题描述 // 实现一个方法,拆解URL参数中queryString * // 入参格式参考: const url = 'http://sample.com/?a=1&b=2&c=xx&d#hash'; * const params = { a: '5', e: '6'};
// 出参格式参考: const result = { a: '5', b: '2', c: 'xx', d: '', e: '6' };
// 拆解URL参数中queryString,返回一个 key - value 形式的 object function querySearch(url, params) { // 在这里写代码}*/
function querySearch(url: string, params: { [key: string]: string }) {const result: { [key: string]: string } = { ...params };const queryString = url.split('?')[1]?.split('#')[0] || '';const queryParams = new URLSearchParams(queryString);queryParams.forEach((value, key) => {result[key] = value;});return result;
}// 示例
const url = 'http://sample.com/?a=1&b=2&c=xx&d#hash';
const params = { a: '5', e: '6' };
const result = querySearch(url, params);
console.log(result); // { a: '5', b: '2', c: 'xx', d: '', e: '6' }
金额处理
/**
实现金额千位分隔符,用法如下
parseToMoney(1234.56); // return '1,234.56'
parseToMoney(123456789); // return '123,456,789'
parseToMoney(1087654.321); // return '1,087,654.321'
*/
//正则
function parseToMoney(num: number): string {const [integerPart, decimalPart] = num.toString().split('.');const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');return decimalPart ? `${formattedInteger}.${decimalPart}` : formattedInteger;
}//不用正则
function parseToMoney(num: number): string {const [integerPart, decimalPart] = num.toString().split('.');const reverseInteger = integerPart.split('').reverse().join('');let formattedInteger = '';for (let i = 0; i < reverseInteger.length; i++) {if (i > 0 && i % 3 === 0) {formattedInteger += ',';}formattedInteger += reverseInteger[i];}formattedInteger = formattedInteger.split('').reverse().join('');return decimalPart ? `${formattedInteger}.${decimalPart}` : formattedInteger;
}// 示例
console.log(parseToMoney(1234.56)); // '1,234.56'
console.log(parseToMoney(123456789)); // '123,456,789'
console.log(parseToMoney(1087654.321)); // '1,087,654.321'
扁平化数组
/** 数组扁平化处理,并排序var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]; // newArr = [1, 2, 3, 4, 5, 6, 7 ,8, 9, 10, 11, 12, 13, 14];const newArr = myFlatten(arr) console.log(newArr)*/
function myFlatten (req) { // 请补充
}
function myFlatten(arr: any[]): number[] {return arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? myFlatten(val) : val), []).sort((a, b) => a - b);
}// 示例
const arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];
const newArr = myFlatten(arr);
console.log(newArr); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
相关文章:
private前端常见算法
1.数组 合并两个有序数组(简单-5) https://leetcode.cn/problems/merge-sorted-array/description/?envTypestudy-plan-v2&envIdtop-interview-150 移除元素(简单-4) https://leetcode.cn/problems/remove-element/descr…...
Go语言之十条命令(The Ten Commands of Go Language)
Go语言之十条命令 Go语言简介 Go语言(又称Golang)是由Google开发的一种开源编程语言,首次公开发布于2009年。Go语言旨在提供简洁、高效、可靠的软件开发解决方案,特别强调并发编程和系统编程。 Go语言的基本特征 静态强类…...
Residency 与 Internship 的区别及用法解析
Residency 与 Internship 的区别及用法解析 在英文中,“residency” 和 “internship” 都与职业培训相关,但它们的使用场景和具体含义存在显著差异。本文将详细解析这两个词的区别,以及它们在不同语境下的应用。 Residency 的定义及使用场景…...
成品电池综合测试仪:电子设备性能与安全的守护者|鑫达能
在现代科技和工业领域,电池作为能量储存和转换的关键组件,其性能的稳定性和可靠性至关重要。为了确保电池在各种应用场景中都能发挥最佳性能,成品电池综合测试仪应运而生。这一设备不仅能够对电池的各项性能指标进行全面、准确的检测…...
Taro地图组件和小程序定位
在 Taro 中使用腾讯地图 1.首先在项目配置文件 project.config.json 中添加权限: {"permission": {"scope.userLocation": {"desc": "你的位置信息将用于小程序位置接口的效果展示"}} }2.在 app.config.ts 中配置&#x…...
深入了解 SSL/TLS 协议及其工作原理
深入了解 SSL/TLS 协议及其工作原理 一. 什么是 SSL/TLS?二. SSL/TLS 握手过程三. SSL/TLS 数据加密与传输四. 总结 点个免费的赞和关注,有错误的地方请指出,看个人主页有惊喜。 作者:神的孩子都在歌唱 一. 什么是 SSL/TLS? 安全套接层&am…...
【计算机操作系统:二、操作系统的结构和硬件支持】
第2章 操作系统的结构和硬件支持 2.1 操作系统虚拟机 操作系统虚拟机是一种通过软件技术对硬件资源进行抽象和虚拟化的机制,使用户能够以逻辑方式访问和使用计算机资源。 定义与概念: 虚拟机是操作系统虚拟化技术的核心产物,通过模拟硬件资…...
51单片机——步进电机模块
直流电机没有正负之分,在两端加上直流电就能工作 P1.0-P1.3都可以控制电机,例如:使用P1.0,则需要把线接在J47的1(VCC)和2(OUT1)上 1、直流电机实验 要实现的功能是:直…...
当算法遇到线性代数(四):奇异值分解(SVD)
SVD分解的理论与应用 线性代数系列相关文章(置顶) 1.当算法遇到线性代数(一):二次型和矩阵正定的意义 2.当算法遇到线性代数(二):矩阵特征值的意义 3.当算法遇到线性代数࿰…...
SASS 简化代码开发的基本方法
概要 本文以一个按钮开发的实例,介绍如何使用SASS来简化CSS代码开发的。 代码和实现 我们希望通过CSS开发下面的代码样式,从样式来看,每个按钮的基本样式相同,就是颜色不同。 如果按照传统的方式开发,需要开发btn &…...
40.TryParse尝试转化为int类型 C#例子
也许这个时候学有点晚,但是不管怎样都学了 尝试转化,不能转化就返回bool类型的假 它会直接给括号里面的int类型赋值 代码: using System; using System.Timers; public class Program {static void Main(){int a;bool i;while (true){Get…...
【微服务】2、网关
Spring Cloud微服务网关技术介绍 单体项目拆分微服务后的问题 服务地址问题:单体项目端口固定(如黑马商城为8080),拆分微服务后端口各异(如购物车808、商品8081、支付8086等)且可能变化,前端难…...
红队-shell编程篇(上)
声明 通过学习 泷羽sec的个人空间-泷羽sec个人主页-哔哩哔哩视频,做出的文章如涉及侵权马上删除文章 笔记的只是方便各位师傅学习知识,以下网站只涉及学习内容,其他的都与本人无关,切莫逾越法律红线,否则后果自负 一、建立Shell文件 1. Shell简介 Shell是一种命令行界面&am…...
电子价签会是零售界的下一个主流?【新立电子】
电子价签,作为一种能够替代传统纸质标签的数字显示屏,已经在零售行业中展现出其巨大的潜力。它具有实时更新、集中管理、高效节能的特点,实现价格的实时更新,大大减少更新价格的工作量和时间。为消费者带来更加便捷、准确的购物体…...
5 分布式ID
这里讲一个比较常用的分布式防重复的ID生成策略,雪花算法 一个用户体量比较大的分布式系统必然伴随着分表分库,分机房部署,单体的部署方式肯定是承载不了这么大的体量。 雪花算法的结构说明 如下图所示: 雪花算法组成 从上图我们可以看…...
SpringBoot | @Autowired 和 @Resource 的区别及原理分析
关注:CodingTechWork 引言 在Spring框架中,Autowired 和 Resource 是两种常用的依赖注入注解,它们都用于自动装配Bean,简化了开发者手动创建和管理Bean的繁琐工作。然而,它们的实现机制和使用方式有所不同。理解这两者…...
『SQLite』解释执行(Explain)
摘要:本节主要讲解SQL的解释执行:Explain。 在 sqlite 语句之前,可以使用 “EXPLAIN” 关键字或 “EXPLAIN QUERY PLAN” 短语,用于描述表查询的细节。 基本语法 EXPLAIN 语法: EXPLAIN [SQLite Query]EXPLAIN QUER…...
0基础学前端-----CSS DAY12
视频参考:B站Pink老师 今天是CSS学习的第十二天,今天开始的笔记对应Pink老师课程中的CSS第七天的内容。 本节重点:CSS高级技巧 本章目录 本节目标1. 精灵图1.1 为什么需要精灵图1.2 精灵图使用案例:拼出自己的名字 2. 字体图标2.…...
(概率论)无偏估计
参考文章:(15 封私信 / 51 条消息) 什么是无偏估计? - 知乎 (zhihu.com) 首先,第一个回答中,马同学图解数学讲解得很形象, 我的概括是:“注意,有一个总体的均值u。然后,如果抽样n个&…...
Minio-Linux-安装
文章目录 1.Linux安装1.下载源码包2.上传到/usr/local/minio1.进入目录2.上传 3.开放执行权限4.创建minio文件存储目录及日志目录5.编写启动的shell脚本1.脚本编写2.赋予执行权限 6.启动!1.执行run脚本2.查看日志3.开放9001和9000端口1.服务器2.安全组3.访问&#x…...
css实现圆环展示百分比,根据值动态展示所占比例
代码如下 <view class""><view class"circle-chart"><view v-if"!!num" class"pie-item" :style"{background: conic-gradient(var(--one-color) 0%,#E9E6F1 ${num}%),}"></view><view v-else …...
Java - Mysql数据类型对应
Mysql数据类型java数据类型备注整型INT/INTEGERint / java.lang.Integer–BIGINTlong/java.lang.Long–––浮点型FLOATfloat/java.lang.FloatDOUBLEdouble/java.lang.Double–DECIMAL/NUMERICjava.math.BigDecimal字符串型CHARjava.lang.String固定长度字符串VARCHARjava.lang…...
1.3 VSCode安装与环境配置
进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件,然后打开终端,进入下载文件夹,键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...
LLM基础1_语言模型如何处理文本
基于GitHub项目:https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken:OpenAI开发的专业"分词器" torch:Facebook开发的强力计算引擎,相当于超级计算器 理解词嵌入:给词语画"…...
ardupilot 开发环境eclipse 中import 缺少C++
目录 文章目录 目录摘要1.修复过程摘要 本节主要解决ardupilot 开发环境eclipse 中import 缺少C++,无法导入ardupilot代码,会引起查看不方便的问题。如下图所示 1.修复过程 0.安装ubuntu 软件中自带的eclipse 1.打开eclipse—Help—install new software 2.在 Work with中…...
08. C#入门系列【类的基本概念】:开启编程世界的奇妙冒险
C#入门系列【类的基本概念】:开启编程世界的奇妙冒险 嘿,各位编程小白探险家!欢迎来到 C# 的奇幻大陆!今天咱们要深入探索这片大陆上至关重要的 “建筑”—— 类!别害怕,跟着我,保准让你轻松搞…...
【JVM】Java虚拟机(二)——垃圾回收
目录 一、如何判断对象可以回收 (一)引用计数法 (二)可达性分析算法 二、垃圾回收算法 (一)标记清除 (二)标记整理 (三)复制 (四ÿ…...
Windows安装Miniconda
一、下载 https://www.anaconda.com/download/success 二、安装 三、配置镜像源 Anaconda/Miniconda pip 配置清华镜像源_anaconda配置清华源-CSDN博客 四、常用操作命令 Anaconda/Miniconda 基本操作命令_miniconda创建环境命令-CSDN博客...
手机平板能效生态设计指令EU 2023/1670标准解读
手机平板能效生态设计指令EU 2023/1670标准解读 以下是针对欧盟《手机和平板电脑生态设计法规》(EU) 2023/1670 的核心解读,综合法规核心要求、最新修正及企业合规要点: 一、法规背景与目标 生效与强制时间 发布于2023年8月31日(OJ公报&…...
elementUI点击浏览table所选行数据查看文档
项目场景: table按照要求特定的数据变成按钮可以点击 解决方案: <el-table-columnprop"mlname"label"名称"align"center"width"180"><template slot-scope"scope"><el-buttonv-if&qu…...
