Skip to content

Commit f1df55b

Browse files
author
彭立衡
committed
feat(242|349|350|976): has been finish those problem
1 parent 2750c38 commit f1df55b

File tree

14 files changed

+329
-2
lines changed

14 files changed

+329
-2
lines changed

favicon.png

501 Bytes
Loading

kit/137.SingleNumber2/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package singleNumber
33
func singleNumber(nums []int) int {
44
a, b := 0, 0
55
for i := range nums {
6-
a ^= nums[i] &^ b
7-
b ^= nums[i] &^ a
6+
a = (a ^ nums[i]) &^ b
7+
b = (b ^ nums[i]) &^ a
88
}
99
return a
1010
}

kit/242.ValidAnagram/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# [242. Valid Anagram](https://leetcode.com/problems/valid-anagram/)
2+
3+
## 2019/05/15
4+
5+
### 题目 💗[easy]

kit/242.ValidAnagram/main.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package isAnagram
2+
3+
import "fmt"
4+
5+
func isAnagram(s string, t string) bool {
6+
for i := range t {
7+
fmt.Println(s[i] + t[i])
8+
}
9+
return true
10+
}

kit/242.ValidAnagram/main_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package isAnagram
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type para struct {
10+
one string
11+
two string
12+
}
13+
14+
type ans struct {
15+
one bool
16+
}
17+
18+
type question struct {
19+
p para
20+
a ans
21+
}
22+
23+
func Test_OK(t *testing.T) {
24+
ast := assert.New(t)
25+
26+
qs := []question{
27+
question{
28+
p: para{
29+
one: "anagram",
30+
two: "nagaram",
31+
},
32+
a: ans{
33+
one: true,
34+
},
35+
},
36+
}
37+
38+
for _, q := range qs {
39+
a, p := q.a, q.p
40+
ast.Equal(a.one, isAnagram(p.one, p.two), "输入:%v", p)
41+
}
42+
43+
// ast.Panics(func() { longestPalindrome([]int{}, []int{}) }, "对空切片求中位数,却没有panic")
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [349. Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)
2+
3+
## 2019/05/15
4+
5+
### 题目 💗[easy]
6+
7+
交集
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package intersection
2+
3+
func intersection(nums1 []int, nums2 []int) []int {
4+
res := []int{}
5+
for i := 0; i < len(nums1); i++ {
6+
for j := 0; j < len(nums2); j++ {
7+
if nums1[i] == nums2[j] && indexOf(res, nums1[i]) == -1 {
8+
res = append(res, nums1[i])
9+
}
10+
}
11+
}
12+
return res
13+
}
14+
15+
func indexOf(nums []int, val int) int {
16+
for i := range nums {
17+
if nums[i] == val {
18+
return i
19+
}
20+
}
21+
return -1
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package intersection
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type para struct {
10+
one []int
11+
two []int
12+
}
13+
14+
type ans struct {
15+
one []int
16+
}
17+
18+
type question struct {
19+
p para
20+
a ans
21+
}
22+
23+
func Test_OK(t *testing.T) {
24+
ast := assert.New(t)
25+
26+
qs := []question{
27+
question{
28+
p: para{
29+
one: []int{1, 2, 2, 1},
30+
two: []int{2, 2},
31+
},
32+
a: ans{
33+
one: []int{2},
34+
},
35+
},
36+
}
37+
38+
for _, q := range qs {
39+
a, p := q.a, q.p
40+
ast.Equal(a.one, intersection(p.one, p.two), "输入:%v", p)
41+
}
42+
43+
// ast.Panics(func() { longestPalindrome([]int{}, []int{}) }, "对空切片求中位数,却没有panic")
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [349. Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)
2+
3+
## 2019/05/15
4+
5+
### 题目 💗[easy]
6+
7+
交集
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package intersect
2+
3+
func intersect(nums1 []int, nums2 []int) []int {
4+
res := []int{}
5+
n, m := len(nums1), len(nums2)
6+
for i := 0; i < n; i++ {
7+
for j := 0; j < m; j++ {
8+
if nums1[i] == nums2[j] {
9+
res = append(res, nums1[i])
10+
nums1 = append(nums1[:i], nums1[i+1:]...)
11+
nums2 = append(nums2[:j], nums2[j+1:]...)
12+
n--
13+
m--
14+
i--
15+
j--
16+
break
17+
}
18+
}
19+
}
20+
return res
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package intersect
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type para struct {
10+
one []int
11+
two []int
12+
}
13+
14+
type ans struct {
15+
one []int
16+
}
17+
18+
type question struct {
19+
p para
20+
a ans
21+
}
22+
23+
func Test_OK(t *testing.T) {
24+
ast := assert.New(t)
25+
26+
qs := []question{
27+
question{
28+
p: para{
29+
one: []int{1, 2, 2, 1},
30+
two: []int{2},
31+
},
32+
a: ans{
33+
one: []int{2},
34+
},
35+
},
36+
question{
37+
p: para{
38+
one: []int{4, 9, 5},
39+
two: []int{9, 4, 9, 8, 4},
40+
},
41+
a: ans{
42+
one: []int{4, 9},
43+
},
44+
},
45+
}
46+
47+
for _, q := range qs {
48+
a, p := q.a, q.p
49+
ast.Equal(a.one, intersect(p.one, p.two), "输入:%v", p)
50+
}
51+
52+
// ast.Panics(func() { longestPalindrome([]int{}, []int{}) }, "对空切片求中位数,却没有panic")
53+
54+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# [976. Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/)
2+
3+
## 2019/05/15
4+
5+
### 题目 💗[easy]
6+
7+
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths.
8+
9+
给定一个数组 A,长度>0. 返回最长的由这三个数组成的非零数组周长长度.
10+
11+
---
12+
13+
If it is impossible to form any triangle of non-zero area, return 0.
14+
15+
如果不能返回非零区域长方形,返回 0.
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package largestPerimeter
2+
3+
import (
4+
"sort"
5+
)
6+
7+
func largestPerimeter(A []int) int {
8+
sort.Ints(A)
9+
res := 0
10+
for i := len(A) - 1; i > 1; i-- {
11+
if A[i] < A[i-1]+A[i-2] {
12+
res := A[i] + A[i-1] + A[i-2]
13+
return res
14+
}
15+
}
16+
return res
17+
}
18+
19+
// func largestPerimeter(A []int) int {
20+
// res := 0
21+
// for i := 0; i < len(A)-2; i++ {
22+
// for j := i + 1; j < len(A)-1; j++ {
23+
// for k := j + 1; k < len(A); k++ {
24+
// arr := []int{
25+
// A[i],
26+
// A[j],
27+
// A[k],
28+
// }
29+
// sort.Ints(arr)
30+
// if arr[1]+arr[0] > arr[2] {
31+
// res = max(res, arr[0]+arr[1]+arr[2])
32+
// }
33+
// }
34+
// }
35+
// }
36+
// return res
37+
// }
38+
39+
// func max(a, b int) int {
40+
// if a > b {
41+
// return a
42+
// } else {
43+
// return b
44+
// }
45+
// }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package largestPerimeter
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type para struct {
10+
one []int
11+
}
12+
13+
type ans struct {
14+
one int
15+
}
16+
17+
type question struct {
18+
p para
19+
a ans
20+
}
21+
22+
func Test_OK(t *testing.T) {
23+
ast := assert.New(t)
24+
25+
qs := []question{
26+
question{
27+
p: para{
28+
one: []int{2, 1, 2},
29+
},
30+
a: ans{
31+
one: 5,
32+
},
33+
},
34+
// question{
35+
// p: para{
36+
// one: []int{1, 2, 1},
37+
// },
38+
// a: ans{
39+
// one: 0,
40+
// },
41+
// },
42+
}
43+
44+
for _, q := range qs {
45+
a, p := q.a, q.p
46+
ast.Equal(a.one, largestPerimeter(p.one), "输入:%v", p)
47+
}
48+
49+
// ast.Panics(func() { longestPalindrome([]int{}, []int{}) }, "对空切片求中位数,却没有panic")
50+
51+
}

0 commit comments

Comments
 (0)