算法设计与分析leetcode作业第一周题解

divide-and-conquer

  • 第一题 Maximum Subarray(53题)

题意:

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

题解:使用数组array作为辅助数组,array[i]表示的是从0到i的连续子数组的最大值。在计算array[i]的时候,若array[i]是负值,则array[i]不变,否则array[i]为array[i]+array[i-1];

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        if (nums.size() == 0)
            return 0;
        vector<int> array;
        array.push_back(nums[0]);
        for (int i = 1;i < nums.size(); i++) {
            if (array[i-1] > 0)
                array.push_back(array[i-1]+nums[i]);
            else
                array.push_back(nums[i]);
        }
        int result = array[0];
        for (int i = 1; i < array.size(); i++)
            if (array[i] > result)
                result = array[i];
        return result;
    }
};
  • 第二题 Majority Element(169题)

题意:

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:

Input: [3,2,3]
Output: 3

Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2

题解:

将数组先从小到大排序后,取处于中间值的数即可。因为当一个数出现的次数大于全部数字个数的一半的时候,将这些数排序后处于中间位置的一定是这个出现次数大于一半的那个数。

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        return nums[nums.size()/2];
    }
};
  • 第三题 Search a 2D Matrix II(240题)

题意:

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

Example:

Consider the following matrix:

[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if (matrix.size() == 0 || matrix[0].size() == 0)
            return false;
        int i = 0, j = matrix[0].size()-1;
        while (i < matrix.size() && j >= 0) {
            if (matrix[i][j] == target)
                return true;
            if (matrix[i][j] < target)
                i++;
            else
                
                j--;
        }
        return false;
    }
};
  • 第四题 Different Ways to Add Parentheses(241题)

题意:

Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +- and *.

Example 1:

Input: "2-1-1"
Output: [0, 2]
Explanation: 
((2-1)-1) = 0 
(2-(1-1)) = 2

Example 2:

Input: "2*3-4*5"
Output: [-34, -14, -10, -10, 10]
Explanation: 
(2*(3-(4*5))) = -34 
((2*3)-(4*5)) = -14 
((2*(3-4))*5) = -10 
(2*((3-4)*5)) = -10 
(((2*3)-4)*5) = 10

题解:

利用分治的思想,从左至右遍历字符串,遇到“+”“-”“*”的符号时,将符号左右两边分成两个字符串,再调用函数递归求解

class Solution {
public:
    vector<int> diffWaysToCompute(string input) {
        string s1,s2;
        vector<int> res,res1,res2;
        s1.clear();
        s2.clear();
        res.clear();
        res1.clear();
        res2.clear();
        for (int i = 0; i < input.size(); i++) {
            if (input[i] == '-' || input[i] == '+' || input[i] == '*') {
                s1 = input.substr(0,i);
                s2 = input.substr(i+1,input.length()-i-1);
                res1 = diffWaysToCompute(s1);
                res2 = diffWaysToCompute(s2);
                for (int m = 0; m < res1.size(); m++) {
                    for (int n = 0; n < res2.size(); n++) {
                        if (input[i] == '-')
                            res.push_back(res1[m]-res2[n]);
                        else if (input[i] == '+')
                            res.push_back(res1[m]+res2[n]);
                        else
                            res.push_back(res1[m]*res2[n]);
                    }
                }
            }
        }
        if (res.size() == 0)
            res.push_back(string_to_int(input));
        return res;
    }
    
    int string_to_int(string input) {
        int res = 0;
        for (int i = 0; i < input.size(); i++) {
            res = res*10+(input[i]-'0');
        }
        return res;
    } 
    
};

 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值