链表实现-回文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 ...
随机推荐
- centos7使用tar包安装mysql5.7
特别注意: 文档中涉及到密码的都是用的是弱密码,是存在安全风险的,一定要根据自己的情况修改为复杂度更高的密码! centos 7.6 mysql 5.7.31 基础目录: /srv/{app,data ...
- 深度剖析js闭包
一.什么是闭包? 方法里面返回一个方法 二.闭包存在的意义 延长变量的生命周期 作用域链 沟通内外部方法的桥梁 闭包会常驻内存 ==>慎用闭包 闭包里的变量不会被回收 创建私有环建 例 ...
- Windows 10无法显示无线网络连接
最近刚刚升级了一下操作系统,升级到了1903版本.正好又有一个HP的打印机安装了一下.结果,发现居然无法管理无线网络了.如果看不到图,请点我. 右击选择连接,也无法显示SSID. 驱动是从这个官网下载 ...
- day01-项目开发流程
多用户即时通讯系统01 1.项目开发流程 2.需求分析 用户登录 拉取在线用户列表 无异常退出(包括客户端和服务端) 私聊 群聊 发文件 服务器推送新闻/广播 3.设计阶段 3.1界面设计 用户登录: ...
- 【学习笔记】前馈神经网络(ANN)
前言 最近跟着<神经网络与深度学习>把机器学习的内容简单回顾了一遍,并进行了一定的查缺补漏,比如SVM的一些理解,one-hot向量,softmax回归等等. 然后我将继续跟着这本书,开始 ...
- centos7.2 安装MongoDB
1.配置阿里云yum仓库 #vim /etc/yum.repos.d/mongodb-org-4.0.repo [mngodb-org] name=MongoDB Repository baseurl ...
- Windows 下JDK绿色免安装制作教程
java自从被oracle收购后,windows下新的版本只有安装版.没有zip免安装. windows安装版有一下坏处 会写注册表 会将java.exe,javaw.exe 等解压到C:\Windo ...
- Loki二进制命令帮助
Usage of config-file-loader: -auth.enabled Set to false to disable auth. (default true) -azure.accou ...
- H5调用微信支付
这里用的是 vue项目; 首先在mounted中判断是否有openId,如果没有,则去获取 let openid = localStorage.getItem('openid'); if (!open ...
- Django 出现 frame because it set X-Frame-Options to deny 错误
一.背景 使用django3 进行开发时,由于项目前端页面使用iframe框架,浏览器错误提示信息如下 Refused to display 'http://127.0.0.1:8000/' in a ...