链表实现-回文palindrome判断
1.数字回文判断(逆转,分离未位,砍掉个位,保存原来)
s = s * 10 + a%10
a = a/10
2.字符串判断回文
package main
//思路: 开发一个栈来来存放链表的上半段
func isPalindrome1(l *LinkedList) bool {
lLen := l.length
if lLen == 0 {
return false
}
if lLen == 1 {
return true
}
s := make([]string, 0, lLen/2)
cur := l.head
for i := uint(1); i <= lLen; i++ {
cur = cur.next
if lLen % 2 != 0 && i == (lLen/2 + 1) {
continue
}
//有一半入栈
if i <= lLen/2 {
s = append(s,cur.GetValue().(string))
} else {
//后一半和前一半做对比
if s[lLen - i] != cur.GetValue().(string) {
return false
}
}
}
return true
}
//思路: 找到链表中间节点,将前半部分转置,在从中间向左右遍历对比
func isPalindrome2(l *LinkedList) bool {
lLen := l.length
if lLen == 0 {
return false
}
if lLen == 1 {
return true
}
var isPalindrome = true
step := lLen/2
var pre *ListNode = nil
cur := l.head.next
next := l.head.next.next
for i := uint(1); i <= step; i++ {
tmp := cur.GetNext()
cur.next = pre
pre = cur
cur = tmp
next = cur.GetNext()
}
mid := cur
var left,right *ListNode = pre,nil
if lLen%2 != 0 {
right = mid.GetNext()
} else {
right = mid
}
for nil != left && nil != right {
if left.GetValue().(string) != right.GetValue().(string) {
isPalindrome = false
break
}
left = left.GetNext()
right = right.GetNext()
}
cur = pre
pre = mid
for nil != cur {
next = cur.GetNext()
cur.next = pre
pre = cur
cur = next
}
l.head.next = pre
return isPalindrome
}
//test
package main
import "testing"
func TestPalindrome1(t *testing.T){
strs := []string{"heooeh","hello","heooeh","a",""}
for _,str1 := range strs {
l := NewLinkedList()
for _,c := range str1 {
l.InsertToTail(string(c))
}
l.Print()
t.Log(isPalindrome1(l))
}
}
func TestPalindrome2(t *testing.T) {
strs := []string{"heooeh","hello","heooeh","a",""}
for _,str1 := range strs {
l := NewLinkedList()
for _,c := range str1 {
l.InsertToTail(string(c))
}
l.Print()
t.Log(isPalindrome2(l))
l.Print()
}
}
链表实现:
package main
import "fmt"
type ListNode struct{
next *ListNode
value interface{}
}
type LinkedList struct {
head *ListNode
length uint
}
func NewListNode(v interface{}) *ListNode{
return &ListNode{nil,v}
}
func NewLinkedList() *LinkedList {
return &LinkedList{NewListNode(0),0}
}
func (this *ListNode) GetNext() *ListNode {
return this.next
}
func (this *ListNode) GetValue() interface{} {
return this.value
}
func (this *LinkedList) InsertToAfter(p *ListNode,v interface{}) bool {
if nil == p {
return false
}
newNode := NewListNode(v)
oldNext := p.next
p.next = newNode
newNode.next = oldNext
this.length ++
return true
}
func (this *LinkedList) InsertToHead(v interface{}) bool {
return this.InsertToAfter(this.head,v)
}
func (this *LinkedList) InsertToTail(v interface{}) bool {
cur := this.head
for nil != cur.next {
cur = cur.next
}
return this.InsertToAfter(cur,v)
}
func (this *LinkedList) FindByIndex(index uint) *ListNode {
if index > this.length {
return nil
}
cur := this.head.next
var i uint = 0
for ; i < index; i++ {
cur = cur.next
}
return cur
}
func (this *LinkedList) DeleteNode(p *ListNode) bool {
if nil == p {
return false
}
cur := this.head.next
pre := this.head
for nil != cur {
if cur == p {
break
}
pre = cur
cur = cur.next
}
if nil == cur {
return false
}
pre.next = p.next
p = nil
this.length --
return true
}
func (this *LinkedList) Print() {
cur := this.head.next
format := ""
for cur != nil {
format += fmt.Sprintf("+%v",cur.GetValue())
cur = cur.next
if nil != cur {
format += "->"
}
}
fmt.Println(format)
}
//test
package main
import "testing"
func TestInsertToHead(t *testing.T) {
l := NewLinkedList()
for i := 0; i < 10; i++ {
l.InsertToHead(i+1)
}
l.Print()
}
func TestInsertToTail(t *testing.T) {
l := NewLinkedList()
for i := 0; i < 10; i++ {
l.InsertToTail(i+1)
}
l.Print()
}
func TestFindByIndex(t *testing.T) {
l := NewLinkedList()
for i := 0; i < 10; i++ {
l.InsertToTail(i+1)
}
t.Log(l.FindByIndex(0))
t.Log(l.FindByIndex(9))
t.Log(l.FindByIndex(10))
}
func TestDeleteNode(t *testing.T) {
l := NewLinkedList()
for i := 0; i < 10; i++ {
l.InsertToTail(i+1)
}
l.Print()
t.Log(l.DeleteNode(l.head.next.next))
l.Print()
}
链表实现-回文palindrome判断的更多相关文章
- 链表回文串判断&&链式A+B
有段时间没有练习了,链表回文串判断用到了栈.链式A+B将没有的项用0补充.链表有没有头节点,及结点和链表的区别,即pNode和pHead. //#include<iostream> //u ...
- 链表回文串判断&&链式A+B
有段时间没有练习了,链表回文串判断用到了栈.链式A+B将没有的项用0补充.链表有没有头节点,及结点和链表的区别,即pNode和pHead. //#include<iostream> //u ...
- 【Python】回文palindrome——利用字符串反转
回文 palindrome Python 字符串反转string[::-1] Slice notation "[a : b : c]" means "count in i ...
- 【Python 实例】回文数判断
[Python 实例]回文数判断 题目: 源代码: 运行结果: 题目: 判断输入的字符串是否为回文数 源代码: """ string_reverse_output():反 ...
- 单链表的回文判断(O(n)时间复杂度和O(1)的空间复杂度)
对于单链表来说,判断回文最简单的方法就是遍历链表,将链表中的元素复制到数组中,然后对数组进行判断是否是回文数组,但是这不符合O(1)的空间复杂度. 由于空间复杂度的要求,需要就地操作链表,不能开辟多余 ...
- 判断回文字符串、回文链表、回文数(python实现)
所谓回文字符串,就是正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.即是对称结构 判断回文字符串 方法一: def is_palin ...
- LeetCode 9 Palindrome Number(回文数字判断)
Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的 ...
- <LC刷题二>回文字符串判断之leetcode125&234
其他刷题记录见博客首页 1,leecode125 验证回文串 原题: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. ...
- 【Leetcode链表】回文链表(234)
题目 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否用 O(n) ...
- C,C++回文字符串判断(字符串指针的用法)
功能:输入一个字符串,判断是否为回文. 主要锻炼指针的用法. 1.C版 #include<stdio.h> int main() { ]; char a; ,flag=; while((a ...
随机推荐
- pathlib路径问题
下面是我的文件框架 app ------ file1---- .py1 file2---- .py2 config.py 我在config文件中设置了变量参数 BASE_DIR = pathlib.P ...
- 通过ftutilx 插件实现流版式文件全文检索
Oracle 支持流版式文件的全文检索,而原生的PostgreSQL是不支持流版式文件全文检索的.KingbaseES 通过ftutilx 插件将流版式文件转换成文本文件,从而支持流版式文件全文检索. ...
- 第一行代码Android(第3版).pdf下载
2020年人民邮电出版社出版的图书 <第一行代码Android(第3版)>是2020年4月人民邮电出版社出版的图书,作者是郭霖. 封面: 内容简介: <第一行代码 Android 第 ...
- 三分钟,带你了解PLM
PLM应用于单一地点或者多个地点的企业内部.以及在产品研发领域具有协作关系的企业之间的.支持产品全生命周期的信息的创建.管理.分发和应用的综合性的应用解决方案,能够集成与产品相关的流程.应用系统和信息 ...
- WinUI 3 踩坑记:第一个窗口
本文是 WinUI 3 踩坑记 的一部分,该系列发布于 GitHub@Scighost/WinUI3Keng,文中的代码也在此仓库中,若内容出现冲突以 GitHub 上的为准. WinUI 3 应用的 ...
- 编写 bzt 脚本的正确姿势
这是今年1月24日的旧文,发现没在这里发过,就搬运过来了. 声明 本文讨论的使用场景主要为使用已有的 jmx 脚本,并配合 json 对 jmx 脚本进行部分参数的动态修改. 只补充一些官方文档上没有 ...
- Linux病毒扫描工具ClamAV(Clam AntiVirus)安装使用
在线检测木马病毒的网址:https://www.virustotal.com/gui/home/upload 一.简介 ClamAV(Clam AntiVirus)是Linux平台上的开源病毒扫描程序 ...
- 谣言检测——(PSA)《Probing Spurious Correlations in Popular Event-Based Rumor Detection Benchmarks》
论文信息 论文标题:Probing Spurious Correlations in Popular Event-Based Rumor Detection Benchmarks论文作者:Jiayin ...
- SECS半导体设备通讯-4 GEM通信标准
一 概述 GEM标准定义了通信链路上的半导体设备的行为. SECS-II标准定义了在主机和设备之间交换的消息和相关数据项.GEM标准则定义了在哪种情况下应该使用哪些SECS-II消息以及由此产生的结果 ...
- ArcMap布局添加图表问题
在ArcMap分析制图过程中,经常会产生一些图表,然而在布局中添加这些图表会发现一些意想不到的问题. 问题重现 将图表直接添加到布局会发现图表有黑底,这在我们布局出图中是十分不美观的,这该如何解决呢? ...