9. Palindrome Number Easy大水题,本来打算直接倒序相加对比是否相等,但是int 的范围是-2147483647-2147483648 即输入的是int但倒序后不一定是int会超出范围,所以改用了存数组的方法,前后遍历数组并对比,但是会有奇偶对应的问题,所以还要加上次数的统计,感觉题意本身是想让我们用int转字符串来做。emmm等下回我见到了,再总结把,哈哈哈。。
Easy
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.函数:
C++:string类,substr(2) 截取除开头两个以外的。substr(2,5)截取从2到5的。
题目:
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 lettersa-z
.p
could be empty and contains only lowercase lettersa-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*." Output: false
题目转述:
原串S与之匹配的P。*代表之前的字符可以是0个或1个。.代表可以是任意字符
给定两个字符串看他们是否匹配
思想:
逐个递归判断是否匹配,判断的只有一个函数,当发现前面匹配时,就把前面的删除,后面的接着传入函数。
解决代码:
#include<iostream> #include<math.h> #include<map> #include <string> #include<string.h> #include<stdio.h> #include <cstdlib> using namespace std; class Solution { public: bool isMatch(string s,string p) { //分三种情况讨论,这个和正常的字符匹配题区别是有.和*的干扰 //判断空情况,但这只是p的空情况,如果下面还要考虑到s空的可能 if(p.empty()) return s.empty(); //p不只有一个字符,且p后面有*,说明p当前的第一个字符可以没有或者出现无限多次 //下面return的两个就是分类讨论,如果没有,就把P的前两个去掉再循环,否则,比较第一个是否相同,去掉s中的第一个继续比较 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)); else //都不是上面的情况,说明可能出现.或者纯字符,但目前只用考虑第一位,只有出现 //*才考虑两位,但目前已排除这种情况 //所以就判断是否为空,看当前是否匹配,去掉后是否匹配 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; }
愿为天下目,萃聚六路华