Skip to content

Commit 371330f

Browse files
committed
[E:60/533, M:57/974, H:6/387] add No: 167 Two Sum II - Input array is sorted
1 parent beae683 commit 371330f

File tree

18 files changed

+642
-3
lines changed

18 files changed

+642
-3
lines changed

questions/serial/中等/151/golang/solution/reverse-words-in-a-string.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66
"runtime/debug"
7-
"strings"
87
)
98

109
func Export(s string) string {
@@ -27,5 +26,5 @@ func Export(s string) string {
2726
/****************************************************/
2827

2928
func reverseWords(s string) string {
30-
strings.Index()
29+
3130
}

questions/serial/简单/167/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## [两数之和 II - 输入有序数组](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/)
2+
3+
给定一个已按照**_升序排列_ **的有序数组,找到两个数使得它们相加之和等于目标数。
4+
5+
函数应该返回这两个下标值 __ index1 和 index2,其中 index1 必须小于 index2__
6+
7+
**说明:**
8+
9+
* 返回的下标值(index1 和 index2)不是从零开始的。
10+
* 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。
11+
12+
**示例:**
13+
14+
`**输入:** numbers = [2, 7, 11, 15], target = 9
15+
**输出:** [1,2]
16+
**解释:** 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。`
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/167/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[2,7,11,15]
17+
9
18+
19+
*/
20+
21+
tests := []struct {
22+
name string
23+
input1 []int
24+
input2 int
25+
want []int
26+
}{
27+
{
28+
name: "test-[2,7,11,15]-9",
29+
input1: []int{2, 7, 11, 15},
30+
input2: 9,
31+
want: []int{1, 2},
32+
},
33+
{
34+
name: "test-[2,7,11,15]-17",
35+
input1: []int{2, 7, 11, 15},
36+
input2: 17,
37+
want: []int{1, 4},
38+
},
39+
{
40+
name: "test-[2,7,11,15]-22",
41+
input1: []int{2, 7, 11, 15},
42+
input2: 22,
43+
want: []int{2, 4},
44+
},
45+
{
46+
name: "test-[2,7,11,15]-18",
47+
input1: []int{2, 7, 11, 15},
48+
input2: 18,
49+
want: []int{2, 3},
50+
},
51+
{
52+
name: "test-[2,7,11,15]-26",
53+
input1: []int{2, 7, 11, 15},
54+
input2: 26,
55+
want: []int{3, 4},
56+
},
57+
}
58+
59+
testLog := leet.NewTestLog(len(tests))
60+
defer testLog.Render()
61+
62+
timeoutDuration := time.Second * 2
63+
64+
for idx, test := range tests {
65+
// 超时检测
66+
got := test.want
67+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
68+
got = solution.Export(test.input1, test.input2)
69+
cancel()
70+
})
71+
72+
if timeout {
73+
testLog.Fail(idx+1, test.name, "timeout")
74+
continue
75+
}
76+
77+
if !reflect.DeepEqual(test.want, got) {
78+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
79+
continue
80+
}
81+
82+
testLog.Pass(idx+1, test.name)
83+
}
84+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(numbers []int, target int) []int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", numbers, target)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return twoSum(numbers, target)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func twoSum(numbers []int, target int) []int {
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(numbers []int, target int) []int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", numbers, target)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return twoSum(numbers, target)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func twoSum(numbers []int, target int) []int {
29+
30+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## [两数之和 II - 输入有序数组](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/)
2+
3+
给定一个已按照**_升序排列_ **的有序数组,找到两个数使得它们相加之和等于目标数。
4+
5+
函数应该返回这两个下标值 __ index1 和 index2,其中 index1 必须小于 index2__
6+
7+
**说明:**
8+
9+
* 返回的下标值(index1 和 index2)不是从零开始的。
10+
* 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。
11+
12+
**示例:**
13+
14+
`**输入:** numbers = [2, 7, 11, 15], target = 9
15+
**输出:** [1,2]
16+
**解释:** 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。`
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/167/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[2,7,11,15]
17+
9
18+
19+
*/
20+
21+
tests := []struct {
22+
name string
23+
input1 []int
24+
input2 int
25+
want []int
26+
}{
27+
{
28+
name: "test-[2,7,11,15]-9",
29+
input1: []int{2, 7, 11, 15},
30+
input2: 9,
31+
want: []int{1, 2},
32+
},
33+
{
34+
name: "test-[2,7,11,15]-17",
35+
input1: []int{2, 7, 11, 15},
36+
input2: 17,
37+
want: []int{1, 4},
38+
},
39+
{
40+
name: "test-[2,7,11,15]-22",
41+
input1: []int{2, 7, 11, 15},
42+
input2: 22,
43+
want: []int{2, 4},
44+
},
45+
{
46+
name: "test-[2,7,11,15]-18",
47+
input1: []int{2, 7, 11, 15},
48+
input2: 18,
49+
want: []int{2, 3},
50+
},
51+
{
52+
name: "test-[2,7,11,15]-26",
53+
input1: []int{2, 7, 11, 15},
54+
input2: 26,
55+
want: []int{3, 4},
56+
},
57+
}
58+
59+
testLog := leet.NewTestLog(len(tests))
60+
defer testLog.Render()
61+
62+
timeoutDuration := time.Second * 2
63+
64+
for idx, test := range tests {
65+
// 超时检测
66+
got := test.want
67+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
68+
got = solution.Export(test.input1, test.input2)
69+
cancel()
70+
})
71+
72+
if timeout {
73+
testLog.Fail(idx+1, test.name, "timeout")
74+
continue
75+
}
76+
77+
if !reflect.DeepEqual(test.want, got) {
78+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
79+
continue
80+
}
81+
82+
testLog.Pass(idx+1, test.name)
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(numbers []int, target int) []int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", numbers, target)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return twoSum(numbers, target)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func twoSum(numbers []int, target int) []int {
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(numbers []int, target int) []int {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", numbers, target)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return twoSum(numbers, target)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func twoSum(numbers []int, target int) []int {
29+
30+
}

0 commit comments

Comments
 (0)