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;
}
}

java 回文字符串的更多相关文章

  1. LeetCode 第五题 最长的回文字符串 (JAVA)

    Longest Palindromic Substring 简介:字符串中最长的回文字符串 回文字符串:中心对称的字符串 ,如 mom,noon 问题详解: 给定一个字符串s,寻找字符串中最长的回文字 ...

  2. 转载-----Java Longest Palindromic Substring(最长回文字符串)

    转载地址:https://www.cnblogs.com/clnchanpin/p/6880322.html 假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic st ...

  3. Java Longest Palindromic Substring(最长回文字符串)

    假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic string.如aba,或者abba.本题是这种,给定输入一个字符串.要求输出一个子串,使得子串是最长的padro ...

  4. 【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】

    背景 近期開始研究算法,于是在leetcode上做算法题,第五题Longest Palindromic Substring便是关于回文子串的. 什么是回文字串 回文字符串是指将该字符串前后颠倒之后和该 ...

  5. Java 判断是否为回文字符串

    回文字符串有两种:abcba,abccba. 代码: static boolean func(String str) { int len = str.length(); for (int i = 0; ...

  6. leetcode.双指针.680验证回文字符串-Java

    1. 具体题目 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca&q ...

  7. Java实现 LeetCode 680 验证回文字符串 Ⅱ(暴力)

    680. 验证回文字符串 Ⅱ 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: " ...

  8. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  9. SRM589 DV1 250 回文字符串

    其实这道题挺简单的,不过刚开始我钻了一个错误的死胡同.想明白之后才发现. 题目要求用最少的时间来将一个字符串变成回文字符串.(具体题目参看topcoder srm589 DV1 250分值的题目,不便 ...

随机推荐

  1. BZOJ1068 [SCOI2007]压缩 【区间dp】

    题目 给一个由小写字母组成的字符串,我们可以用一种简单的方法来压缩其中的重复信息.压缩后的字符串除了小 写字母外还可以(但不必)包含大写字母R与M,其中M标记重复串的开始,R重复从上一个M(如果当前位 ...

  2. element-ui select组件使用需要注意的

    当在使用select组件的时候,要注意 <el-select v-model="scope.row.state" @change="editDriftStatus& ...

  3. ADO:用代码调用存储过程

    原文发布时间为:2008-08-02 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  4. 转 Django+Bootstrap练习--我的类博客系统开发

    转自: http://blog.sina.com.cn/s/blog_7e050dc80102w312.html 本文记录了一个类博客网站从无到有的搭建过程,同时也是我入门django以及再次入门前端 ...

  5. 内核的bootmem内存分配器【转】

    转自:http://blog.csdn.net/zmxiangde_88/article/details/8041040 版权声明:本文为博主原创文章,未经博主允许不得转载. 在内核启动期间,伙伴系统 ...

  6. upper_bound()和lower_bound()

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  7. Linux 之 LNMP服务器搭建-MySQL

    LNMP服务器搭建-MySQL 参考教程:[千峰教育] 系统版本: CentOS 6.8 关闭防火墙和Selinux service iptables stop setenforce 0 安装mysq ...

  8. Integration testing

    Integration testing 集成测试用来确保app的不同模块之间可以正确的一起工作.ASP.NET Core提供单元测试框架和内建的测试网络服务来支持集成测试,并且测试网络服务不需要网络开 ...

  9. CSS-点赞爱心小动画

    爱心图片: HTML: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...

  10. echarts 金字塔

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...