精华内容
下载资源
问答
  • Palindrome

    2019-06-11 04:25:31
    Palindrome Number Validate Palindrome Shortest Palindrome Palindrome Partitioning Palindrome Partitioning II Longest Palindromic Substring

    Palindrome Number
    Validate Palindrome
    Shortest Palindrome
    Palindrome Partitioning
    Palindrome Partitioning II
    Longest Palindromic Substring

    展开全文
  • palindrome

    2017-10-27 21:48:44
    A string that reads the same thing as the inverse reads (excluding whitespace) is called palindrome.  Such as string: "Madam im Adam", "Dad" are palindrome. Design algorithm to determine whether a ...

    A string that reads the same thing as the inverse reads (excluding whitespace) is called palindrome. 

    Such as string: "Madam im Adam", "Dad" are palindrome.

    Design algorithm to determine whether a string is palindrome,

     requires the use of stack to achieve discrimination of palindrome.

    #include <iostream>
    #include <cstring>
    using namespace std;
    #define MaxSize 1000
    class SqStack
    {
    	public:
    		SqStack();
    		bool isEmpty();
    		void push(char ch);
    		char pop();
    		void Print();
    		void Palindrome();
    	private:
    		char data[MaxSize];
    		int top;
    };
    SqStack::SqStack()
    {
    	this->top=-1;
    }
    bool SqStack::isEmpty()
    {
    	if(this->top=-1)
    		return true;
    	else
    		return false;	
    }
    void SqStack::push(char ch)
    {
    	if(top == MaxSize-1)
    		cout<<"栈满"<<endl;
    	else
    	{
    		++top;
    		data[top] = ch;
    	}
    }
    char SqStack::pop()	
    {
    	char ch;
    	if(top==-1)
    	{
    		cout<<"栈空"<<endl;
    		return 0;
    	}
    	else
    	{
    		ch = data[top];
    		--top;
    	}
    	return ch;
    }
    void SqStack::Palindrome()
    {
    	bool tag = true;
    	int n = top;
    	for(int i =0;i<(top+1)/2;++i)
    	{
    		if(data[i]!=data[n])
    		{
    			tag = false;
    			break;
    		}
    		n--;
    	}
    	if(tag)
    		cout<<"回文数"<<endl;
    	else 
    		cout<<"非回文数"<<endl;
    }
    void SqStack::Print()	
    {
    	if(top==-1)
    		cout<<"栈空"<<endl;
    	else
    		cout<<data<<endl;
    }
    int main()
    {
    	SqStack sq1,sq2;
    	string s;
    	cin>>s;
    	char q;
    	cout<<"字符串长度:"<<s.length()<<endl;
    	for(int i= 0;i<s.length()/2;++i)
    	{
    		sq1.push(s[i]);
    		sq2.push(s[s.length()-1-i]);
    	}
    	cout<<"字符串为:"<<s<<endl; 
    	int tag=true;
    	for(int i= 0;i<s.length()/2;++i)
    	{
    		if(sq1.pop()!=sq2.pop())
    		{
    			tag = false;
    			break;
    		}
    	}
    	if(tag)
    		cout<<s<<"是回文数"<<endl;
    	else
    		cout<<s<<"不是回文数"<<endl;
    	//sq.Palindrome();
    	//sq.pop(q);//出栈 
    
    	return 0;
    }
    


    展开全文
  • palindrome-源码

    2021-03-22 08:09:21
    palindrome
  • Palindrome Goodie

    2020-12-26 19:38:02
    <div><p>Very simple, displays whether or not a string is a palindrome. Queried like so: <p>'is (query) a[n] a palindrome[?]' and 'isPalindrome (query)' <p>Results look like: '...
  • 1354. Palindrome. Again Palindrome Time limit: 1.0 second Memory limit: 64 MB Awordis the nonempty sequence of symbolsa1a2…an. Apalindromeis the worda1a2…anthat is read from the left to t...

    1354. Palindrome. Again Palindrome

    Time limit: 1.0 second Memory limit: 64 MB
    A word is the nonempty sequence of symbols a1a2an. A palindrome is the word a1a2an that is read from the left to the right and from the right to the left the same way (a1a2an = anan−1a1). If S1 = a1a2an and S2 = b1b2bm, then S1S2 = a1a2anb1b2bm. The input contains some word S1. You are to find a nonempty word S2 of the minimal length that S1S2 is a palindrome.

    Input

    The first input line contains S1 (it may consist only of the Latin letters). It’s guaranteed that the length of S1 doesn’t exceed 10000 symbols.

    Output

    S1S2.

    Samples

    inputoutput
    No
    
    NoN
    
    OnLine
    
    OnLineniLnO
    
    AbabaAab
    
    AbabaAababA
    
    Problem Author: Denis Nazarov Problem Source: USU Junior Championship March'2005
    ***************************************************************************************
    kmp没想出来,用简单法做的
    ***************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<cstdio>
     7 #include<queue>
     8 #include<vector>
     9 #include<stack>
    10 using namespace std;
    11 char  str[10011];
    12 int n,i,j,k;
    13 bool  judge(int x)//检查后缀的最大回文
    14  {
    15      int h,g;
    16      for(h=x,g=n-1;g>h;h++,g--)
    17        if(str[h]!=str[g])
    18         return false;
    19     return true;
    20  }
    21  int main()
    22  {
    23      scanf("%s",str);
    24      n=strlen(str);
    25      for(i=0;i<n;i++)
    26       cout<<str[i];
    27      for(i=1;i<n;i++)
    28       if(judge(i))
    29        break;
    30     for(j=i-1;j>=0;j--)
    31      cout<<str[j];
    32     cout<<endl;
    33     return 0;
    34 
    35 
    36  }
    View Code

     

    转载于:https://www.cnblogs.com/sdau--codeants/p/3275087.html

    展开全文
  • Palindrome Number

    2019-07-07 15:22:20
    Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121...

    一 问题描述

    Palindrome Number

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
    Example 1:

    Input: 121
    Output: true
    

    Example 2:

    Input: -121
    Output: false
    Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
    

    Example 3:

    Input: 10
    Output: false
    Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
    

    Follow up:
    Coud you solve it without converting the integer to a string?
    翻译:
    判定一个整数是不是回文串。一个是否是回文串取决于正着读或反着读是否一样。

    二 解法

    1. 第一解法(个人,转换字符串)

    分析:
    将数字转换成字符串数组之后,从两端往中间比较,很容易得出结论。
    代码:

    var isPalindrome = function(x) {
        
        let str = x.toString().split("");
        
        let len = str.length - 1;
        let mid = str.length / 2;
        
        for (let i = 0; i <= mid; i++) {
            if (str[i] !== str[len - i])
                return false
        }
        return true;
    };
    

    2. 第二解法(个人,非转字符串)

    分析:
    将数字通过数学计算生成一个相反排列的数字,与原数字比较即可。这时候,需要先判断是否是负数。并且,js中整数除法可以除出来小数,因此需要使用Math.floor()方法向下取整。
    代码:

    var isPalindrome = function(x) {
        
        let num = x;
        let arc = 0;
        
        if (x < 0)
            return false;
        
        while (num >= 10) {
            arc *= 10;
            arc += num % 10;
            num = Math.floor(num /10);
        }
        
        arc = arc * 10 + num;
        return arc === x;
        
    };
    

    3. 第三解法(个人,最优,非转字符串)

    分析:
    上述的通过数学计算,从头到尾完全反转,但实际上,是不需要这样的。回文串判断只需要前后半段比较。因此,通过新设置一个arc变量存储后半段数字,然后比较x是否大于arc,只要大于arc,就和前面的逻辑一样,一直生成后半段反转数字。
    之后,判断有两个标准:

    1. 是否相等,如果相等, 必定是回文串
    2. 前半段此时已经小于后半段,说明,如果前半段乘10加上后半段的个位数如果和后半段相等,也可以判定为回文串。

    注:这种判断方法不能正确处理0的问题。例如x = 10,x = 100。因为0 * 10 = 0。会干扰判断。因此在一开始需要排除掉这种情况。这种情况有两个特征,首先是x % 10 === 0,其次是x !== 0。

    代码:

        var isPalindrome = function(x) {
            
            let num = x;
            let arc = 0;
            
            if (x < 0 || x % 10 === 0 && x !== 0)
                return false;
            
            while (num > arc) {
                arc *= 10;
                arc += num % 10;
                num = Math.floor(num /10);
            }
        
            if(arc === num || num * 10 + arc % 10 === arc)
                return true;
        
            return false;  
        };
    

    结果:

    11509 / 11509 test cases passed.
    Status: Accepted
    Runtime: 164 ms
    Memory Usage: 45.3 MB
    Runtime:faster than 98%
    

    By DoubleJan
    2019.7.7

    展开全文
  • Shortest Palindrome

    2020-05-26 05:48:00
    Given a strings, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. Example 1: ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 15,655
精华内容 6,262
关键字:

palindrome