-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp.go
169 lines (152 loc) · 3.36 KB
/
regexp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"log"
"regexp"
"strings"
)
func main() {
log.Println(isNumber("1e3"))
}
func isMatch(s string, p string) bool {
si := 0
pi := 0
spec1 := "*"
spec2 := "."
log.Println(s, p)
if len(s) == 0 && len(p) == 0 {
return true
}
// todo edge p=""
if pi < len(p)-1 && p[pi+1] == spec1[0] {
if isMatch(s[si:len(s)], p[pi+2:len(p)]) {
return true
}
for si < len(s) && (s[si] == p[pi] || p[pi] == spec2[0]) {
si++
if isMatch(s[si:len(s)], p[pi+2:len(p)]) {
return true
}
}
} else if len(s) == 0 && len(p) > 0 || len(s) > 0 && len(p) == 0 {
return false
} else if s[si] == p[pi] || p[pi] == spec2[0] {
si++
pi++
log.Println(s, p, si, pi)
return isMatch(s[si:len(s)], p[pi:len(p)])
} else {
return false
}
if si == len(s) && pi == len(p) {
return true
}
log.Println("finish", si, len(s), pi, len(p))
return false
}
// 32. Longest Valid Parentheses
// Given a string containing just the characters '(' and ')', find the length
// of the longest valid (well-formed) parentheses substring.
type Parenthesis struct {
ch byte
i int
}
func longestValidParentheses(s string) int {
lp := "("
rp := ")"
i := 0
max := 0
stack := []Parenthesis{}
startIndex := 0
for i < len(s) {
if s[i] == lp[0] {
stack = append(stack, Parenthesis{ch: lp[0], i: i})
log.Println("lp", stack)
} else if s[i] == rp[0] {
if len(stack) > 0 && stack[len(stack)-1].ch == lp[0] {
stack = stack[0 : len(stack)-1]
if len(stack) > 0 {
startIndex = stack[len(stack)-1].i
} else {
startIndex = -1
}
log.Println(i, startIndex, stack)
if max < i-startIndex {
max = i - startIndex
}
log.Println(i, startIndex, stack, max)
} else {
stack = append(stack, Parenthesis{ch: rp[0], i: i})
log.Println("rp", stack)
}
} else {
panic("incorrect symbol")
}
i++
}
return max
<<<<<<< HEAD
}
=======
}
// 149. Max Points on a Line
// Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
func maxPoints(points [][]int) int {
i := 0
max := 0
if len(points) == 1 {
return 1
}
for i < len(points)-1 {
duplicates := 0
j := i + 1
for j < len(points) {
if points[i][0] == points[j][0] && points[i][1] == points[j][1] {
duplicates++
if max < duplicates+1 {
max = duplicates + 1
}
} else {
s := slope(points[i], points[j])
log.Println(i, j, s)
l := j + 1
counter := 2 + duplicates
for l < len(points) {
if slope(points[l], points[j]) == s || (points[l][0] == points[j][0] && points[l][1] == points[j][1]) {
counter++
}
log.Println("l", slope(points[l], points[j]), s, l, counter)
l++
}
if max < counter {
max = counter
}
}
j++
}
i++
}
return max
}
func slope(p1 []int, p2 []int) float64 {
slope := float64(p1[1]-p2[1]) / float64(p1[0]-p2[0])
if p1[0]-p2[0] == 0 {
slope = 0
}
return slope
}
func isNumber(s string) bool {
s = strings.TrimSpace(s)
if len(s) == 0 {
return false
}
r := regexp.MustCompile(`(^[^0-9]+$`)
if !r.MatchString(s) {
return false
}
vs := strings.Split(s, "e")
if len(vs) > 2 {
return false
}
return true
}
>>>>>>> 4747750d398f9329d4261bd32b59682072233186