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;
}
}
java 回文字符串的更多相关文章
- LeetCode 第五题 最长的回文字符串 (JAVA)
Longest Palindromic Substring 简介:字符串中最长的回文字符串 回文字符串:中心对称的字符串 ,如 mom,noon 问题详解: 给定一个字符串s,寻找字符串中最长的回文字 ...
- 转载-----Java Longest Palindromic Substring(最长回文字符串)
转载地址:https://www.cnblogs.com/clnchanpin/p/6880322.html 假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic st ...
- Java Longest Palindromic Substring(最长回文字符串)
假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic string.如aba,或者abba.本题是这种,给定输入一个字符串.要求输出一个子串,使得子串是最长的padro ...
- 【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】
背景 近期開始研究算法,于是在leetcode上做算法题,第五题Longest Palindromic Substring便是关于回文子串的. 什么是回文字串 回文字符串是指将该字符串前后颠倒之后和该 ...
- Java 判断是否为回文字符串
回文字符串有两种:abcba,abccba. 代码: static boolean func(String str) { int len = str.length(); for (int i = 0; ...
- leetcode.双指针.680验证回文字符串-Java
1. 具体题目 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca&q ...
- Java实现 LeetCode 680 验证回文字符串 Ⅱ(暴力)
680. 验证回文字符串 Ⅱ 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: " ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- SRM589 DV1 250 回文字符串
其实这道题挺简单的,不过刚开始我钻了一个错误的死胡同.想明白之后才发现. 题目要求用最少的时间来将一个字符串变成回文字符串.(具体题目参看topcoder srm589 DV1 250分值的题目,不便 ...
随机推荐
- 在 Ubuntu 16.04 上安装 Eclipse Oxygen
2017 年 6 月 28 日,Eclipse 社区(the Eclipse Community)发布了 Eclipse Oxygen.本文记录了我在 Ubuntu 16.04 上安装 Eclipse ...
- LSU——1116 Necklace(尺取)
1116 Necklace 通过率:5/23 难度系数:0时间限制:1000ms 内存限制:32000KB java 两倍. 介绍 Little King has a beautiful pearl ...
- 应用defineProperty简单实现vue的双向数据绑定
双向数据绑定简易版本如何应用defineProperty的getter setter 方法 有这样HTML片段 <input type="text" id="dem ...
- [LeetCode] Text Justification words显示的排序控制
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- LeetCode OJ--Minimum Path Sum **
https://oj.leetcode.com/problems/minimum-path-sum/ 对一个grid从左上角到右下角的路径,求出路径中和最小的. 受之前思路的影响,就寻思递归,并且记录 ...
- LeetCode OJ——Word Ladder2
http://oj.leetcode.com/problems/word-ladder-ii/ class Solution { public: vector<vector<string& ...
- 洛谷——P1144 最短路计数
P1144 最短路计数 题目描述 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: 输入第一行包含2个正整数N,M,为图的顶 ...
- Skiing(最短路)
poj——3037 Skiing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4921 Accepted: 1315 ...
- awk 统计
命令太多,记不住,组合起来用一把…..示例文件: 1 2 3 4 5 6 7 8 9 10 11 [root@lovedan test]# cat a.txt hello good world hel ...
- linux编译
文章一 1)用户点击编译程序时,编译程序将C++源代码转换成目标代码,目标代码通常由 机器指令和记录如何将程序加载到内存的信息组成.其后缀通常为.obj或.o: 2)目标文件中存储的只是用户所编写的代 ...