restructure solutions dir
This commit is contained in:
parent
f9ddad5f88
commit
ccb8b5673b
10 changed files with 0 additions and 0 deletions
32
solutions/1/q125/solution.go
Normal file
32
solutions/1/q125/solution.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package q125
|
||||
|
||||
func isAlphanumeric(c byte) bool {
|
||||
return c >= '0' && c <= '9' ||
|
||||
c >= 'a' && c <= 'z'
|
||||
}
|
||||
|
||||
func toLower(c byte) byte {
|
||||
if c >= 'A' && c <= 'Z' {
|
||||
return 'a' + c - 'A'
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func isPalindrome(s string) bool {
|
||||
conv := make([]byte, 0, len(s))
|
||||
for i := range len(s) {
|
||||
c := toLower(s[i])
|
||||
if isAlphanumeric(c) {
|
||||
conv = append(conv, c)
|
||||
}
|
||||
}
|
||||
|
||||
for i := range len(conv) / 2 {
|
||||
if conv[i] != conv[len(conv)-1-i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var _ = isPalindrome
|
||||
23
solutions/1/q134/solution.go
Normal file
23
solutions/1/q134/solution.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package q134
|
||||
|
||||
func canCompleteCircuit(gas []int, cost []int) int {
|
||||
invalidated := 0
|
||||
start, current, tank := 0, 0, 0
|
||||
|
||||
for {
|
||||
tank = tank + gas[current] - cost[current]
|
||||
current = (current + 1) % len(gas)
|
||||
if tank < 0 {
|
||||
invalidated += (current - start + len(gas)) % len(gas)
|
||||
if invalidated >= len(gas) || current == start {
|
||||
return -1
|
||||
}
|
||||
start = current
|
||||
tank = 0
|
||||
} else if current == start {
|
||||
return start
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var _ = canCompleteCircuit
|
||||
34
solutions/1/q134/solution_test.go
Normal file
34
solutions/1/q134/solution_test.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package q134
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_canCompleteCircuit(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string // description of this test case
|
||||
// Named input parameters for target function.
|
||||
gas []int
|
||||
cost []int
|
||||
want int
|
||||
}{
|
||||
{
|
||||
gas: []int{1, 2, 3, 4, 5},
|
||||
cost: []int{3, 4, 5, 1, 2},
|
||||
want: 3,
|
||||
},
|
||||
{
|
||||
gas: []int{2, 3, 4},
|
||||
cost: []int{3, 4, 3},
|
||||
want: -1,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := canCompleteCircuit(tt.gas, tt.cost)
|
||||
if got != tt.want {
|
||||
t.Errorf("canCompleteCircuit() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
26
solutions/1/q135/solution.go
Normal file
26
solutions/1/q135/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q135
|
||||
|
||||
func candy(ratings []int) int {
|
||||
n := len(ratings)
|
||||
candies := make([]int, n)
|
||||
|
||||
for i := range n {
|
||||
candies[i] = 1
|
||||
if i > 0 && ratings[i-1] < ratings[i] {
|
||||
candies[i] = candies[i-1] + 1
|
||||
}
|
||||
}
|
||||
for i := n - 2; i >= 0; i-- {
|
||||
if ratings[i+1] < ratings[i] {
|
||||
candies[i] = max(candies[i], candies[i+1]+1)
|
||||
}
|
||||
}
|
||||
|
||||
sum := 0
|
||||
for _, c := range candies {
|
||||
sum += c
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
var _ = candy
|
||||
24
solutions/1/q150/solution.go
Normal file
24
solutions/1/q150/solution.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package q150
|
||||
|
||||
func twoSum(numbers []int, target int) []int {
|
||||
l, r := 0, len(numbers)-1
|
||||
|
||||
for l < r {
|
||||
for numbers[r]+numbers[l] > target {
|
||||
r--
|
||||
}
|
||||
if numbers[r]+numbers[l] == target {
|
||||
return []int{l + 1, r + 1}
|
||||
}
|
||||
|
||||
for numbers[r]+numbers[l] < target {
|
||||
l++
|
||||
}
|
||||
if numbers[r]+numbers[l] == target {
|
||||
return []int{l + 1, r + 1}
|
||||
}
|
||||
}
|
||||
return []int{} // impossible
|
||||
}
|
||||
|
||||
var _ = twoSum
|
||||
Loading…
Add table
Add a link
Reference in a new issue