leetcode题解日练--2016.9.12

不给自己任何借口

今日题目:

1、正则表达式匹配

2、在有序数组中查找特定元素

今日摘录:

大抵浮生若梦,姑且此处销魂。
——曾国藩

10. Regular Expression Matching | Difficulty: Hard

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).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch(“aa”,”a”) → false
isMatch(“aa”,”aa”) → true
isMatch(“aaa”,”aa”) → false
isMatch(“aa”, “a*”) → true
isMatch(“aa”, “.*”) → true
isMatch(“ab”, “.*”) → true
isMatch(“aab”, “c*a*b”) → true

tag:DP|回溯|字符串
题意:字符串匹配问题,只有.和*两种特殊符号。

思路:
只有当模式串和字符串同时等于\0,才可以认为两个串匹配。
在匹配中,对于每个位的匹配可以分为三种情况
1、(相应位匹配 || 模式串为.&&字符串不是\0)&& 模式串下一位是*
2、(相应位匹配||模式串为.&&字符串不是\0)&& 模式串下一位不是*
3、相应位不匹配 &&(模式位不为.||字符串是\0)
对应1,最复杂。分为取0,取1,*>=2三种情况。
*取0对应跳过当前匹配位,继续寻找patter的下一个匹配位,str不变,pattern+2
*取1对应当前匹配位算一次成功匹配,str+1,pattern+2
*取>=2对应一次成功匹配,继续匹配字符串的下一位是否匹配,str+1,pattern不变
三者取或。即只要有一种情况能匹配成功认为字符串就是匹配成功的。
对应2,相当于一次成功匹配,str+1,pattern+1
对应3,匹配失败,直接返回false

class Solution {
private:
    int sLen,pLen;
public:
    bool isMatch(string s, string p) {
        sLen = s.size(),pLen = p.size();
        return helper(s,p,0,0);
    }
    bool helper(const string &s,const string &p,int str,int pat)
    {
        if(str==sLen && pat==pLen)    return true;
        if(str!=sLen && pat>pLen)    return false;
        if(pat<pLen-1 && p[pat+1]=='*')
        {
            if( s[str]==p[pat] || (p[pat]=='.' && str!=sLen))
                /*
                helper(s,p,str,pat+2):模式串未匹配
                helper(s,p,str+1,pat):模式串已经匹配成功,尝试匹配下一个字符串
                helper(s,p,str+1,pat+2):模式串已经成功匹配,并且不匹配下一个字符串内容
                */
                return helper(s,p,str,pat+2) ||  helper(s,p,str+1,pat);//||helper(s,p,str+1,pat+2);
            else
                return helper(s,p,str,pat+2);
        }
        if(s[str]==p[pat] || p[pat]=='.' && str!=sLen)
            return helper(s,p,str+1,pat+1);
        return false;
    }
};

结果:62ms

33. Search in Rotated Sorted Array | Difficulty: Hard

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

Subscribe to see which companies asked this question

tag:数组|二分

题意:在旋转数组中查找特定元素,数组中无重复元素。
思路:
1、直接二分的思想求解

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int size = nums.size();
        if(size<=0) return false;
        int left = 0,right=size-1;
        while(left<right)
        {
            int mid = left+(right-left)/2;

            if(nums[mid]==target)   return mid;
            //如果当前中间元素没找到的情况下,此时分为三种情况:
            //第一种情况是当前mid左边元素是排序好的,这个时候如果target比left元素大比mid元素小,此时target一定在mid的左边,否则就一定不在左边
            if(nums[mid]>nums[left])
            {
                if(nums[mid]>target&&nums[left]<=target)    right = mid-1;
                else    left = mid+1;
            }
            //第二种情况是当前mid右边的元素是排序好的,这个时候如果target大于mid小于right,则target一定在mid右边,否则就一定不在右边
            else if(nums[left]>nums[mid])
            {
                if(nums[mid]<target&&nums[right]>=target)   left = mid+1;
                else    right = mid-1;
            }
            //第三种情况是mid和left相等并且都不等于target,此时无法判断target在mid左边还是右边,所以只能逐个遍历
            else    left++;
        }

        return (nums[left]==target) ?left:-1;
    }
};

结果:3ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值