题目:
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 (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

思路:
低买高卖,贪心算法。每次判断是否需要买和卖的时候,都要用当前的值和下一个值比较,如果有利可图,则操作;否则,不处理。算法复杂度:O(n)

代码:

class Solution {
public:

const int NULL_PRICE = -1;
int maxProfit(vector<int>& prices) {
    unsigned int max_limit = prices.size();
    int buy_price = NULL_PRICE;
    int max_profit = 0;
    for (unsigned int i = 0; i < max_limit; i++)
    {
        if ((i + 1) >= max_limit)
        {
            break;
        }

        int total_price = prices[i];
        int tomorrow_prive = prices[i + 1];

        if (total_price > tomorrow_prive)
        {
            if (buy_price != NULL_PRICE)
            {
                max_profit += total_price - buy_price;
                buy_price = NULL_PRICE;
            }
        }

        if (total_price < tomorrow_prive)
        {
            if (buy_price == NULL_PRICE)
            {
                buy_price = total_price;
            }
        }
    }

    if (buy_price != NULL_PRICE)
    {
        max_profit += prices[max_limit - 1] - buy_price;
    }

    return max_profit;
}

};
总结:
1、思路比较简单,主要用到的是贪心算法,即局部最优。
2、注意最小值有可能为0。

昨天下班回家的时候,婆婆带着儿子在小区里玩,跟小家伙打招呼,懵懵懂懂的也不知道是妈妈吧。

一起回家后,儿子的目光一直追随着我,放冰包,放奶,换鞋,去卫生间,洗手,做完这一系列事情后,儿子那期盼的眼睛里,眼泪已泫然欲滴。儿子在到家的第一时间张开手要妈妈,可是妈妈却没有及时的回应,儿子,你是伤心了吧。

看到儿子这样的表现,虽然好累好累,但是满满的幸福感,满满的感动,儿子,你也是很想妈妈的,对吧!

首先,欢迎大家来“芹声勤语”做客。 芹声勤语专栏已设置许久,本想在开栏当天发表文章,但是因各种原因,到今天才有时间来写开篇辞。在此非常感谢博主给专栏命此名,实乃一莫大的惊喜,因本人名“勤”,昵称“芹”,博主的用心非常感动。

开此专栏的灵感来源于和博主的一次谈话。可能是人在经历很多事情之后,慢慢的学会了淡忘,当你习惯了这种淡忘以后,好多事情在脑海里只能找到模糊的印记,可却不能清晰回忆,特别是正在经历的“孕傻”时期。每天上班,工作,吸奶,下班,带娃,睡觉,这样快节奏的规律性的生活往往让一天的时间不知不觉的溜走,却回忆不起来过去的一天经历的有意义的事情,因为记忆已经习惯性的选择忘记,可是每天真的会有很多欣喜。人生在回忆过去的时候不应该是空洞无内容的,应该是充满感动或无奈、欣喜或悲、幸福或者不幸福的,那种经历过的存在感,在你回忆的时候应该是依旧能让你感动的。因此跟博主提出想要有一片地方,专门记录这些点点滴滴,当前行休息的时候,还可以看过去走过的路。

网络世界的你我他,生活中可能并无交集,但是却又紧密的联系在一起,欢迎大家来芹声勤语的小家,分享自己的点点滴滴。

题目:
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

思路:
这道题是判断两个二叉树是否相等,各个节点和分支是否相等。依然使用递归遍历法,终结的条件是两个节点都为空,两个节点不一样,然后循环遍历左右节点,如果有一个节点不一样,那么返回的值始终为false。算法复杂度:O(N)

代码一:

/**

  • Definition for a binary tree node.
  • struct TreeNode {
  • int val;
  • TreeNode *left;
  • TreeNode *right;
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  • };
    */

class Solution {
public:

bool isSameTree(TreeNode* p, TreeNode* q)
{
    if( p == NULL && q == NULL )
    {
        return true;
    }
    else if( p == NULL && q != NULL )
    {
        return false;
    }
    else if( p != NULL && q == NULL )
    {
        return false;
    }
     
    bool bResult = true;
    if( p->val == q->val )
    {
        bResult = true;
    }
    else
    {
        bResult = false;
    }
     
    bool bSameLeft = isSameTree(p->left, q->left);
    bool bSameRight = isSameTree(p->right, q->right);
     
    if( !bSameLeft )
    {
        bResult = false;
    }
    if( !bSameRight )
    {
        bResult = false;
    }
    return bResult;
}

};
代码二:

/**

  • Definition for a binary tree node.
  • struct TreeNode {
  • int val;
  • TreeNode *left;
  • TreeNode *right;
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  • };
    */

class Solution {
public:

bool isSameTree(TreeNode* p, TreeNode* q)
{
    if( (p == NULL && q != NULL) || (p != NULL && q == NULL ) )
    {
        return false;
    }
     
    if( p == NULL && q == NULL )
    {
        return true;
    }

    if( p->val != q->val )
    {
        return false;
    }
     
    return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
}   

};
总结:
1、二叉树的遍历:左节点递归遍历,右节点递归遍历。算法复杂度都是O(N)
2、两次提交成功,可以第一次是因为少了个";"号错误。
3、算法可以再优化,代码同样可以再优化。代码简洁、效率高、可读性强,这样才是一段好代码。代码二比一精简了一倍,但其可读性却更好了!