java 回文字符串

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;
    }
}
时间: 2024-12-22 03:39:14

java 回文字符串的相关文章

判断是否是回文字符串(Java实现)

1.回文的定义:“回文数”就是正读倒读都一样的整数.如奇数个数字:98789,这个数字正读是98789 倒读也是98789.偶数个数字3223也是回文数.字母 abcba 也是回文. 2. 判断一个字符串是否是回文字符串(Java实现) 1 public class Test4 { 2 public static boolean isHuiWen(String text) { 3 int length = text.length(); 4 for (int i = 0; i < length /

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

背景 近期開始研究算法,于是在leetcode上做算法题,第五题Longest Palindromic Substring便是关于回文子串的. 什么是回文字串 回文字符串是指将该字符串前后颠倒之后和该字符串一样的字符串.比如:a,aaaa,aba,abba- 最长回文子串 要求最长回文子串,就须要遍历每个子串,时间复杂度是O(N2):推断字串是不是回文,时间复杂度是O(N),这种话算法的时间复杂度就是O(N3). 我刚開始想到的就是中心扩展法,代码例如以下: public static Stri

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

假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic string.如aba,或者abba.本题是这种,给定输入一个字符串.要求输出一个子串,使得子串是最长的padromic string. 下边提供3种思路 1.两側比較法 以abba这样一个字符串为例来看,abba中,一共同拥有偶数个字.第1位=倒数第1位.第2位=倒数第2位......第N位=倒数第N位 以aba这样一个字符串为例来看,aba中.一共同拥有奇数个字符.排除掉正中间的那个字符后,第1位=倒数第1

题目1192:回文字符串

题目描述: 给出一个长度不超过1000的字符串,判断它是不是回文(顺读,逆读均相同)的. 输入: 输入包括一行字符串,其长度不超过1000. 输出: 可能有多组测试数据,对于每组数据,如果是回文字符串则输出"Yes!",否则输出"No!". 样例输入: hellolleh helloworld 样例输出: Yes! No! C++ 代码: #include<stdio.h> #include<string.h> int main() { ch

hdu3068 求一个字符串中最长回文字符串的长度 Manacher算法

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 31611    Accepted Submission(s): 11618 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组

判断一个字符串是否为回文字符串

#include <stdio.h> #include <assert.h> #include <string.h> int is_pal_str(const char *p) {  assert(p);  int len = strlen(p);  const char *start = p;  const char *end = p+len - 1;  while (start < end)  {   if (*start == *end)   {    st

shell脚本实现检测回文字符串

所有回文字的结构特征如下: 如果字符数是偶数,那么它在结构上表现为:一个字符序列连着另一个字符相同但次序恰好相反的字符序列. 如果字符数为奇数,那么它在结构上表现为:一个字符序列连着另一个字符相同但次序恰好相反的字符序列,但是这两个序列中间共享一个相同的字符. sed命令能够记住之前匹配的子样式.可以用正则表达式:'\(.\)',匹配任意一个字符,\1表示其反向引用.如匹配有两个字符的回文正则表达式为: '\(.\)\(.\)\2\1' 匹配任意长度的回文脚本如下所示: #!/bin/bash

最长回文字符串 POJ3974

曾经有一个好算法放到我面前,我没有好好珍惜,直到用到的时候才后悔莫及. 那就是Manacher(马拉车算法),以O(n)的复杂度计算最长回文字符串. 曾经刷Leetcode的时候,室友跟我说了这个算法,但当时那个题目用中间枚举也过了,我就没有在意,直到前天才弄会,写这篇报告之前, 我又专门写了一遍马拉车,果然还是有点问题的. 详细原理链接 点击 Mark #include <stdio.h> #include <iostream> #include <string.h>

131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

131. Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["