当前位置:网站首页>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", ""])
边栏推荐
- Flask Foundation
- File links cannot be opened or downloaded in Google browser
- 萬卷書 - 價值投資者指南 [The Education of a Value Investor]
- 691. Cube IV
- 【已解决】win10找不到本地组策略编辑器解决方法
- TCP cumulative acknowledgement and window value update
- My 2020 summary "don't love the past, indulge in moving forward"
- 《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
- 树莓派更新工具链
- 10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
猜你喜欢

Dora (discover offer request recognition) process of obtaining IP address

Interfaces and related concepts
![[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet](/img/9d/42dfef67246740f0dba0c6d8f1b625.jpg)
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet

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

TCP cumulative acknowledgement and window value update

Pits encountered in the use of El checkbox group

【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘

POI excel percentage

Summary of abnormal mechanism of interview

JMeter test result output
随机推荐
Visit Google homepage to display this page, which cannot be displayed
Flask Foundation
带你全流程,全方位的了解属于测试的软件事故
Use the jvisualvm tool ----- tocmat to start JMX monitoring
Wireshark software usage
691. 立方体IV
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
Sorting, dichotomy
Interfaces and related concepts
I. D3.js hello world
File operation serialization recursive copy
Architecture notes
【已解决】Unknown error 1146
RestHighLevelClient获取某个索引的mapping
CentOS switches and installs mysql5.7 and mysql8.0
【最详细】最新最全Redis面试大全(50道)
Advanced API (use of file class)
centos php7.2.24升级到php7.3
Pits encountered in the use of El checkbox group
4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!