当前位置:网站首页>Longest common prefix and
Longest common prefix and
2022-07-03 07:18:00 【darabiuz】
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string “”.
Example 1:
Input :strs = [“flower”,“flow”,“flight”]
Output :“fl”
Example 2:
Input :strs = [“dog”,“racecar”,“car”]
Output :""
explain : Input does not have a common prefix .
// First assume the longest public prefix ans Is the first , Traverse each subsequent string , And ans Go through one by one , Record subscript if different , Intercept ans return
var longestCommonPrefix = function (strs) {
if (!strs.length) return ''
let ans = strs[0]
// Traversal string array
for (let i = 1; i < strs.length; i++) {
// Traversing every character of a string
let tmp
if (!strs[i].length) return ''
for (let j = 0; j < strs[i].length; j++) {
// and ans contrast , Different ends convenience , here j It's the next one in a different position index
if (ans[j] != strs[i][j]) {
tmp = j
break
}
tmp = j + 1
}
ans = ans.substr(0, tmp)
}
return ans
};
longestCommonPrefix(["abab", "aba", ""])
边栏推荐
- Notes on the core knowledge of Domain Driven Design DDD
- Advanced API (byte stream & buffer stream)
- High concurrency memory pool
- [plus de détails] dernière entrevue complète redis (50)
- Interview questions about producers and consumers (important)
- Advanced API (UDP connection & map set & collection set)
- Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
- Book recommendation~
- POI excel percentage
- Thoughts on project development
猜你喜欢

Basic components and intermediate components

In depth analysis of reentrantlock fair lock and unfair lock source code implementation

Flask Foundation

Notes on the core knowledge of Domain Driven Design DDD

SecureCRT password to cancel session recording

Deep learning parameter initialization (I) Xavier initialization with code
![[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)](/img/7f/3d6b6ea5319f7165e07baf0a78d318.jpg)
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)

Common problems in io streams
![Gridome + strapi + vercel + PM2 deployment case of [static site (3)]](/img/65/8d79998e96a2c74ba6e237bee652c6.jpg)
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]

4279. Cartesian tree
随机推荐
Liang Ning: 30 lectures on brain map notes for growth thinking
The education of a value investor
Thoughts in Starbucks
CentOS php7.3 installing redis extensions
【最详细】最新最全Redis面试大全(50道)
twenty million two hundred and twenty thousand three hundred and nineteen
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
Advanced APL (realize group chat room)
How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘
When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
Raspberry pie update tool chain
JS date comparison
Laravel Web Framework
Distributed transactions
Advanced API (multithreading 02)
Book recommendation~
JMeter test result output
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]