当前位置:网站首页>String longest common prefix
String longest common prefix
2022-06-25 10:06:00 【Morris_】
LC The longest common prefix
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string “”.
Input :strs = [“flower”,“flow”,“flight”]
Output :“fl”
swift Realization
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]
}
// Traverse by first string
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]
}
}
Ideas :

边栏推荐
- Jetpack compose layout (IV) - constraintlayout
- ShardingSphere-Proxy 4.1 分库分表
- Kotlin keyword and operator
- puzzle(019.2)六边锁
- CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded
- Kotlin Foundation
- Cocopod error failed: undefined method `map 'for nil:nilclass
- Mengyou Technology: tiktok live broadcast with goods elements hot topics retention skills shaping image highlight selling points
- 如何自制一个安装程序,将程序打包生成安装程序的办法
- CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
猜你喜欢

可穿戴设备或将会泄露个人隐私

Redis (I) principle and basic use

How do wechat sell small commodity programs do? How to open wechat apps to sell things?

链表 删除链表中的节点

Jetpack compose layout (IV) - constraintlayout

MongoDB的原理、基本使用、集群和分片集群

ShardingSphere-Proxy 4.1 分庫分錶

CyCa children's physical etiquette Yueqing City training results assessment successfully concluded

ScheduleMaster分布式任务调度中心基本使用和原理

Minio基本使用与原理
随机推荐
Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days
The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores
Minio基本使用与原理
NFC read / write mode development - book summary
Jetpack compose layout (I) - basic knowledge of layout
Arduino bootloader burning summary
在指南针上面开股票账户好不好,安不安全?
从海量手机号中匹配某一个手机号
An auxiliary MVP architecture project quick development library -mvpfastdagger
Best producer consumer code
Opencv中的GrabCut图像分割
How much does a wechat applet cost? Wechat applet development and production costs? Come and have a look
可穿戴设备或将会泄露个人隐私
瑞吉外卖项目(二)
Match a mobile number from a large number of mobile numbers
x86电脑上下载debian的arm64的包
How to apply for a widget on wechat how to get a widget on wechat
WPF 绑定表达式和绑定数据源(一)
Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.
How to do the wechat selling applet? How to apply for applets