Skip to content

Commit f918be3

Browse files
committed
提交尝试但未通过的题
1 parent 4e11f40 commit f918be3

18 files changed

+900
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package leetcode
2+
3+
func maximalRectangle(matrix [][]byte) int {
4+
return 0
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question85 struct {
9+
para85
10+
ans85
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para85 struct {
16+
one [][]byte
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans85 struct {
22+
one int
23+
}
24+
25+
func Test_Problem85(t *testing.T) {
26+
27+
qs := []question85{
28+
29+
question85{
30+
para85{[][]byte{[]byte{'1', '0', '1', '0', '0'}, []byte{'1', '0', '1', '1', '1'}, []byte{'1', '1', '1', '1', '1'}, []byte{'1', '0', '0', '1', '0'}}},
31+
ans85{6},
32+
},
33+
}
34+
35+
fmt.Printf("------------------------Leetcode Problem 85------------------------\n")
36+
37+
for _, q := range qs {
38+
_, p := q.ans85, q.para85
39+
fmt.Printf("【input】:%v 【output】:%v\n", p, maximalRectangle(p.one))
40+
}
41+
fmt.Printf("\n\n\n")
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
func minCut(s string) int {
4+
if s == "" {
5+
return 0
6+
}
7+
result := len(s)
8+
current := make([]string, 0, len(s))
9+
dfs132(s, 0, current, &result)
10+
return result
11+
}
12+
13+
func dfs132(s string, idx int, cur []string, result *int) {
14+
start, end := idx, len(s)
15+
if start == end {
16+
*result = min(*result, len(cur)-1)
17+
return
18+
}
19+
for i := start; i < end; i++ {
20+
if isPal(s, start, i) {
21+
dfs132(s, i+1, append(cur, s[start:i+1]), result)
22+
}
23+
}
24+
}
25+
26+
func min(a int, b int) int {
27+
if a > b {
28+
return b
29+
}
30+
return a
31+
}
32+
33+
func isPal(str string, s, e int) bool {
34+
for s < e {
35+
if str[s] != str[e] {
36+
return false
37+
}
38+
s++
39+
e--
40+
}
41+
return true
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question132 struct {
9+
para132
10+
ans132
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para132 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans132 struct {
22+
one int
23+
}
24+
25+
func Test_Problem132(t *testing.T) {
26+
27+
qs := []question132{
28+
29+
question132{
30+
para132{"aab"},
31+
ans132{1},
32+
},
33+
}
34+
35+
fmt.Printf("------------------------Leetcode Problem 132------------------------\n")
36+
37+
for _, q := range qs {
38+
_, p := q.ans132, q.para132
39+
fmt.Printf("【input】:%v 【output】:%v\n", p, minCut(p.s))
40+
}
41+
fmt.Printf("\n\n\n")
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package leetcode
2+
3+
func removeDuplicateLetters(s string) string {
4+
return ""
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question316 struct {
9+
para316
10+
ans316
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para316 struct {
16+
one string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans316 struct {
22+
one string
23+
}
24+
25+
func Test_Problem316(t *testing.T) {
26+
27+
qs := []question316{
28+
29+
question316{
30+
para316{"bcabc"},
31+
ans316{"abc"},
32+
},
33+
question316{
34+
para316{"cbacdcbc"},
35+
ans316{"acdb"},
36+
},
37+
}
38+
39+
fmt.Printf("------------------------Leetcode Problem 316------------------------\n")
40+
41+
for _, q := range qs {
42+
_, p := q.ans316, q.para316
43+
fmt.Printf("【input】:%v 【output】:%v\n", p, removeDuplicateLetters(p.one))
44+
}
45+
fmt.Printf("\n\n\n")
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package leetcode
2+
3+
import (
4+
"github.com/halfrost/LeetCode-Go/structures"
5+
)
6+
7+
// Interval define
8+
type Interval = structures.Interval
9+
10+
// SummaryRanges define
11+
type SummaryRanges struct {
12+
intervals []Interval
13+
}
14+
15+
// Constructor352 define
16+
func Constructor352() SummaryRanges {
17+
return SummaryRanges{}
18+
}
19+
20+
// AddNum define
21+
func (sr *SummaryRanges) AddNum(val int) {
22+
if sr.intervals == nil {
23+
sr.intervals = []Interval{
24+
Interval{
25+
Start: val,
26+
End: val,
27+
},
28+
}
29+
return
30+
}
31+
32+
low, high := 0, len(sr.intervals)-1
33+
for low <= high {
34+
mid := low + (high-low)>>1
35+
if sr.intervals[mid].Start <= val && val <= sr.intervals[mid].End {
36+
return
37+
} else if val < sr.intervals[mid].Start {
38+
high = mid - 1
39+
} else {
40+
low = mid + 1
41+
}
42+
}
43+
44+
if low == 0 {
45+
if sr.intervals[0].Start-1 == val {
46+
sr.intervals[0].Start--
47+
return
48+
}
49+
ni := Interval{Start: val, End: val}
50+
sr.intervals = append(sr.intervals, ni)
51+
copy(sr.intervals[1:], sr.intervals)
52+
sr.intervals[0] = ni
53+
return
54+
}
55+
56+
if low == len(sr.intervals) {
57+
if sr.intervals[low-1].End+1 == val {
58+
sr.intervals[low-1].End++
59+
return
60+
}
61+
sr.intervals = append(sr.intervals, Interval{Start: val, End: val})
62+
return
63+
}
64+
65+
if sr.intervals[low-1].End+1 < val && val < sr.intervals[low].Start-1 {
66+
sr.intervals = append(sr.intervals, Interval{})
67+
copy(sr.intervals[low+1:], sr.intervals[low:])
68+
sr.intervals[low] = Interval{Start: val, End: val}
69+
return
70+
}
71+
72+
if sr.intervals[low-1].End == val-1 && val+1 == sr.intervals[low].Start {
73+
sr.intervals[low-1].End = sr.intervals[low].End
74+
n := len(sr.intervals)
75+
copy(sr.intervals[low:], sr.intervals[low+1:])
76+
sr.intervals = sr.intervals[:n-1]
77+
return
78+
}
79+
80+
if sr.intervals[low-1].End == val-1 {
81+
sr.intervals[low-1].End++
82+
} else {
83+
sr.intervals[low].Start--
84+
}
85+
}
86+
87+
// GetIntervals define
88+
func (sr *SummaryRanges) GetIntervals() [][]int {
89+
intervals := [][]int{}
90+
for _, interval := range sr.intervals {
91+
intervals = append(intervals, []int{interval.Start, interval.End})
92+
}
93+
return intervals
94+
}
95+
96+
/**
97+
* Your SummaryRanges object will be instantiated and called as such:
98+
* obj := Constructor();
99+
* obj.AddNum(val);
100+
* param_2 := obj.GetIntervals();
101+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func Test_Problem352(t *testing.T) {
9+
obj := Constructor352()
10+
fmt.Printf("obj = %v\n", obj)
11+
obj.AddNum(1)
12+
fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1,1]
13+
obj.AddNum(3)
14+
fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1,1] [3,3]
15+
obj.AddNum(7)
16+
fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1, 1], [3, 3], [7, 7]
17+
obj.AddNum(2)
18+
fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1, 3], [7, 7]
19+
obj.AddNum(6)
20+
fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1, 3], [6, 7]
21+
}

0 commit comments

Comments
 (0)