牛牛是一位聪明的农民,他养了很多奶牛。每天,他都会观察奶牛的价格,并试图在合适的时机买入奶牛,并在将来的某一天卖出奶牛,以获取最大的利润。 给定一个整数数组 prices,其中 prices[i] 表示第 i 天奶牛的价格,请你计算出牛牛能够通过一次买卖获取的最大利润。如果无法获取利润,则返回 0。 请你帮助牛牛解决这个问题。
示例1
输入
[7, 1, 5, 3, 6, 4]
输出
5
示例2
输入
[7, 6, 4, 3, 1]
输出
0
备注:
数组长度范围:1 数组元素范围:0
加载中...
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型vector * @return int整型 */ int maxProfit(vector
& prices) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param prices int整型一维数组 # @return int整型 # class Solution: def maxProfit(self , prices ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (List
prices) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ function maxProfit( prices ) { // write code here } module.exports = { maxProfit : maxProfit };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param prices int整型一维数组 # @return int整型 # class Solution: def maxProfit(self , prices: List[int]) -> int: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ func maxProfit( prices []int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @param pricesLen int prices数组长度 * @return int整型 */ int maxProfit(int* prices, int pricesLen ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param prices int整型一维数组 # @return int整型 # class Solution def maxProfit(prices) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ def maxProfit(prices: Array[Int]): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ fun maxProfit(prices: IntArray): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ export function maxProfit(prices: number[]): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ func maxProfit ( _ prices: [Int]) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ pub fn maxProfit(&self, prices: Vec
) -> i32 { // write code here } }
[7, 1, 5, 3, 6, 4]
5
[7, 6, 4, 3, 1]
0