forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.0 KiB
37 lines
1.0 KiB
2 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
prices := []int{8, 4, 6, 2, 3}
|
||
|
fmt.Printf("打印结构体指针数组1:%v \n", finalPrices(prices))
|
||
|
prices = []int{1, 2, 3, 4, 5}
|
||
|
fmt.Printf("打印结构体指针数组2:%v \n", finalPrices(prices))
|
||
|
prices = []int{10, 1, 1, 6}
|
||
|
fmt.Printf("打印结构体指针数组3:%v ", finalPrices(prices))
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。
|
||
|
|
||
|
商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j] <= prices[i] 的 最小下标 ,如果没有满足条件的 j ,你将没有任何折扣。
|
||
|
|
||
|
请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。
|
||
|
*/
|
||
|
|
||
|
func finalPrices(prices []int) []int {
|
||
|
arr := prices
|
||
|
a:
|
||
|
for i, l := 0, len(prices); i < l; i++ {
|
||
|
for j := i + 1; j < l; j++ {
|
||
|
if prices[j] <= prices[i] {
|
||
|
arr[i] = prices[i] - prices[j]
|
||
|
continue a
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return arr
|
||
|
}
|