leetcode11

Wildcard Matching
看的大神的答案。当p[pPointer] == ‘?’ or p[pPointer] == s[sPointer]的时候,两个指针都向后移一位,当p[pPointer] == ‘*’的时候,用star记录下p当前的位置,pPointer向后移,ss记录s当前的位置,如果上面的条件都不满足了,看star是不是-1,不是的话,把pPointer拉回到star的下一个,ss加一,这里是看*能匹配多少。
比如s=’abcabab’,p=’a*ab’,star=1,当pPointer==4的时候,p[pPointer]!=s[sPointer],所以pPointer又被拉回了2,sPointer被拉回了4,因为ss要先加1。

class Solution:
    # @param s, an input string
    # @param p, a pattern string
    # @return a boolean
    def isMatch(self, s, p):
        len_s = len(s); len_p = len(p)
        pPointer = sPointer = ss = 0
        star = -1
        while sPointer < len_s:
            if pPointer < len_p and (p[pPointer] == '?' or p[pPointer] == s[sPointer]):
                sPointer += 1; pPointer += 1;
                continue
            if pPointer < len_p and p[pPointer] == '*':
                star = pPointer; pPointer +=1; ss = sPointer
                continue
            if star != -1:
                pPointer = star + 1; ss += 1; sPointer = ss
                continue
            return False
        while pPointer < len_p and p[pPointer] == '*':
            pPointer += 1
        return pPointer == len_p

Jump Game II
太可怕了,这方法。。。什么时候我才能自己想出来。
对于每次lastReach < i 时,说明跳上一次不到i,需要cnt++

class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        curReach=0 #当前能跳到的最远距离
        lastReach=0 #上次最远可以跳到的距离
        count=0
        n=len(nums)
        for i in range(n):
            if lastReach1
            curReach=max(curReach,nums[i]+i)
            print i,lastReach,curReach
        return count

Maximum Subarray
。。。这么简单的都做了这么久

#coding=utf-8
class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        res=[]
        res.append(nums[0])
        for i in range(1,len(nums)):
            if res[i-1]+nums[i]else:
                res.append(res[i-1]+nums[i])
        return max(res)

Permutations
每次拿出一个数,剩下的数递归

#coding=utf-8


class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        if len(nums)==0:
            return []
        if len(nums)==1:
            return [nums]
        res=[]
        for i in range(len(nums)):
            for j in self.permute(nums[:i]+nums[i+1:]):
                res.append([nums[i]]+j)
        return res

你可能感兴趣的:(leecode)