Skip to content

Commit

Permalink
add: sword offer
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw99 committed Oct 5, 2020
1 parent 59f4896 commit 0496f31
Show file tree
Hide file tree
Showing 10 changed files with 539 additions and 0 deletions.
52 changes: 52 additions & 0 deletions medium/131_partition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import "fmt"

func main() {
s := "aab"
fmt.Println(partition(s))
}

func partition(s string) [][]string {
var res [][]string
if len(s) == 0 {
return res
}

var backtrack func(s string, path []string, pos int)
backtrack = func(s string, path []string, pos int) {
if pos == len(s) {
tmp := make([]string, len(path))
copy(tmp, path)
res = append(res, tmp)
}

for i := pos; i < len(s); i++ {
if isPalindrome(s[pos : i+1]) {
path = append(path, s[pos:i+1])
backtrack(s, path, i+1)
path = path[:len(path)-1]
}
}
}

backtrack(s, []string{}, 0)

return res
}

func isPalindrome(s string) bool {
if len(s) == 0 || len(s) == 1 {
return true
}

l := len(s)
for i := 0; i < l; i++ {
if s[i] != s[l-1] {
return false
}
l--
}

return true
}
29 changes: 29 additions & 0 deletions medium/1634_buildTree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import "fmt"

func main() {
preOrder := []int{3, 9, 20, 15, 7}
inOrder := []int{9, 3, 15, 20, 7}
fmt.Println(buildTree(preOrder, inOrder))
}

func buildTree(preorder []int, inorder []int) *TreeNode {
for i, _ := range inorder {
if inorder[i] == preorder[0] {
return &TreeNode{
Val: preorder[0],
Left: buildTree(preorder[1:i+1], inorder[:i]),
Right: buildTree(preorder[i+1:], inorder[i+1:]),
}
}
}

return nil
}

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
33 changes: 33 additions & 0 deletions medium/1651_cuttingRope2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import "fmt"

func main() {
n := 120
fmt.Println(cuttingRope2(n))
}

func cuttingRope2(n int) int {
var res = 1

if n <= 3 {
return n - 1
}

if n%3 == 1 {
res *= 4
n -= 4
}

if n%3 == 2 {
res *= 2
n -= 2
}

for i := 0; i < n/3; i++ {
res %= 1e9 + 7
res *= 3
}

return res % (1e9 + 7)
}
130 changes: 130 additions & 0 deletions medium/1653_myPow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package main

import (
"fmt"
"math"
)

func main() {
x := -13.62608
n := 7
fmt.Println(myPow(x, n))
fmt.Println(myPowIteration(x, n))
fmt.Println(myPowSelf(x, n))
}

// base = x, exponent = n
func myPow(x float64, n int) float64 {
if n == 0 {
return 1
}
if n == 1 {
return x
}

if n < 0 {
x = 1 / x
n = -n
}

res := 1.0
for n >= 1 {
if n&1 == 1 {
res *= x
n--
} else {
res *= x * x
n = n >> 1
}
}

return res
}

// base = x, exponent = n
func myPowIteration(x float64, n int) float64 {
if n == 0 {
return 1
}
if n == 1 {
return x
}
if n < 0 {
x = 1 / x
n = -n
}

temp := myPowIteration(x, n/2)
if n%2 == 0 {
return temp * temp
}

return temp * temp * x
}

// base = x, exponent = n
func myPowSelf(x float64, n int) float64 {
var res float64 = 1
if x == 1 || n == 0 {
return res
}
if x == 0 {
return 0
}

// 判断 x 符号
negativeX := false
if x < 0 {
x = -x
negativeX = true
}

// 判断 n 符号
negativeN := false
if n < 0 {
n = -n
negativeN = true
}

if n >= math.MaxInt32 {
if 0 < x && x < 1 {
return 0
}
if x == 1 && negativeX {
if n%2 == 0 {
return res
}
return -res
}
if x > 1 {
if negativeN {
return 0
}

// x 为负数
if negativeX {
if n%2 == 0 {
return math.MaxFloat64
}
return 0
}
return math.MaxFloat64
}
}

for i := 0; i < n; i++ {
res *= x
}

if negativeN {
res = 1 / res
}

if negativeX {
if n%2 != 0 {
res = -res
}
}

return res
}
21 changes: 21 additions & 0 deletions medium/1657_isNumber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"regexp"
"strings"
)

func main() {
s := "3.e5"
fmt.Println(isNumber(s))
}

func isNumber(s string) bool {
var res bool
s = strings.Trim(s, " ")

res, _ = regexp.MatchString("^(([+-]?[0-9]+(\\.[0-9]*)?)|([+-]?\\.?[0-9]+))([eE][+-]?[0-9]+)?$", s)

return res
}
54 changes: 54 additions & 0 deletions medium/1682_findNthDigit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"strconv"
)

func main() {
n := 19
fmt.Println(findNthDigit(n))
}

func findNthDigit(n int) int {
// 写法一
var start = 1
var digit = 1
var flag = 9

for n-flag*digit > 0 {
n -= flag * digit
flag *= 10
start *= 10
digit++
}

num := start + (n-1)/digit
return int(strconv.Itoa(num)[(n-1)%digit] - '0')

// 写法二
/*digits := 1
flag := 9
// 求n所在的数为几位数
for n-flag*digits > 0 {
n = n - flag*digits
flag = flag * 10
digits++
}
// 若n为个位数,返回n即可
if digits == 1 {
return n
}
number := 1
for k := 1; k < digits; k++ {
number = number * 10
}
// 求n对应的数
number = number + (n-1)/digits
idx := (n - 1) % digits
// 将n对应的数转换为字符串类型,取第idx位并转换为整数
strnums := strconv.Itoa(number)
res, _ := strconv.Atoi(string(strnums[idx]))
return res*/
}
Loading

0 comments on commit 0496f31

Please sign in to comment.