zoukankan      html  css  js  c++  java
  • 每日一练leetcode

    商品折扣后的最终价格

    给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。

    商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j] <= prices[i] 的 最小下标 ,如果没有满足条件的 j ,你将没有任何折扣。

    请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。

    class Solution {
        public int[] finalPrices(int[] prices) {
            int length = prices.length;
            int pro = 0;
            int re = pro + 1;
            while(pro < length){
                while(re < length){
                if(prices[re] <= prices[pro]){
                    prices[pro] = prices[pro] - prices[re];
                    break;
                    }
                    re++;
                }
                pro++;
                re = pro + 1;
                
            }
            return prices;

        }
    }
     
    此题是个简单题使用双指针挨个遍历即可
  • 相关阅读:
    开发工具
    人脸识别
    mysql 3813:Column check constraint 'tb_course_chk_3' references other column.
    sleep()和wait()的异同
    线程通信——wait(),notify(),notifyAll()
    对王建民老师的评价&JAVA结课自我总结
    JAVA学习日报 12/19
    JAVA学习日报 12/18
    JAVA学习日报 12/17
    JAVA学习日报 12/11
  • 原文地址:https://www.cnblogs.com/nenu/p/15355694.html
Copyright © 2011-2022 走看看