当前位置:网站首页>14 -- validate palindrome string II
14 -- validate palindrome string II
2022-06-25 14:43:00 【JH_ Cao】
subject
Github link
Given a non empty string s, Delete at most one character . Determine whether it can be a palindrome string .
Example 1:
Input : s = “aba” Output : true
Example 2:Input : s = “abca” Output : true explain : You can delete c character .
- Ideas
- Double pointer , Move the pointer left and right , See if it's equal
- Be careful not to delete the left and right elements , Although the complexity of deleting the last element of the array is O(1), But the complexity of deleting the first element of an array is O(n)
- Skip left or right , Should be considered once
The code is as follows :
func validPalindrome(_ s: String) -> Bool {
var temp = Array(s).map({String($0)})
var isDeleted = false
func helper1( _ left: Int, _ right: Int) -> Bool {
var left = left
var right = right
while left < right {
if temp[left] == temp[right] {
left += 1
right -= 1
} else {
if isDeleted {
return false
}
isDeleted = true
return helper1(left + 1, right) || helper1(left, right - 1) // Delete left or right , Take into account
}
}
return true
}
return helper1(0, temp.count - 1)
}
边栏推荐
- 【世界历史】第二集——文明的曙光
- Classifier and cross entropy loss function
- Two common ways for orcale to clear table data
- Getting started with shell variables
- Extend JS copy content to clipboard
- Get the parameters in the URL and the interchange between parameters and objects
- JS to verify whether the string is a regular expression
- Kubernetes cluster construction of multiple ECS
- 【HBZ分享】LockSupport的使用
- 程序員為什麼要軟一點?
猜你喜欢
Complete and detailed compilation of experimental reports
Uniapp icon configuration
oracle数据库常用的函数总结
shell 变量 入门
【Try to Hack】vulhub靶场搭建
Thymeleaf Usage Summary
Experts' suggestions | 8 measures to accelerate your innovative career planning and growth
重磅!国产 IDE 发布,由阿里研发,完全开源!(高性能+高定制性)
JGG | overview of duhuilong group of Hebei University Research on plant pan genomics
程序員為什麼要軟一點?
随机推荐
Where is it safe to open an account for buying funds? Ask for guidance
JGG | 河北大学杜会龙组综述植物泛基因组学研究
HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
[untitled] the CMD command window displays' NPM 'which is not an internal or external command
How to crop GIF dynamic graph? Take this picture online clipping tool
Shell array
Add a string at the input and textarea cursors
Clipboard tutorial
[deep learning] multi label learning
PubSub JS library realizes "cross component" data transfer
112页机器学习-数学基础回顾.pptx
【中国海洋大学】考研初试复试资料分享
[world history] Episode 1: people in the Stone Age
JS to add elements to the header, or tail of an array
Is qiniu regular? Is it safe to open a stock account?
How to choose a technology stack for web applications in 2022
【中國海洋大學】考研初試複試資料分享
For the first time in China, Chinatelecom 5g underground personnel positioning project is officially commercial: it can track the position in real time to ensure operation safety
一次性总结:64个数据分析常用术语!
弹性布局(display:flex;)属性详解