Skip to content

Commit 97eb405

Browse files
committed
init commit
0 parents  commit 97eb405

File tree

8 files changed

+239
-0
lines changed

8 files changed

+239
-0
lines changed

.github/workflows/go.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build_and_test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v3
17+
- name: Setup Go
18+
uses: actions/setup-go@v3
19+
with:
20+
go-version: 1.16
21+
- name: Build
22+
run: go build -v ./...
23+
- name: Test
24+
run: go test -v -cover ./...
25+
- name: BenchmarkTest
26+
run: go test -benchmem -run=none -bench ^BenchmarkTest sol/sol

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Dependency directories (remove the comment below to include it)
18+
# vendor/
19+
20+
# Go workspace file
21+
go.work

.gitlab-ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
stages:
2+
- test
3+
build and test:
4+
image: golang:1.16
5+
stage: test
6+
script:
7+
- go build -v ./...
8+
- go test -v -cover ./...
9+
- go test -benchmem -run=none -bench ^BenchmarkTest sol/sol

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
go test -v -cover ./...

READMED.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# golang_target_sum_input_array_sorted
2+
3+
Given a **1-indexed** array of integers `numbers` that is already ***sorted in non-decreasing order***, find two numbers such that they add up to a specific `target` number. Let these two numbers be `numbers[index1]` and `numbers[index2]` where `1 <= index1 < index2 <= numbers.length`.
4+
5+
Return *the indices of the two numbers,* `index1` *and* `index2`***added by one** as an integer array* `[index1, index2]` *of length 2.*
6+
7+
The tests are generated such that there is **exactly one solution**. You **may not** use the same element twice.
8+
9+
Your solution must use only constant extra space.
10+
11+
## Examples
12+
13+
**Example 1:**
14+
15+
```
16+
Input: numbers = [2,7,11,15], target = 9
17+
Output: [1,2]
18+
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
19+
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: numbers = [2,3,4], target = 6
26+
Output: [1,3]
27+
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
28+
29+
```
30+
31+
**Example 3:**
32+
33+
```
34+
Input: numbers = [-1,0], target = -1
35+
Output: [1,2]
36+
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
37+
38+
```
39+
40+
**Constraints:**
41+
42+
- `2 <= numbers.length <= $3 * 10^4$`
43+
- `1000 <= numbers[i] <= 1000`
44+
- `numbers` is sorted in **non-decreasing order**.
45+
- `1000 <= target <= 1000`
46+
- The tests are generated such that there is **exactly one solution**.
47+
48+
## 解析
49+
50+
給定一個由小到大排序過的整數陣列 numbers. 給定一個整數 target
51+
52+
要求實作一個演算法找出 numbers 中兩個數相加合的 target
53+
54+
並且要求空間複雜度必須要是 O(1)
55+
56+
因為是排序過的整數陣列
57+
58+
所以可以透過兩個 pointer , lp =0, rp = len(numbers) -1
59+
60+
當 lp < rp 時做以下判斷
61+
62+
當 numbers[lp] + numbers[rp] == target 則找到這兩個index 回傳 [lp + 1 , rp +1 ] // 因為題目要求是 1-indexed 的座標 而 golang 與 java 都是 0-indexed
63+
64+
當 numbers[lp] + numbers[rp] > target
65+
66+
因為左界已經到最小值,所以要讓兩數相加變小只能把右界向左移 更新 rp -=1
67+
68+
當 numbers[lp] + numbers[rp] < target
69+
70+
因為左界已經到最大值,所以要讓兩數相加變大只能把左界向右移 更新 lp +=1
71+
72+
當跑到最後都找不到則回傳 nil
73+
74+
詳細作法如下圖
75+
76+
![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/7956aeec-0364-49e7-ad71-5375d074fd6c/Untitled.png)
77+
78+
79+
80+
## 程式碼
81+
```go
82+
package sol
83+
84+
func twoSum(numbers []int, target int) []int {
85+
lp, rp := 0, len(numbers)-1
86+
for lp < rp {
87+
if numbers[lp]+numbers[rp] == target {
88+
return []int{lp + 1, rp + 1}
89+
}
90+
if numbers[lp]+numbers[rp] < target {
91+
lp++
92+
}
93+
if numbers[lp]+numbers[rp] > target {
94+
rp--
95+
}
96+
}
97+
return nil
98+
}
99+
100+
```
101+
102+
## 困難點
103+
104+
1. 要想出左右指標移動的條件
105+
106+
## Solve Point
107+
108+
- [x] 初始化 lp = 0, rp = len(numbers) - 1
109+
- [x] 當 lp < rp 時 做以下運算
110+
- [x] 當 numbers[lp] + numbers[rp] == target 則回傳 [lp+1, rp+1]
111+
- [x] 當 numbers[lp] + numbers[rp] > target 則更新 rp -= 1
112+
- [x] 當 numbers[lp] + numbers[rp] < target 則更新 lp += 1
113+
- [x] 當跑到 lp = rp 時 , 回傳 nil 代表沒有找到符合的 pair

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module sol
2+
3+
go 1.16

sol/solution.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sol
2+
3+
func twoSum(numbers []int, target int) []int {
4+
lp, rp := 0, len(numbers)-1
5+
for lp < rp {
6+
if numbers[lp]+numbers[rp] == target {
7+
return []int{lp + 1, rp + 1}
8+
}
9+
if numbers[lp]+numbers[rp] < target {
10+
lp++
11+
}
12+
if numbers[lp]+numbers[rp] > target {
13+
rp--
14+
}
15+
}
16+
return nil
17+
}

sol/solution_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package sol
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func BenchmarkTest(b *testing.B) {
9+
numbers := []int{2, 7, 11, 15}
10+
target := 9
11+
for idx := 0; idx < b.N; idx++ {
12+
twoSum(numbers, target)
13+
}
14+
}
15+
func Test_twoSum(t *testing.T) {
16+
type args struct {
17+
numbers []int
18+
target int
19+
}
20+
tests := []struct {
21+
name string
22+
args args
23+
want []int
24+
}{
25+
{
26+
name: "numbers = [2,7,11,15], target = 9",
27+
args: args{numbers: []int{2, 7, 11, 15}, target: 9},
28+
want: []int{1, 2},
29+
},
30+
{
31+
name: "numbers = [2,3,4], target = 6",
32+
args: args{numbers: []int{2, 3, 4}, target: 6},
33+
want: []int{1, 3},
34+
},
35+
{
36+
name: "numbers = [-1,0], target = -1",
37+
args: args{numbers: []int{-1, 0}, target: -1},
38+
want: []int{1, 2},
39+
},
40+
}
41+
for _, tt := range tests {
42+
t.Run(tt.name, func(t *testing.T) {
43+
if got := twoSum(tt.args.numbers, tt.args.target); !reflect.DeepEqual(got, tt.want) {
44+
t.Errorf("twoSum() = %v, want %v", got, tt.want)
45+
}
46+
})
47+
}
48+
}

0 commit comments

Comments
 (0)