LeetCode刷题日记(Day15)

Problem 118. Pascal’s Triangle

  • 题目描述
    Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.

    In Pascal’s triangle, each number is the sum of the two numbers directly above it.

    For example,

    Input: 5
    Output:
    [
         [1],
        [1,1],
       [1,2,1],
      [1,3,3,1],
     [1,4,6,4,1]
    ]
    
  • 解题思路
    这是一道杨辉三角题,每一行满足以下规律:

  1. 首元素和末元素均为1;
  2. 设cur为当前行,v为上一行,则满足 cur[i] = v[i] + v[i-1];
  • 代码实现
class Solution {
public:
    vector<vector<int> > generate(int numRows) {
        vector<vector<int> > res;
        if(numRows == 0)
            return res;
        vector<int> v = {1};
        res.push_back(v);
        for(int i = 1; i < numRows; ++i){
        	v.push_back(0);
            vector<int> cur = v;
            for(int j = 1; j <= i; ++j)
                cur[j] = v[j-1] + v[j];
            res.push_back(cur);
            v = cur;
        }
        return res;
    }
};

Problem 121. Best Time to Buy and Sell Stock

  • 题目描述
    Say you have an array for which the i element is the price of a given stock on day i.

    If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

    Note that you cannot sell a stock before you buy one.

    Example

    Input: [7,1,5,3,6,4]
    Output: 5
    Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
                 Not 7-1 = 6, as selling price needs to be larger than buying price.
    
    Input: [7,6,4,3,1]
    Output: 0
    Explanation: In this case, no transaction is done, i.e. max profit = 0.
    
  • 解题思路
    要求最大收益,只要对数组 prices 进行一次遍历,令 _min 为当前访问过的元素中的最小值, prices[i] - _min 为当前利润,每次对当前利润求最大值即可。

  • 代码实现

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            if(prices.size() == 0)
                return 0;
            int res = 0, _min = prices[0];
            for(int i = 1; i < prices.size(); ++i){
                _min = min(prices[i], _min);
                res = max(res, prices[i]-_min);
            }
            return res;
        }
    };
    

Problem 122. Best Time to Buy and Sell Stock II

  • 题目描述
    Say you have an array for which the ith element is the price of a given stock on day i.

    Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

    Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

    Example 1:

    Input: [7,1,5,3,6,4]
    Output: 7
    Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
                 Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
    

    Example 2:

    Input: [1,2,3,4,5]
    Output: 4
    Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
                 Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
                 engaging multiple transactions at the same time. You must sell before buying again.
    
  • 解题思路
    与上题相比,本题可以多次进行 buy 和 sell,即求累积最大利润 res。对 prices 从后向前进行遍历,如果 prices[i] > prices[i-1],表明在 i-1 和 i 这段时间内可以获得利润为 prices[i] - prices[i-1],将其累计到 res 中。

  • 代码实现

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int len = prices.size();
        if(len == 0)
            return 0;
        int res = 0;
        for(int i = len-1; i >0; --i){
            if(prices[i] > prices[i-1])
                res += prices[i] - prices[i-1];
        }
        return res;
    }
};

Problem 125. Valid Palindrome

  • 题目描述
    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

    Note: For the purpose of this problem, we define empty string as valid palindrome.

    Example 1:

    Input: "A man, a plan, a canal: Panama"
    Output: true
    

    Example 2:

    Input: "race a car"
    Output: false
    
  • 解题思路
    本题要判断一个输入的字符串是否是回文串(忽略字符串中的非字母和数字,同时不考虑字母的大小写)。思路是定义下标 left 和 right,分别指向字符串 s 的左边和右边的有效元素(字母或数字),并判断 s[left] 和 s[right] 是否相等,若不相等直接返回 false,相等则 left 和 right 下标继续向里移动。

    需要注意的是,在对字符串s进行处理的时候,不能申请额外的空间储存新的字符串,否则会造成 Memory Limit Exceeded。

  • 代码实现

class Solution {
public:
    bool isPalindrome(string s) {
        int len = s.length();
        if(len == 0 || len == 1)
            return true;
        int left = 0, right = len-1;
        while(left < right){
            while(!(isalnum(s[left])) && left < right)
                left++;
            while(!(isalnum(s[right])) && left < right)
                right--;
            if(tolower(s[left++]) != tolower(s[right--]))
                return false;
        }
        return true;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值