当前位置:网站首页>leetcode-14. Longest common prefix JS longitudinal scanning method
leetcode-14. Longest common prefix JS longitudinal scanning method
2022-07-06 09:22:00 【Qianfan at the front】
subject

Code
/** * @param {string[]} strs * @return {string} */
// Longitudinal scanning method
var longestCommonPrefix = function(strs) {
let strLen = strs[0].length; // The length of the first element
// Traverse the first element
for (let i = 0; i < strLen; i++) {
let str = strs[0][i];
// Traverse the entire array
for (let j = 0; j < strs.length; j++) {
if (strs[j][i] !== str) {
// If there is a different character , Then exit the loop
return strs[j].slice(0, i);
}
}
}
// End of cycle , Describe each character in the first element of the array , Other elements of the array have ,
// At this point, the first element of the array has been traversed , So this element is the longest common prefix
return strs[0];
};
边栏推荐
- Basic usage of xargs command
- Intel Distiller工具包-量化实现2
- 美团二面:为什么 Redis 会有哨兵?
- What is an R-value reference and what is the difference between it and an l-value?
- [oc]- < getting started with UI> -- common controls uibutton
- Selenium+pytest automated test framework practice
- CUDA realizes focal_ loss
- Redis cluster
- Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
- Improved deep embedded clustering with local structure preservation (Idec)
猜你喜欢

Intel Distiller工具包-量化实现1

Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find

Post training quantification of bminf

Redis之Bitmap

LeetCode:124. Maximum path sum in binary tree

Advanced Computer Network Review(4)——Congestion Control of MPTCP

How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)

Selenium+Pytest自动化测试框架实战(下)

什么是MySQL?MySql的学习之路是怎样的

Withdrawal of wechat applet (enterprise payment to change)
随机推荐
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
[daily question] Porter (DFS / DP)
IDS cache preheating, avalanche, penetration
不同的数据驱动代码执行相同的测试场景
How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)
Basic usage of xargs command
LeetCode:34. Find the first and last positions of elements in a sorted array
[OC-Foundation框架]-<字符串And日期与时间>
CSP salary calculation
Kratos ares microservice framework (I)
Selenium+pytest automated test framework practice
A convolution substitution of attention mechanism
基于B/S的医院管理住院系统的研究与实现(附:源码 论文 sql文件)
Blue Bridge Cup_ Single chip microcomputer_ PWM output
go-redis之初始化连接
requests的深入刨析及封装调用
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth
Reids之缓存预热、雪崩、穿透
The order of include header files and the difference between double quotation marks "and angle brackets < >
The five basic data structures of redis are in-depth and application scenarios