import java.util.*; public class Solution { public int maxProfit (int[] prices) { // 只进行一次买入和卖出 int ret =0; //双层遍历 int n = prices.length; for(int i=0;i<n;i++){ for(int j=i;j <n;j++){ ret = Math.max(ret,prices[j]-prices[i]); } } return ret; } }
import java.util.*; public class Solution { public int maxProfit (int[] prices) { // 只进行一次买入和卖出 int ret =0; //双层遍历 int n = prices.length; for(int i=0;i<n;i++){ for(int j=i;j <n;j++){ ret = Math.max(ret,prices[j]-prices[i]); } } return ret; } }