package string.string1_5;

public class Palindrome
{
/**
* 从两头向中间移动
* @param str
* @return
*/
public static boolean isPalindrome(String str)
{
if(str == null || str.equals(""))
return false ;
int left = 0 ;
int right = str.length()-1 ; while (left < right)
{
if(str.charAt(left++) != str.charAt(right--))
return false ;
} return true ;
} /**
* 从中间向两头移动
* @param str
* @return
*/
public static boolean isPalindrome2(String str)
{
if(str == null || str.equals(""))
return false ; int left = str.length()/2 ;
int right = str.length()-1-left ; while (left >= 0)
{
if(str.charAt(left--) != str.charAt(right++))
return false ;
} return true ;
} /**
* 判断链表是否为回文
* @param node
* @return
*/
public static boolean isPalindrome3(ListNode node)
{
//当链表为空或者链表只包含一个元素
if(node == null || node.next == null)
return true ;
ListNode head = new ListNode() ;
head.next = node ;
ListNode slow = head ;
ListNode quick = head ; while (quick.next != null)
{
slow = slow.next ;
for(int i=0 ; i<2 ; i++)
if(quick.next != null)
quick = quick.next ;
else
break ;
} ListNode f = slow.next ;
slow.next = null ;
ListNode l = null ;
ListNode t = null ; while (f != null)
{
t = f ;
f = f.next ;
t.next = l ;
l = t ;
} slow.next = t ; quick = slow.next ;
slow = node ; while (quick != null)
{
if(slow.ch != quick.ch)
return false ;
slow = slow.next ;
quick = quick.next ;
} return true ;
} public static void main(String[] args)
{
/*
ListNode node1 = new ListNode('a') ;
ListNode node2 = new ListNode('b') ;
ListNode node3 = new ListNode('c') ;
ListNode node4 = new ListNode('c') ;
ListNode node5 = new ListNode('b') ;
ListNode node6 = new ListNode('a') ; node1.next = node2 ;
node2.next = node3 ;
node3.next = node4 ;
node4.next = node5 ;
node5.next = node6 ;*/
ListNode node1 = new ListNode('a') ;
ListNode node2 = new ListNode('b') ;
ListNode node3 = new ListNode('c') ;
ListNode node5 = new ListNode('a') ;
ListNode node6 = new ListNode('a') ; node1.next = node2 ;
node2.next = node3 ;
node3.next = node5 ;
node5.next = node6 ; System.out.println(isPalindrome3(node1));
}
} class ListNode
{
char ch ;
ListNode next ; public ListNode() {} public ListNode(char ch) {
this.ch = ch;
}
}

最新文章

  1. 本地项目上传到GitHub
  2. ssh密钥私钥不能登陆问题处理
  3. Swift版的SQLiteHelper
  4. __cdecl和__stdcall
  5. 【转】Spark性能优化指南——基础篇
  6. Ubuntu 利用 xinetd 限制 SSH 连接数
  7. 启动Eclipse 弹出&quot;Failed to load the JNI shared library jvm.dll&quot;错误
  8. 将textField编辑完内容作为参数发送请求
  9. Delphi Application.MessageBox详解
  10. java加载配置文件
  11. Unix/Linux环境C编程入门教程(16) LinuxMint CCPP开发环境搭建
  12. 怎样批量把excel中已显示的科学计数法取消
  13. ural 1073.Square Country(动态规划)
  14. sql server调优
  15. ranch 源码分析(一)
  16. freeRTOS中文实用教程3--中断管理之计数信号量
  17. Pytorch 基础
  18. Xilinx 常用模块汇总(verilog)【04】
  19. 页面ajax自带的访问后台时,正在加载中
  20. 阿里云(一)云存储OSS的命令行osscmd的安装和使用

热门文章

  1. 【FFT】学习笔记
  2. bzoj 4566 [Haoi2016]找相同字符SA
  3. net4:Panel动态添加控件及隐藏,Table动态创建表格
  4. CSS 盒模型、解决方案、BFC 原理讲解--摘抄
  5. 安装sass的艰难过程
  6. SnakeYaml使用
  7. oracle function dtrace
  8. 邁向IT專家成功之路的三十則鐵律 鐵律十八:IT人求職之道-文化
  9. ActiveX控件打包成Cab置于网页中自动下载安装 [转]
  10. 关于Adapter对数据库的查询、删除操作