NC134股票(无限次交易)(视频讲解+四种语言)

股票(无限次交易)

https://www.nowcoder.com/practice/9e5e3c2603064829b0a0bbfca10594e9?tpId=117&tqId=37846&rp=1&ru=%2Factivity%2Foj&qru=%2Fta%2Fjob-code-high%2Fquestion-ranking&tab=answerKey

- 1、题目描述:
图片说明

- 2、题目链接:

https://www.nowcoder.com/practice/9e5e3c2603064829b0a0bbfca10594e9?tpId=117&tqId=37846&rp=1&ru=%2Factivity%2Foj&qru=%2Fta%2Fjob-code-high%2Fquestion-ranking&tab=answerKey
-3、 设计思想:
图片说明
详细操作流程看下图:
图片说明

-4、视频讲解链接B站视频讲解

-5、代码:
c++版本:

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 计算最大收益
     * @param prices int整型vector 股票每一天的价格
     * @return int整型
     */
    int maxProfit(vector<int>&amp; prices) {
        // write code here
        int res = 0;//代表最终收益
        for(int i= 1;i &lt; prices.size();i ++){
            int temp = prices[i]-prices[i-1];//收益
            if(temp &gt; 0) res += temp;//如果为正收益就加和
        }
        return res;
    }
}; 

Java版本:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 计算最大收益
     * @param prices int整型一维数组 股票每一天的价格
     * @return int整型
     */
    public int maxProfit (int[] prices) {
        // write code here
        int res = 0;//代表最终收益
        for(int i= 1;i &lt; prices.length;i ++){
            int temp = prices[i]-prices[i-1];//收益
            if(temp &gt; 0) res += temp;//如果为正收益就加和
        }
        return res;
    }
}

Python版本:

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
# 计算最大收益
# @param prices int整型一维数组 股票每一天的价格
# @return int整型
#
class Solution:
    def maxProfit(self , prices ):
        # write code here
        res = 0#代表最终收益
        for i in range(1,len(prices)):
            temp = prices[i] - prices[i-1]#收益
            if temp &gt; 0: res += temp#如果为正收益就加和
        return res

JavaScript版本:

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * 计算最大收益
 * @param prices int整型一维数组 股票每一天的价格
 * @return int整型
 */
function maxProfit( prices ) {
    // write code here
        let res = 0;//代表最终收益
        for(let i= 1;i < prices.length;i ++){
            let temp = prices[i]-prices[i-1];//收益
            if(temp > 0) res += temp;//如果为正收益就加和
        }
        return res;
}
module.exports = {
    maxProfit : maxProfit
};
牛客题霸 文章被收录于专栏

本专栏主要是牛客题霸习题的讲解,有详细的考点分类,大家可以可以看看呦!!!

全部评论

相关推荐

10-20 15:26
门头沟学院 Java
桥头牛油火锅:这个比例不正常,简历的话项目经历放中间,项目功能分点可以再明确点,前面加“·”或者“1 2 3”,另外简历上的照片可以去外面摄影店拍一下,以后也会用到的,hr筛人也是多少会看的,毕竟世界是一个巨大的卡颜局嘛,还有有些hr由于消息太多可能没看到,后面可能会回来找你,要简历的还会多一点,我也是普2本,比例大致是600:90:15:3,当然我实力不太够,拿的offer比较少,慢慢来吧
点赞 评论 收藏
分享
11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
评论
3
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务