mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Merge pull request #591 from X-shuffle/master
更新 0056.合并区间 go版本(优化空间复杂度,不开辟新数组)
This commit is contained in:
@ -176,29 +176,26 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```Go
|
```golang
|
||||||
func merge(intervals [][]int) [][]int {
|
func merge(intervals [][]int) [][]int {
|
||||||
|
//先从小到大排序
|
||||||
sort.Slice(intervals,func(i,j int)bool{
|
sort.Slice(intervals,func(i,j int)bool{
|
||||||
return intervals[i][0]<intervals[j][0]
|
return intervals[i][0]<intervals[j][0]
|
||||||
})
|
})
|
||||||
|
//再弄重复的
|
||||||
res:=[][]int{}
|
for i:=0;i<len(intervals)-1;i++{
|
||||||
prev:=intervals[0]
|
if intervals[i][1]>=intervals[i+1][0]{
|
||||||
|
intervals[i][1]=max(intervals[i][1],intervals[i+1][1])//赋值最大值
|
||||||
for i:=1;i<len(intervals);i++{
|
intervals=append(intervals[:i+1],intervals[i+2:]...)
|
||||||
cur :=intervals[i]
|
i--
|
||||||
if prev[1]<cur[0]{
|
|
||||||
res=append(res,prev)
|
|
||||||
prev=cur
|
|
||||||
}else {
|
|
||||||
prev[1]=max(prev[1],cur[1])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res=append(res,prev)
|
return intervals
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
func max(a,b int)int{
|
func max(a,b int)int{
|
||||||
if a > b { return a }
|
if a>b{
|
||||||
|
return a
|
||||||
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -216,7 +216,30 @@ class Solution: # 贪心思路
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```golang
|
||||||
|
func maxProfit(prices []int, fee int) int {
|
||||||
|
var minBuy int = prices[0] //第一天买入
|
||||||
|
var res int
|
||||||
|
for i:=0;i<len(prices);i++{
|
||||||
|
//如果当前价格小于最低价,则在此处买入
|
||||||
|
if prices[i]<minBuy{
|
||||||
|
minBuy=prices[i]
|
||||||
|
}
|
||||||
|
//如果以当前价格卖出亏本,则不卖,继续找下一个可卖点
|
||||||
|
if prices[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:
|
||||||
```Javascript
|
```Javascript
|
||||||
// 贪心思路
|
// 贪心思路
|
||||||
|
@ -159,7 +159,26 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
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:
|
||||||
```Javascript
|
```Javascript
|
||||||
var monotoneIncreasingDigits = function(n) {
|
var monotoneIncreasingDigits = function(n) {
|
||||||
|
Reference in New Issue
Block a user