41-45题 题解

41First Missing Positive

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.


//模拟下,找第一个没有的正整数
class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        int *p = (int*)malloc(sizeof(int)*(nums.size()+1));
        memset(p, 0, sizeof(int)*(nums.size()+1));
        for(int i = 0; i < nums.size(); i++)
        {
            if(nums[i] <= 0 || nums[i] > nums.size()) continue;
            p[nums[i]] = 1;
        }
        for(int i = 1; i <= nums.size(); i++)
        {
            if(!p[i]) return i;
        }
        return nums.size()+1;
    }
};
42 Trapping Rain Water

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.


The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcosfor contributing this image!

//模拟哈,记录前面最大与最小的编号
class Solution {
public:
    int trap(vector<int>& height) {
        int mx, mi;
        mx = mi = 0;
        int ans = 0;
        for(int i = 1; i < height.size(); i++)
        {
            if(height[i] > height[mi] && height[mx] > height[mi])
            {
                int h = min(height[i], height[mx]);
                for(int j = mx+1; j < i; j++)
                {
                     if(h < height[j]) continue;
                     ans += h - height[j];
                     height[j] = h;
                }
            }
            mi = height[i] >= height[mi] ? mi : i;
            mx = height[i] >= height[mx] ? i : mx;
            if(mi < mx) mi = mx;
        }
        return ans;
    }
};


43 Multiply Strings

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.
//模拟下乘法
class Solution {
public:
    string multiply(string num1, string num2) {
        int res[301];
        memset(res, 0, sizeof(res));
        int len1 = num1.length(), len2 = num2.length();
        int len = len1+len2;
        int cur, a, b, p;
        for(int i = len1-1; i >= 0; i--)
        {
            a = num1[i] - '0';
            p = 0;
            cur = len;
            for(int j = len2-1; j >= 0; j--)
            {
                b = num2[j] - '0';
                int w = a*b+p+res[cur];
                res[cur] = w%10;
                p = w/10;
                cur--;
            }
            if(p) res[cur] += p;
            len--;
        }
        int i = 1;
        len = len1+len2;
        while(i <= len && res[i] == 0) i++;
        string s;
        if(i > len) return "0";
        for(; i <= len; i++) s += res[i] + '0';
        return s;
    }
};
44 Wildcard Matching

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
//只记录最后一个*的位置,因为前面都匹配过了,如果所有的*都递归一遍,会超时
class Solution {
public:
    bool isMatch(string s, string p) {
        int ss, pp = -1;
        int r = s.length(), rr = p.length();
        int l, ll;
        l = ll = 0;
        while(l < r)
        {
            if(s[l] == p[ll] || p[ll] == '?') l++, ll++;
            else if(p[ll] == '*') ss = l, pp = ll++;
            else if(pp != -1) l = ++ss, ll = pp+1;
            else return false;
        }
        while(ll < rr && p[ll] == '*') ll++;
        return ll == rr;
    }
};

//dp[i][j] 就是s串前i个与p串前j个匹配
class Solution {
public:
    bool isMatch(string s, string p) {
        int r = s.length(), rr = p.length();
        int dp[r+1][rr+1];
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;
        for(int i = 0; i < rr; i++)
        {
            if(p[i] == '*') dp[0][i+1] = 1;
            else break;
        }
        for(int i = 1; i <= r; i++)
        {
            for(int j = 1; j <= rr; j++)
            {
                if((s[i-1] == p[j-1] || p[j-1] == '?') && dp[i-1][j-1])
                    dp[i][j] = 1;
                else if(p[j-1] == '*') dp[i][j] |= dp[i][j-1]|dp[i-1][j]|dp[i-1][j-1];
            }
        }
        return dp[r][rr];
    }
};
45 Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

For example:
Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

Note:
You can assume that you can always reach the last index.

//遍历一遍就好了
class Solution {
public:
    int jump(vector<int>& nums) {
        if(nums.size() <= 1) return 0;
        int ans = 1, d = 0, r;
        while(d + nums[d] < nums.size()-1)
        {
            r = -1;
            for(int i = d+1; i <= d+nums[d]; i++)
                if(r == -1 || r + nums[r] < i + nums[i]) r = i;
            d = r;
            ans++;
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值