当前位置:网站首页>字符串 最长公共前缀
字符串 最长公共前缀
2022-06-25 09:37:00 【Morris_】
LC 最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。
输入:strs = [“flower”,“flow”,“flight”]
输出:“fl”
swift 实现
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let strs = ["flower","flow","flight"]
//let strs = ["dog","racecar","car"]
//let strs = ["ab", "a"]
//let strs = ["flower","flower","flower","flower"]
print(longestCommonPrefix(strs))
}
func longestCommonPrefix(_ strs: [String]) -> String {
if strs.count == 0 {
return ""
}
if strs.count == 1 {
return strs[0]
}
// 按第一个字符串进行遍历
let array = Array(strs[0])
for i in 0..<array.count {
let c = array[i]
var index = 1
while index < strs.count {
let temp = Array(strs[index])
if temp.count > i {
if c != temp[i] {
if i > 0 {
return String(strs[0].prefix(i))
}
else {
return ""
}
}
}
else {
return String(strs[0].prefix(i))
}
index += 1
}
}
return strs[0]
}
}
思路:

边栏推荐
- Question B of the East China Cup: how to establish a population immune barrier against novel coronavirus?
- Processing picture class library
- 力扣-104. 二叉树的最大深度
- JS tool function, self encapsulating a throttling function
- Oracle function trigger
- 22 mathematical modeling contest 22 contest C
- I put a two-dimensional code with rainbow candy
- Voiceprint Technology (V): voiceprint segmentation and clustering technology
- x86的编码格式
- Swift recursively queries the array for the number closest to the specified value
猜你喜欢

Online notes on Mathematics for postgraduate entrance examination (8): Kego equations, eigenvalues and eigenvectors, similarity matrix, quadratic series courses
![[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition](/img/b4/84c30ed112c4dffd8d51697b2f4a4f.jpg)
[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition

Simple waterfall effect

8、智慧交通项目(1)

Rxjs TakeUntil 操作符的学习笔记

Japanese online notes for postgraduate entrance examination (9): composition template
![[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)](/img/f3/a8414635ad39ad405864f5fcf66d6d.jpg)
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)

Applet cloud development joint table data query and application in cloud function

With the QQ group file storage function of super nice, you immediately have n cloud disks that are easy to download and never expire

Encoding format for x86
随机推荐
处理图片类库
Download the arm64 package of Debian on X86 computer
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)
Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
Japanese online notes for postgraduate entrance examination (9): composition template
[learn C from me and master the key to programming] insertion sort of eight sorts
Unique Wulin, architecture selection manual (including PDF)
Online notes on Mathematics for postgraduate entrance examination (9): a series of courses on probability theory and mathematical statistics
How much money have I made by sticking to fixed investment for 3 years?
I put a two-dimensional code with rainbow candy
Question B of the East China Cup: how to establish a population immune barrier against novel coronavirus?
Rxjs TakeUntil 操作符的学习笔记
纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口
Learning notes of rxjs takeuntil operator
Etcd tutorial - Chapter 4 etcd cluster security configuration
Neo4jdesktop (neo4j desktop version) configures auto start (boot auto start)
可穿戴设备或将会泄露个人隐私
Flutter dialog: cupertinoalertdialog
将jar包注册为服务,实现开机自动启动
Test Development Engineer