day 5: trying to rizz up gophy mol again
This commit is contained in:
parent
9bd8221089
commit
07b8dc6b3e
|
|
@ -61,3 +61,6 @@ func intToRomanOne(num int) string {
|
||||||
// then a single allocation is made of the exact size needed for the result.
|
// then a single allocation is made of the exact size needed for the result.
|
||||||
return r3[num%1e4/1e3] + r2[num%1e3/1e2] + r1[num%100/10] + r0[num%10]
|
return r3[num%1e4/1e3] + r2[num%1e3/1e2] + r1[num%100/10] + r0[num%10]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
package gopherriz
|
||||||
|
|
||||||
|
func longestPalindromeAlternative(s string) string {
|
||||||
|
if len(s) < 2 {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
start, maxLength := 0, 1
|
||||||
|
expand := func(l, i int) {
|
||||||
|
for l >= 0 && i < len(s) && s[l] == s[i] {
|
||||||
|
if i-l+1 > maxLength {
|
||||||
|
start = l
|
||||||
|
maxLength = i - l + 1
|
||||||
|
}
|
||||||
|
l = l - 1
|
||||||
|
i = i + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, _ := range s {
|
||||||
|
expand(i, i)
|
||||||
|
expand(i, i+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s[start : start+maxLength]
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package gopherriz
|
||||||
|
|
||||||
|
func longestPalindrome(s string) string {
|
||||||
|
transformed := "#"
|
||||||
|
for _, letter := range s {
|
||||||
|
transformed += string(letter) + "#"
|
||||||
|
}
|
||||||
|
|
||||||
|
n := len(transformed)
|
||||||
|
p := make([]int, n)
|
||||||
|
c, r := 0, 0
|
||||||
|
mirror := 0
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
mirror = c*2 - i
|
||||||
|
if i < r {
|
||||||
|
p[i] = min(r-i, p[mirror])
|
||||||
|
}
|
||||||
|
|
||||||
|
for i+p[i]+1 < n && i-p[i]-1 >= 0 && transformed[i+p[i]+1] == transformed[i-p[i]-1] {
|
||||||
|
p[i] += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if i+p[i] > r {
|
||||||
|
c = i
|
||||||
|
r = c + p[c]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
maxLength, centre := 0, 0
|
||||||
|
for i, v := range p {
|
||||||
|
if v > maxLength {
|
||||||
|
maxLength = v
|
||||||
|
centre = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
start := (centre - maxLength) / 2
|
||||||
|
return s[start : start+maxLength]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(a int, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue