当前位置:网站首页>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", ""])
边栏推荐
- Final, override, polymorphism, abstraction, interface
- The difference between typescript let and VaR
- Centos切换安装mysql5.7和mysql8.0
- [day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
- Pits encountered in the use of El checkbox group
- 4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
- C代码生产YUV420 planar格式文件
- TypeScript let與var的區別
- 【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
- php artisan
猜你喜欢

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

C code production YUV420 planar format file

691. Cube IV

Summary of abnormal mechanism of interview

PAT甲级真题1166
![[solved] unknown error 1146](/img/f1/b8dd3ca8359ac9eb19e1911bd3790a.png)
[solved] unknown error 1146

Recursion, Fibonacci sequence

Basic knowledge about SQL database
![[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones](/img/d0/850e095a43610366d6144b2471f3f7.jpg)
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones

7.2 brush two questions
随机推荐
Download address collection of various versions of devaexpress
JMeter test result output
【已解决】win10找不到本地组策略编辑器解决方法
【CMake】CMake链接SQLite库
GStreamer ffmpeg avdec decoded data flow analysis
LeetCode
MySQL syntax (basic)
TypeScript let与var的区别
How to specify the execution order for multiple global exception handling classes
The essence of interview
php安装composer
Advanced API (use of file class)
在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
Visit Google homepage to display this page, which cannot be displayed
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
Specified interval inversion in the linked list
"Moss ma not found" solution
Store WordPress media content on 4everland to complete decentralized storage
Common analysis with criteria method
TypeScript let與var的區別