mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-08 02:15:01 +08:00
Compare commits
10 Commits
dependabot
...
master
Author | SHA1 | Date | |
---|---|---|---|
d78a9e0927 | |||
a190dbe625 | |||
0ff840fe0d | |||
93adea0ba2 | |||
bc9ec086e4 | |||
b75de5582c | |||
281ae8befc | |||
25c03cf13a | |||
b878af0ccf | |||
c5bcb82ce7 |
17
.github/workflows/deploy.yml
vendored
17
.github/workflows/deploy.yml
vendored
@ -28,12 +28,11 @@ jobs:
|
||||
hugo -D --minify
|
||||
|
||||
- name: Deploy to Server # 第四步,rsync推文件
|
||||
uses: AEnterprise/rsync-deploy@v1.0 # 使用别人包装好的步骤镜像
|
||||
env:
|
||||
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }} # 引用配置,SSH私钥
|
||||
ARGS: -avz --delete --exclude='*.pyc' # rsync参数,排除.pyc文件
|
||||
SERVER_PORT: '22' # SSH端口
|
||||
FOLDER: ./website/public/* #推送的文件夹,路径相对于代码仓库的根目录
|
||||
SERVER_IP: ${{ secrets.SSH_HOST }} # 引用配置,服务器的host名(IP或者域名domain.com)
|
||||
USERNAME: ${{ secrets.SSH_USERNAME }} # 引用配置,服务器登录名
|
||||
SERVER_DESTINATION: /var/www/books/leetcode/ # 部署到目标文件夹
|
||||
uses: appleboy/ssh-action@v0.1.3 # 使用别人包装好的步骤镜像
|
||||
with:
|
||||
host: ${{ secrets.SSH_HOST }}
|
||||
username: ${{ secrets.SSH_USERNAME }}
|
||||
key: ${{ secrets.DEPLOY_KEY }}
|
||||
port: ${{ secrets.SERVER_PORT }}
|
||||
script: |
|
||||
rsync -avz --delete --exclude='*.pyc' ./website/public/ ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }}:${{ secrets.SERVER_DESTINATION }}
|
||||
|
2
go.mod
2
go.mod
@ -26,5 +26,5 @@ require (
|
||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
||||
github.com/kr/pretty v0.3.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
golang.org/x/net v0.17.0 // indirect
|
||||
golang.org/x/net v0.7.0 // indirect
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -27,8 +27,8 @@ github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJ
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
|
||||
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
|
@ -2,16 +2,11 @@ package leetcode
|
||||
|
||||
func plusOne(digits []int) []int {
|
||||
for i := len(digits) - 1; i >= 0; i-- {
|
||||
digits[i]++
|
||||
if digits[i] != 10 {
|
||||
// no carry
|
||||
if digits[i] != 9 {
|
||||
digits[i]++
|
||||
return digits
|
||||
}
|
||||
// carry
|
||||
digits[i] = 0
|
||||
}
|
||||
// all carry
|
||||
digits[0] = 1
|
||||
digits = append(digits, 0)
|
||||
return digits
|
||||
return append([]int{1}, digits...)
|
||||
}
|
||||
|
26
leetcode/0134.Gas-Station/Gas.go
Normal file
26
leetcode/0134.Gas-Station/Gas.go
Normal file
@ -0,0 +1,26 @@
|
||||
package leetcode
|
||||
|
||||
func canCompleteCircuit(gas []int, cost []int) int {
|
||||
totalGas := 0
|
||||
totalCost := 0
|
||||
currGas := 0
|
||||
start := 0
|
||||
|
||||
for i := 0; i < len(gas); i++ {
|
||||
totalGas += gas[i]
|
||||
totalCost += cost[i]
|
||||
currGas += gas[i] - cost[i]
|
||||
|
||||
if currGas < 0 {
|
||||
start = i + 1
|
||||
currGas = 0
|
||||
}
|
||||
}
|
||||
|
||||
if totalGas < totalCost {
|
||||
return -1
|
||||
}
|
||||
|
||||
return start
|
||||
|
||||
}
|
47
leetcode/0134.Gas-Station/Gas_test.go
Normal file
47
leetcode/0134.Gas-Station/Gas_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question134 struct {
|
||||
para134
|
||||
ans134
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para134 struct {
|
||||
one []int
|
||||
two []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans134 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem134(t *testing.T) {
|
||||
qs := []question134{
|
||||
|
||||
{
|
||||
para134{[]int{1, 2, 3, 4, 5}, []int{3, 4, 5, 1, 2}},
|
||||
ans134{3},
|
||||
},
|
||||
|
||||
{
|
||||
para134{[]int{2, 3, 4}, []int{3, 4, 3}},
|
||||
ans134{-1},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 134------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans134, q.para134
|
||||
fmt.Printf("【input】:%v, %v 【output】:%v\n", p.one, p.two, canCompleteCircuit(p.one, p.two))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
88
leetcode/0134.Gas-Station/README.md
Normal file
88
leetcode/0134.Gas-Station/README.md
Normal file
@ -0,0 +1,88 @@
|
||||
# [134. Gas Stations](https://leetcode.com/problems/gas-station/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given a list of gas stations on a circular route, where the amount of gas at station i is gas[i], and the cost to travel from station i to its next station (i+1) is cost[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
|
||||
|
||||
Write a function that returns the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
|
||||
Output: 3
|
||||
Explanation: Start at station 3 (index 3), you have 4 liters of gas. Now you have a total of =0+4=4 liters of gas.
|
||||
Travel to station 4. Now you have 4 - 1 + 5 = 8 liters of gas.
|
||||
Travel to station 0. Now you have 8 - 2 + 1 = 7 liters of gas.
|
||||
Travel to station 1. Now you have 7 - 3 + 2 = 6 liters of gas.
|
||||
Travel to station 2. Now you have 6 - 4 + 3 = 5 liters of gas.
|
||||
Travel to station 3 again. This time you will have consumed 5 liters of gas, which is exactly enough for you to return to station 3.
|
||||
Therefore, 3 can be the starting index.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: gas = [2,3,4], cost = [3,4,3]
|
||||
Output: -1
|
||||
Explanation: You cannot start at station 0 or 1 because there is not enough gas to get you to the next station.
|
||||
We start at station 2, we get 4 liters of gas. Now you have a total of =0+4=4 liters of gas.
|
||||
Travel to station 0. Now you have 4 - 3 + 2 = 3 liters of gas.
|
||||
Travel to station 1. Now you have 3 - 3 + 3 = 3 liters of gas.
|
||||
You cannot return to station 2 because it requires 4 liters of gas but you only have 3 liters in your tank.
|
||||
Therefore, no matter what, you cannot travel around the ring.
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `gas.length == n`
|
||||
- `cost.length == n`
|
||||
- `1 <= n <= 10^5`
|
||||
- `0 <= gas[i], cost[i] <= 10^4`
|
||||
|
||||
## 题目大意
|
||||
|
||||
在一条环路上有 n 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
|
||||
|
||||
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
|
||||
|
||||
给定两个整数数组 gas 和 cost ,如果你可以按顺序绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1 。如果存在解,则 保证 它是 唯一 的。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 初始化总油量(totalGas)和总消耗(totalCost)为0,起始点(start)为0,当前油量(currGas)为0。
|
||||
遍历每个加油站,累加油量和消耗,计算当前油量。
|
||||
如果当前油量小于0,说明从上一个加油站到当前加油站的油量不足以到达下一个加油站,因此将起始点设置为当前加油站的下一个位置,并将当前油量重置为0。
|
||||
遍历结束后,如果总油量小于总消耗,说明无法完成整个旅程,返回-1;否则返回起始点。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func canCompleteCircuit(gas []int, cost []int) int {
|
||||
totalGas := 0
|
||||
totalCost := 0
|
||||
currGas := 0
|
||||
start := 0
|
||||
|
||||
for i := 0; i < len(gas); i++ {
|
||||
totalGas += gas[i]
|
||||
totalCost += cost[i]
|
||||
currGas += gas[i] - cost[i]
|
||||
|
||||
if currGas < 0 {
|
||||
start = i + 1
|
||||
currGas = 0
|
||||
}
|
||||
}
|
||||
|
||||
if totalGas < totalCost {
|
||||
return -1
|
||||
}
|
||||
|
||||
return start
|
||||
}
|
||||
|
||||
```
|
@ -6,7 +6,7 @@ func findTargetSumWays(nums []int, S int) int {
|
||||
for _, n := range nums {
|
||||
total += n
|
||||
}
|
||||
if S > total || (S+total)%2 == 1 || S+total < 0 {
|
||||
if S+total < 0 || S > total || (S+total)%2 == 1 {
|
||||
return 0
|
||||
}
|
||||
target := (S + total) / 2
|
||||
|
@ -44,6 +44,7 @@ void hello (int n){
|
||||
|
||||
```c
|
||||
bool isPrime (int n){
|
||||
if (num <= 1) return false;
|
||||
for( int x = 2 ; x * x <= n ; x ++ )
|
||||
if( n % x == 0 )
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user