diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 99c870c6..441cc63c 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -176,30 +176,27 @@ class Solution: ``` Go: -```Go +```golang func merge(intervals [][]int) [][]int { - sort.Slice(intervals, func(i, j int) bool { - return intervals[i][0]=intervals[i+1][0]{ + intervals[i][1]=max(intervals[i][1],intervals[i+1][1])//赋值最大值 + intervals=append(intervals[:i+1],intervals[i+2:]...) + i-- + } + } + return intervals } -func max(a, b int) int { - if a > b { return a } - return b +func max(a,b int)int{ + if a>b{ + return a + } + return b } ``` diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index 6dc95a2d..b0a64bd2 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -216,7 +216,30 @@ class Solution: # 贪心思路 ``` Go: - +```golang +func maxProfit(prices []int, fee int) int { + var minBuy int = prices[0] //第一天买入 + var res int + for i:=0;i=minBuy&&prices[i]-fee-minBuy<=0{ + continue + } + //可以售卖了 + if prices[i]>minBuy+fee{ + //累加每天的收益 + res+=prices[i]-minBuy-fee + //更新最小值(如果还在收获利润的区间里,表示并不是真正的卖出,而计算利润每次都要减去手续费,所以要让minBuy = prices[i] - fee;,这样在明天收获利润的时候,才不会多减一次手续费!) + minBuy=prices[i]-fee + } + } + return res +} +``` Javascript: ```Javascript // 贪心思路 diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index 7cb24236..82f7f3ae 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -159,7 +159,26 @@ class Solution: ``` Go: - +```golang +func monotoneIncreasingDigits(N int) int { + s := strconv.Itoa(N)//将数字转为字符串,方便使用下标 + ss := []byte(s)//将字符串转为byte数组,方便更改。 + n := len(ss) + if n <= 1 { + return N + } + for i:=n-1 ; i>0; i-- { + if ss[i-1] > ss[i] {//前一个大于后一位,前一位减1,后面的全部置为9 + ss[i-1] -= 1 + for j := i ; j < n; j++ {//后面的全部置为9 + ss[j] = '9' + } + } + } + res, _ := strconv.Atoi(string(ss)) + return res +} +``` Javascript: ```Javascript var monotoneIncreasingDigits = function(n) {