Skip to content

Commit 7e13b40

Browse files
authored
Added task 1
1 parent 3667e7f commit 7e13b40

File tree

7 files changed

+129
-26
lines changed

7 files changed

+129
-26
lines changed

.github/workflows/build.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
name: check
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: setup
15+
uses: actions/setup-go@v2
16+
with:
17+
go-version: ^1.21
18+
19+
- name: checkout
20+
uses: actions/checkout@v4
21+
22+
- name: fmt
23+
run: diff -u <(echo -n) <(gofmt -d -s .)
24+
25+
- name: vet
26+
run: go vet ./...
27+
28+
- name: golangci-lint
29+
uses: golangci/golangci-lint-action@v2
30+
31+
- name: test
32+
run: go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...

.github/workflows/go.yml

-26
This file was deleted.

Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
all: fmt vet golangci-lint golint test
2+
3+
check: fmt vet golangci-lint golint
4+
5+
fmt:
6+
test `go fmt ./... | wc -l` -eq 0
7+
8+
vet:
9+
go vet ./...
10+
11+
golangci-lint:
12+
golangci-lint run ./...
13+
14+
golint:
15+
test `golint ./... | grep -v 'should not use dot imports' | wc -l` -eq 0
16+
17+
test:
18+
go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
19+

go.mod

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/javadev/algorithm-go
2+
3+
go 1.21
4+
5+
require github.com/stretchr/testify v1.8.2
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

leetcode/0001.two-sum/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [Two Sum](https://leetcode.com/problems/two-sum/)

leetcode/0001.two-sum/two_sum.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode
2+
3+
func twoSum1(nums []int, target int) []int {
4+
for i := range nums {
5+
for j := range nums {
6+
if i != j && (nums[i]+nums[j]) == target {
7+
return []int{i, j}
8+
}
9+
}
10+
}
11+
return nil
12+
}
13+
14+
func twoSum2(nums []int, target int) []int {
15+
lookup := make(map[int]int)
16+
for i, v := range nums {
17+
j, ok := lookup[v]
18+
lookup[target-v] = i
19+
if ok {
20+
return []int{j, i}
21+
}
22+
}
23+
return nil
24+
}

leetcode/0001.two-sum/two_sum_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
var (
9+
case1, target1, result1 = []int{2, 7, 11, 15}, 9, []int{0, 1}
10+
case2, target2, result2 = []int{7, 11, 15}, 22, []int{0, 2}
11+
case3, target3, result3 = []int{11, 15}, 26, []int{0, 1}
12+
)
13+
14+
func TestTwoSum1(t *testing.T) {
15+
if !reflect.DeepEqual(twoSum1(case1, target1), result1) {
16+
t.Fail()
17+
}
18+
if !reflect.DeepEqual(twoSum1(case2, target2), result2) {
19+
t.Fail()
20+
}
21+
if !reflect.DeepEqual(twoSum1(case3, target3), result3) {
22+
t.Fail()
23+
}
24+
if twoSum1(nil, 0) != nil {
25+
t.Fail()
26+
}
27+
}
28+
29+
func TestTwoSum2(t *testing.T) {
30+
if !reflect.DeepEqual(twoSum2(case1, target1), result1) {
31+
t.Fail()
32+
}
33+
if !reflect.DeepEqual(twoSum2(case2, target2), result2) {
34+
t.Fail()
35+
}
36+
if !reflect.DeepEqual(twoSum2(case3, target3), result3) {
37+
t.Fail()
38+
}
39+
if twoSum2(nil, 0) != nil {
40+
t.Fail()
41+
}
42+
}

0 commit comments

Comments
 (0)