【35-40】LeetCode:Python解题

41. First Missing Positive【Hard】

Given an unsorted integer array, find the first missing positive integer.

For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.

Your algorithm should run in O(n) time and uses constant space.

Solution 1:

class Solution(object):
    def firstMissingPositive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 1
        m = max(nums)
        l = [1.1 for i in range(m+2)]

        for i in nums:
            if i >= 0:
                l[i] = 0 

        minNum = l.index(0)
        return l[minNum:].index(1.1)+minNum if minNum < 2 else 1   

Solution 2:

 class Solution(object):
    def firstMissingPositive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        for any list whose length is N, the first missing positive must be in range [1,...,N+1]
        """
        N = len(nums)
        # move every value to the position of its value
        for target in nums:
            while target < N+1 and target > 0 and target != nums[target-1]:
                nums[target-1], target= target, nums[target-1]
        # find first location where the index doesn't match the value
        for cursor in range(N):
            if nums[cursor] != cursor+1:
                return cursor+1
        return N+1

42. Trapping Rain Water【Hard】

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

For example,

Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

image

Solution:

 class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        left, right = 0, len(height)-1
        left_max = right_max = 0
        ans = 0
        while left < right:
            if height[left] < height[right]:
                if height[left] > left_max:
                    left_max = height[left] 
                else:
                    ans += left_max - height[left]
                left += 1
            else:
                if height[right] > right_max:
                    right_max = height[right] 
                else:
                    ans += right_max - height[right]
                right -= 1

        return ans 

Discussion:

Using 2 pointers:https://leetcode.com/problems/trapping-rain-water/solution/

43. Multiply Strings【Medium】

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.

Note:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

    Solution:

    class Solution(object):
    def multiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        dic = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}
        lens = len(num1)+len(num2)
        product = [0]*lens
        for i in reversed(num2):
            lens = index = lens - 1
            for j in reversed(num1):
                product[index] += dic[i]*dic[j]
                product[index-1] += product[index] / 10
                product[index] = product[index] % 10
                index -= 1
        for i in range(len(product)):
            if product[i] != 0:
                return ''.join(map(str,product[i:]))
    
        return '0' 

44. Multiply Strings【Hard】

Implement wildcard pattern matching with support for ‘?’ and ‘*’.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

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", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false

Solution:


class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        len_s, len_p= len(s), len(p)
        dp = [True] + [False]*len_s      
        if len_p - p.count('*') > len_s:
            return False 

        for c in p:
            if c != '*':
                for i in range(len_s-1,-1,-1): 
                    dp[i+1] = dp[i] and (s[i] == c or c == '?')
            else:
                for i in range(len_s):
                    dp[i+1] = dp[i+1] or dp[i]
            dp[0] = dp[0] and c == '*'
        return dp[-1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值