LeetCode happy to brush the question Matching fifth day expression matching --9Palindrome Number10Regular Expression

9. Palindrome Number Easy flood title, originally intended for direct comparison of reverse addition is equal, but the range is -2147483647-2147483648 i.e. int is inputted but the reverse is not necessarily int int would exceed the range, the switch to the memory array way through the array and compare the before and after, but there will be parity corresponding problem, so count the number of times plus, feeling that Italy itself is wanted us to use int to string do. emmm and so next time I saw, and then sum up the, ha ha ha. .

Easy
14581309FavoriteShare

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.
class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0) return false;
        int temp[11]={0},i=0,none=0;
        bool flag1=0;
        while(x!=0)
        {
            temp[i++]=x%10;
            x/=10;
        }
        //i就是次数
        //cout<<i<<endl;
        if(i%2==1)
        {
            none=(i-1)/2;
            flag1=1;
        }

        for(int t=0;t<i;t++)
        {
            if(t==none)
                continue;
            if(temp[t]!=temp[i-1-t])
                return false;
        }
        return true;
    }
};

 

1. Function:

C ++: string class, substr (2) taken other than the first two. substr (2,5), taken from 2 to 5.

 

topic:

10. Regular Expression Matching

 

Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.

 

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

 

The matching should cover the entire input string (not partial).

 

Note:

 

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

 

Example 1:

 

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

 

Example 2:

 

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

 

Example 3:

 

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

 

Example 4:

 

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".

 

Example 5:

 

INPUT: 
S = "Mississippi" 
P = "MIS * IS * P *." 
The Output: to false 

Title quoted:
the original matching string S P. * Prior representative character can be 0 or 1. The representative can be any character
given two strings to see if they match
thoughts:
one by one to determine whether a recursive match, only one function to determine when found in front match, put the delete front, back, and then passed into the function.
Resolution code:
#include <the iostream> 
#include <math.h> 
#include <Map> 
#include < String > 
#include < String .h> 
#include <stdio.h> 
#include <the cstdlib>
 the using  namespace STD;
 class Solution 
{ 
public :
     BOOL IsMatch ( String S, String p) 
    { 
        // three cases discussed, the difference between this character and the matching question is normal. * and interference
         // determination empty cases, but this is only the case of an empty p, and if the following further taking into account the space s may 
        IF (p.empty ())
             return s.empty ();
        // p is not only a character, and behind there * p, p illustrate the current first character can not have unlimited or multiple occurrences
         // two classification is discussed below return, if not, put the first two P's recycling removed, otherwise, the first comparator are the same, is removed in s first comparison continues 
        iF (P.SIZE ()> . 1 && P [ . 1 ] == ' * ' )
             return IsMatch (s, p.substr ( 2 )) || (s.empty () && (S [! 0 ] == P [ 0 ] || P [ 0 ] == ' . ' ) && IsMatch (s.substr ( . 1 ), P));
         the else 
            // is not the case above description may occur. or a character, but only consider the first and only appearance
             // * before considering the two, but has now excluded this case
             // so we judge whether the air see current matches, match after removing
            return !s.empty()&&(s[0]==p[0]||p[0]=='.')&&isMatch(s.substr(1),p.substr(1));

    }
};
int main()
{
    int n=100;
    bool flag=false;
    string s1="abbcde",s2="a*bcd.";
    Solution s;
    flag=s.isMatch(s1,s2);
    cout<<flag<<endl;
    return 0;
}

 

 

 

Reproduced in: https: //www.cnblogs.com/Marigolci/p/11039087.html

Guess you like

Origin blog.csdn.net/weixin_33885676/article/details/93369891