[LeetCode] Palindrome Number & Valid Palindrome - 回文系列问题

简介:

题目概述:

Determine whether an integer is a palindrome. Do this without extra space.


题目分析:

判断数字是否是回文 例如121、656、3443
方法有很多,正着看和到着看两数相同;当然负数显然不是回文

我的方法:

第一种方法:
由于没有没有看到前面的without extra space。采用的方法是把数字转换为字符串,依次比较最前和最后两个字符是否相同,直到遍历完毕。

/**
 * 判断一个数字是否是回文数字 Palindrome
 * 最简单方法数组存储数字每位 判断回文 或依次比较数字left和right 
 */ 
bool isPalindrome(int x) {
    char str[20];     //存储数字位数
    int i,j;
    int count;        //数字位数
    
    //负数肯定不是回文
    if(x<0) return false;

    i=0;
    while(x>0) {
        str[i]=x%10+'0';
        i++;
        x=x/10;
    }
    
    count=i;
    i=0;
    j=count-1;
    while(i<j) {
        if(str[i]==str[j]) {
            i++;
            j--;
        }
        else {
            return false;
        }
    }
    return true;
}
第二种方法:
通过依次比较最高位和最低位数字,然后依次数字降低两位。要点是先计算最高位是多少位。需要注意计算高位时:x=x-(left*result)
例如110011  高位-1 低位-1   110011-100000=10011  10011/10=1001再判断,而%会出现错误。

/**
 * 判断一个数字是否是回文数字 Palindrome
 * without extra space. 
 */ 
bool isPalindrome(int x) {
    int left,right;    //数字高低位
    int result;        //计算高位数字
    int number;
    
    if(x<0) return false;
    
    //计算最高位
    result=1;
    number=x;
    while(number>=10) {   //注意=10
        result=result*10;
        number=number/10;
    }
    
    while(x>0) {
        left=x/result;    //高位
        right=x%10;       //低位
        if(left!=right) {
            return false;
        }
        else {  //同时缩小两位 5005-5*1000=5 
            x=x-(left*result);     //注意不能是 x=x%(left*result) 1100110011 Runtime Error   
            result=result/100;
            x=x/10;
        }
    }
    return true;
}

Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

该题目主要是判断字符串是否是回文,其中仅仅是字母和数字组成,需要删除额外字符。注意:大小写字母可以相同'z'=‘Z’,大写字母转小写字母加32即可。
判读一个字符串是否是回文,一种方法可以将字符串倒置然后和原字符串进行比较。这里采用一种类似字符串翻转的方法,通过从前后两个方向来比较判断是否是回文。
自己AC感觉还行,代码如下:

//自定义函数 判断是否是数字或字母
bool IsNumberAlp(char ch)
{
    if( (ch<='Z'&&ch>='A') || (ch<='z'&&ch>='a') || 
        (ch<='9'&&ch>='0') ) {
        return true;
    }
    else {
        return false;
    }
}

//字符串回文判断 包括字母和数字 "aA" 返回True说明不区分大小写
bool isPalindrome(char* s) {
    int length;      //字符串长度
    int i,j;
    char left,right; //比较两个字符
    
    //s输出true ""
    if(s==NULL)
        return true;
    length=strlen(s);
    
    //数组下标i从前往后遍历 j从后往前遍历
    i=0;
    j=length-1;
    left=0;
    right=0;
    while(i<=j)
    {
        if(IsNumberAlp(s[i])==true) { //字母 数字
            left = s[i];
            if(left>='A'&&left<='Z') { //大写字母转小写字母加32(重点)
                left=left+32;
            }
        }
        else {
            i++;
            continue;
        }
        
        if(IsNumberAlp(s[j])==true) {
            right = s[j];
            if(right>='A'&&right<='Z') { 
                right=right+32;
            }
        }
        else {
            j--;
            continue;
        }
        
        if(left!=right) {
                return false;
            } else {
                i++;
                j--;
            }
    }
    return true;
}

        最后希望文章对你有所帮助,如果文章中有错误或不足之处,还请海涵~
        (By:Eastmount 2015-9-24 凌晨4点   http://blog.csdn.net/eastmount/)

目录
相关文章
|
3月前
|
Go
golang力扣leetcode 479.最大回文数乘积
golang力扣leetcode 479.最大回文数乘积
21 0
|
5月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
64 1
LeetCode | 234. 回文链表
LeetCode | 234. 回文链表
|
10天前
【力扣】409.最长回文串
【力扣】409.最长回文串
|
3月前
|
Go
golang力扣leetcode 1332. 删除回文子序列
golang力扣leetcode 1332. 删除回文子序列
12 0
|
3月前
|
Go
golang力扣leetcode 234.回文链表
golang力扣leetcode 234.回文链表
10 0
|
3月前
|
机器学习/深度学习
leetcode-234:回文链表
leetcode-234:回文链表
18 0
|
4月前
|
算法 vr&ar 图形学
☆打卡算法☆LeetCode 214. 最短回文串 算法解析
☆打卡算法☆LeetCode 214. 最短回文串 算法解析
|
4月前
|
存储 Java
1457. 二叉树中的伪回文路径 --力扣 --JAVA
给你一棵二叉树,每个节点的值为 1 到 9 。我们称二叉树中的一条路径是 「伪回文」的,当它满足:路径经过的所有节点值的排列中,存在一个回文序列。 请你返回从根到叶子节点的所有路径中 伪回文 路径的数目。
59 3
|
5月前
|
计算机视觉
234.回文链表(LeetCode)
234.回文链表(LeetCode)
234.回文链表(LeetCode)