Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
提交尝试但未通过的题
  • Loading branch information
halfrost committed Aug 7, 2020
commit f918be35eb4e6647c89b41490f8037db1be3abc3
5 changes: 5 additions & 0 deletions leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package leetcode

func maximalRectangle(matrix [][]byte) int {
return 0
}
42 changes: 42 additions & 0 deletions leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package leetcode

import (
"fmt"
"testing"
)

type question85 struct {
para85
ans85
}

// para 是参数
// one 代表第一个参数
type para85 struct {
one [][]byte
}

// ans 是答案
// one 代表第一个答案
type ans85 struct {
one int
}

func Test_Problem85(t *testing.T) {

qs := []question85{

question85{
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'}}},
ans85{6},
},
}

fmt.Printf("------------------------Leetcode Problem 85------------------------\n")

for _, q := range qs {
_, p := q.ans85, q.para85
fmt.Printf("【input】:%v 【output】:%v\n", p, maximalRectangle(p.one))
}
fmt.Printf("\n\n\n")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package leetcode

func minCut(s string) int {
if s == "" {
return 0
}
result := len(s)
current := make([]string, 0, len(s))
dfs132(s, 0, current, &result)
return result
}

func dfs132(s string, idx int, cur []string, result *int) {
start, end := idx, len(s)
if start == end {
*result = min(*result, len(cur)-1)
return
}
for i := start; i < end; i++ {
if isPal(s, start, i) {
dfs132(s, i+1, append(cur, s[start:i+1]), result)
}
}
}

func min(a int, b int) int {
if a > b {
return b
}
return a
}

func isPal(str string, s, e int) bool {
for s < e {
if str[s] != str[e] {
return false
}
s++
e--
}
return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package leetcode

import (
"fmt"
"testing"
)

type question132 struct {
para132
ans132
}

// para 是参数
// one 代表第一个参数
type para132 struct {
s string
}

// ans 是答案
// one 代表第一个答案
type ans132 struct {
one int
}

func Test_Problem132(t *testing.T) {

qs := []question132{

question132{
para132{"aab"},
ans132{1},
},
}

fmt.Printf("------------------------Leetcode Problem 132------------------------\n")

for _, q := range qs {
_, p := q.ans132, q.para132
fmt.Printf("【input】:%v 【output】:%v\n", p, minCut(p.s))
}
fmt.Printf("\n\n\n")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package leetcode

func removeDuplicateLetters(s string) string {
return ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package leetcode

import (
"fmt"
"testing"
)

type question316 struct {
para316
ans316
}

// para 是参数
// one 代表第一个参数
type para316 struct {
one string
}

// ans 是答案
// one 代表第一个答案
type ans316 struct {
one string
}

func Test_Problem316(t *testing.T) {

qs := []question316{

question316{
para316{"bcabc"},
ans316{"abc"},
},
question316{
para316{"cbacdcbc"},
ans316{"acdb"},
},
}

fmt.Printf("------------------------Leetcode Problem 316------------------------\n")

for _, q := range qs {
_, p := q.ans316, q.para316
fmt.Printf("【input】:%v 【output】:%v\n", p, removeDuplicateLetters(p.one))
}
fmt.Printf("\n\n\n")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package leetcode

import (
"github.com/halfrost/LeetCode-Go/structures"
)

// Interval define
type Interval = structures.Interval

// SummaryRanges define
type SummaryRanges struct {
intervals []Interval
}

// Constructor352 define
func Constructor352() SummaryRanges {
return SummaryRanges{}
}

// AddNum define
func (sr *SummaryRanges) AddNum(val int) {
if sr.intervals == nil {
sr.intervals = []Interval{
Interval{
Start: val,
End: val,
},
}
return
}

low, high := 0, len(sr.intervals)-1
for low <= high {
mid := low + (high-low)>>1
if sr.intervals[mid].Start <= val && val <= sr.intervals[mid].End {
return
} else if val < sr.intervals[mid].Start {
high = mid - 1
} else {
low = mid + 1
}
}

if low == 0 {
if sr.intervals[0].Start-1 == val {
sr.intervals[0].Start--
return
}
ni := Interval{Start: val, End: val}
sr.intervals = append(sr.intervals, ni)
copy(sr.intervals[1:], sr.intervals)
sr.intervals[0] = ni
return
}

if low == len(sr.intervals) {
if sr.intervals[low-1].End+1 == val {
sr.intervals[low-1].End++
return
}
sr.intervals = append(sr.intervals, Interval{Start: val, End: val})
return
}

if sr.intervals[low-1].End+1 < val && val < sr.intervals[low].Start-1 {
sr.intervals = append(sr.intervals, Interval{})
copy(sr.intervals[low+1:], sr.intervals[low:])
sr.intervals[low] = Interval{Start: val, End: val}
return
}

if sr.intervals[low-1].End == val-1 && val+1 == sr.intervals[low].Start {
sr.intervals[low-1].End = sr.intervals[low].End
n := len(sr.intervals)
copy(sr.intervals[low:], sr.intervals[low+1:])
sr.intervals = sr.intervals[:n-1]
return
}

if sr.intervals[low-1].End == val-1 {
sr.intervals[low-1].End++
} else {
sr.intervals[low].Start--
}
}

// GetIntervals define
func (sr *SummaryRanges) GetIntervals() [][]int {
intervals := [][]int{}
for _, interval := range sr.intervals {
intervals = append(intervals, []int{interval.Start, interval.End})
}
return intervals
}

/**
* Your SummaryRanges object will be instantiated and called as such:
* obj := Constructor();
* obj.AddNum(val);
* param_2 := obj.GetIntervals();
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package leetcode

import (
"fmt"
"testing"
)

func Test_Problem352(t *testing.T) {
obj := Constructor352()
fmt.Printf("obj = %v\n", obj)
obj.AddNum(1)
fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1,1]
obj.AddNum(3)
fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1,1] [3,3]
obj.AddNum(7)
fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1, 1], [3, 3], [7, 7]
obj.AddNum(2)
fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1, 3], [7, 7]
obj.AddNum(6)
fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1, 3], [6, 7]
}
Loading