当前位置:网站首页>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];
};
边栏推荐
猜你喜欢

Servlet learning diary 8 - servlet life cycle and thread safety

Redis之Bitmap

Redis' performance indicators and monitoring methods

一篇文章带你了解-selenium工作原理详解
![[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth](/img/10/c0545cb34621ad4c6fdb5d26b495ee.jpg)
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth

Pytest参数化你不知道的一些使用技巧 /你不知道的pytest

Reids之缓存预热、雪崩、穿透

Servlet learning diary 7 -- servlet forwarding and redirection

I-BERT

I-BERT
随机推荐
I-BERT
Redis core configuration
What is an R-value reference and what is the difference between it and an l-value?
Global and Chinese market of appointment reminder software 2022-2028: Research Report on technology, participants, trends, market size and share
自定义卷积注意力算子的CUDA实现
[OC foundation framework] - [set array]
Blue Bridge Cup_ Single chip microcomputer_ PWM output
Reids之缓存预热、雪崩、穿透
A convolution substitution of attention mechanism
[daily question] Porter (DFS / DP)
QML control type: Popup
Five layer network architecture
[shell script] - archive file script
Sentinel mode of redis
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
Redis之五大基础数据结构深入、应用场景
Redis之持久化实操(Linux版)
[three storage methods of graph] just use adjacency matrix to go out
Global and Chinese market of AVR series microcontrollers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of linear regulators 2022-2028: Research Report on technology, participants, trends, market size and share