LeetCode-67. 二进制求和(Golang实现)
2023-09-27 14:28:25 时间
LeetCode题号:67. 二进制求和
题目描述:
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 :
示例 1:
输入: "A man, a plan, a canal: Panama"
输出: true
示例 2:输入: "race a car"
输出: false
解题思路:
1、直接在原串上直接判断,值判断数字字母,双子针两头往中间遍历,
2、1、对原串进行晒选,只留字母跟数字 存进一个新数组中,2、然后按普通 回文串判断即可
代码:
//解题思路:1、只留字母跟数字 放进一个新数组中,2、然后按普通回文串判断即可
func isPalindrome1(s string) bool {
var sgood string
for i := 0; i < len(s); i++ {
if returnB(s[i]) {
sgood += string(s[i])
}
}
//全部转换成小写
sgood = strings.ToLower(sgood)
for j := 0; j < len(sgood)/2; j++ {
if sgood[j] != sgood[len(sgood)-1-j] {
return false
}
}
return true
}
//判断只含字母,跟数字
func returnB(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
}
/*
解题思路二:在原字符串上直接判断。
*/
func isPalindrome2(s string) bool {
//转换成小写
s = strings.ToLower(s)
var left, right = 0, len(s) - 1
for left < right {
//只验证字母和数字字符,其余的字符 空格等直接跳过
for left < right && !returnB(s[left]) {
left++
}
//只验证字母和数字字符,其余的跳过
for left < right && !returnB(s[right]) {
right--
}
if left < right {
if s[left] != s[right] {
return false
}
left++
right--
}
}
return true
}
//判断只含字母,跟数字
func returnB(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
}
往期回顾:
【1】LeetCode-409. 最长回文串(Goland实现)
【2】LeetCode-459. 重复的子字符串(Goland实现)
【3】LeetCode-53. 最大子序和(Goland实现)
❤如果文章对您有所帮助,就在文章的右上角或者文章的末尾点个赞吧!(づ ̄ 3 ̄)づ
❤如果喜欢大白兔分享的文章,就给大白兔点个关注吧!(๑′ᴗ‵๑)づ╭❤~
❤对文章有任何问题欢迎小伙伴们下方留言或者入群探讨【群号:708072830】
❤鉴于个人经验有限,所有观点及技术研点,如有异议,请直接回复讨论(请勿发表攻击言论)
相关文章
- LeetCode_贪心算法_中等_738.单调递增的数字
- LeetCode_模拟_中等_799.香槟塔
- LeetCode_栈_简单_155. 最小栈
- LeetCode·每日一题·1641. 统计字典序元音字符串的数目·动态规划
- LeetCode·每日一题·1582.二进制矩阵中的特殊位置·模拟
- LeetCode·每日一题·761.特殊的二进制序列·分治
- ★ Linked List Cycle II -- LeetCode
- Gas Station [leetcode] 两个解决方案
- Leetcode状压DP
- [LeetCode] 636. Exclusive Time of Functions 函数的独家时间
- [LeetCode] 191. Number of 1 Bits 二进制数1的个数
- [LeetCode] 401. Binary Watch 二进制表
- [LeetCode] 283. Move Zeroes 移动零
- leetcode 23 合并k个升序列表
- leetcode 1356 根据数字二进制下1的数目排序
- leetcode 200 岛屿数量
- leetcode 227 基本计算器II