当前位置:网站首页>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 :
边栏推荐
- PHP obtains the IP address, and the apache2 server runs without error
- Rxjs TakeUntil 操作符的学习笔记
- [buuctf.reverse] 117-120
- MySQL create given statement
- js工具函数,自己封装一个节流函数
- 瑞吉外卖项目(二)
- 独步武林,架构选型手册(包含 PDF)
- Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later
- Webapi performance optimization
- Fluent creates, reads and writes JSON files
猜你喜欢
Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
i++ 和 ++i的真正区别
Basic use and principle of Minio
Notes on writing questions in C language -- monkeys eat peaches
Encoding format for x86
Cubemx stm32f105rb USB flash drive reading and writing detailed tutorial
Jetpack compose layout (IV) - constraintlayout
CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
WPF Prism框架
I put a two-dimensional code with rainbow candy
随机推荐
CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded
Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
字符串 实现 strStr()
可穿戴设备或将会泄露个人隐私
How to make a self-service order wechat applet? How to do the wechat order applet? visual editing
独步武林,架构选型手册(包含 PDF)
Simple waterfall effect
(forwarding articles) after skipping multiple pages, shuttle returns to the first page and passes parameters
Why should the terminal retail industry choose the member management system
Cocopod error failed: undefined method `map 'for nil:nilclass
Repo sync will automatically switch the correspondence between the local branch and the remote branch - how to customize this behavior
Remove the mosaic, there's a way, attached with the running tutorial
Basic usage and principle of schedulemaster distributed task scheduling center
Puzzle (019.2) hexagonal lock
How to "transform" small and micro businesses (II)?
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
[MySQL learning notes 21] storage engine
Kotlin Foundation
Webapi performance optimization
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)