当前位置:网站首页>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 :

边栏推荐
- MySQL create given statement
- Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
- WPF 绑定表达式和绑定数据源(一)
- 测试开发工程师
- What functions should smart agriculture applet system design have
- [MySQL learning notes 22] index
- [Ruby on rails full stack course] course directory
- Cocopod error failed: undefined method `map 'for nil:nilclass
- Jetpack compose layout (II) - material components and layout
- Jetpack compose layout (I) - basic knowledge of layout
猜你喜欢

链表 删除链表中的节点

独步武林,架构选型手册(包含 PDF)

ShardingSphere-Proxy 4.1 分庫分錶
![[MySQL learning notes 22] index](/img/08/db7b765f7ddaa5706e3f7d00690d23.png)
[MySQL learning notes 22] index

x86电脑上下载debian的arm64的包

Get started quickly with jetpack compose Technology

NetCore性能排查

How much does a wechat applet cost? Wechat applet development and production costs? Come and have a look

Flutter dialog: cupertinoalertdialog

Basic usage and principle of schedulemaster distributed task scheduling center
随机推荐
oracle 函数 触发器
[Ruby on rails full stack course] course directory
Match a mobile number from a large number of mobile numbers
JS tool function, self encapsulating a throttling function
Computational Thinking and economic thinking
在指南针上面开股票账户好不好,安不安全?
Redis(二)分布式锁与Redis集群搭建
MySQL source code reading (II) login connection debugging
[MySQL learning notes 21] storage engine
Reza RA series - development environment construction
The problem of wirengpi program running permission
Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0
I put a two-dimensional code with rainbow candy
(forwarding articles) after skipping multiple pages, shuttle returns to the first page and passes parameters
Encoding format for x86
Processing picture class library
Mysql 源码阅读(二)登录连接调试
How to "transform" small and micro businesses (I)?
How do wechat applets make their own programs? How to make small programs on wechat?
纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口