Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita72bbac

Browse files
Merge pull requestyoungyangyang04#407 from yangxk201396/master
添加 1005.K次取反后最大化的数组和.md Golang版本 添加 0134.加油站.md Golang版本
2 parents7bbc8ba +04b30d3 commita72bbac

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

‎problems/0134.加油站.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,25 @@ class Solution:
240240
```
241241

242242
Go:
243+
```go
244+
funccanCompleteCircuit(gas []int,cost []int)int {
245+
curSum:=0
246+
totalSum:=0
247+
start:=0
248+
fori:=0; i <len(gas); i++ {
249+
curSum += gas[i] - cost[i]
250+
totalSum += gas[i] - cost[i]
251+
if curSum <0 {
252+
start = i+1
253+
curSum =0
254+
}
255+
}
256+
if totalSum <0 {
257+
return -1
258+
}
259+
return start
260+
}
261+
```
243262

244263
#"diff-7303e5c71a8207887099cf22a685e7950bcc58f48f9b7ffc6fbf8933fc3e663f-245-264-0" data-selected="false" role="gridcell" tabindex="-1" valign="top">245
264
```Javascript

‎problems/1005.K次取反后最大化的数组和.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ class Solution:
138138
```
139139

140140
Go:
141+
```Go
142+
funclargestSumAfterKNegations(nums []int,Kint)int {
143+
sort.Slice(nums,func(i, jint)bool {
144+
return math.Abs(float64(nums[i])) > math.Abs(float64(nums[j]))
145+
})
146+
147+
fori:=0; i <len(nums); i++ {
148+
if K >0 && nums[i] <0 {
149+
nums[i] = -nums[i]
150+
K--
151+
}
152+
}
153+
154+
if K%2 ==1 {
155+
nums[len(nums)-1] = -nums[len(nums)-1]
156+
}
157+
158+
result:=0
159+
fori:=0; i <len(nums); i++ {
160+
result += nums[i]
161+
}
162+
return result
163+
}
164+
```
141165

142166

143167
#"diff-b4e612cb25b06ed4e4aea9b6593c1c507b68beeafa44141bfe85bb4036c32778-empty-empty-0" data-selected="false" role="gridcell" tabindex="-1" valign="top" colSpan="4">

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp