ika 7 年之前
父节点
当前提交
85ffed1ebf
共有 3 个文件被更改,包括 188 次插入8 次删除
  1. 113 3
      medium/43.go
  2. 0 1
      medium/79.go
  3. 75 4
      medium/8.go

+ 113 - 3
medium/43.go

@@ -1,12 +1,122 @@
 package main
 package main
 
 
+import (
+	"fmt"
+	"strconv"
+)
+
+// length < 110, non-negative
 func multiply(num1 string, num2 string) string {
 func multiply(num1 string, num2 string) string {
-	i, j := len(num1)-1, len(num2)
-	for ; ; i, j = i-1, j-1 {
+	if num1 == "0" || num2 == "0" {
+		return "0"
+	}
+	if num1 == "1" {
+		return num2
+	} else if num2 == "1" {
+		return num1
+	}
+	n1 := str2IntSlice(num1)
+	n2 := str2IntSlice(num2)
+	sum := []int{}
+	if len(n1) < len(n2) {
+		n1, n2 = n2, n1
+	}
+	for index, num := range n2 {
+		product := intSliceMulInt(n1, num)
+		zeros := make([]int, len(n2)-1-index)
+		product = append(product, zeros...)
+		sum = addIntSlice(sum, product)
+	}
+	return intSlice2Str(sum)
+}
 
 
+func str2IntSlice(str string) []int {
+	res := []int{}
+	for _, char := range str {
+		res = append(res, int(char-'0'))
 	}
 	}
+	return res
 }
 }
 
 
-func main() {
+func intSlice2Str(slice []int) string {
+	str := []rune{}
+	for _, i := range slice {
+		str = append(str, rune(i+'0'))
+	}
+	return string(str)
+}
+
+func addIntSlice(slice1, slice2 []int) []int {
+	if len(slice1) < len(slice2) {
+		slice1, slice2 = slice2, slice1
+	}
+	if len(slice2) == 0 || (len(slice2) == 1 && slice2[0] == 0) {
+		return slice1
+	}
+	res := make([]int, len(slice1)+1)
+	carry := 0
+	for offset := 0; offset < len(slice1); offset++ {
+		var digit, digit1, digit2 int
+		if offset < len(slice2) {
+			digit1 = slice1[len(slice1)-1-offset]
+			digit2 = slice2[len(slice2)-1-offset]
+		} else if offset < len(slice1) {
+			digit1 = slice1[len(slice1)-1-offset]
+			digit2 = 0
+		} else {
+			digit1, digit2 = 0, 0
+		}
+		digit, carry = addByDigit(digit1, digit2, carry)
+		res[len(res)-1-offset] = digit
+	}
+	if carry == 0 {
+		return res[1:]
+	}
+	res[0] = carry
+	return res
+}
+
+func intSliceMulInt(slice []int, num int) []int {
+	if num == 0 || len(slice) == 0 || (len(slice) == 1 && slice[0] == 0) {
+		return []int{0}
+	}
+	if num == 1 {
+		return slice
+	}
+	res := make([]int, len(slice)+1)
+	carry := 0
+	for offset := 0; offset < len(slice); offset++ {
+		digit := slice[len(slice)-1-offset]
+		digit, carry = mulByDigit(digit, num, carry)
+		res[len(res)-1-offset] = digit
+	}
+	if carry == 0 {
+		return res[1:]
+	}
+	res[0] = carry
+	return res
+}
 
 
+func mulByDigit(digit1, digit2, lastCarry int) (digit, carry int) {
+	digit = digit1*digit2 + lastCarry
+	carry = digit / 10
+	return digit % 10, carry
+}
+
+func addByDigit(digit1, digit2, lastCarry int) (digit, carry int) {
+	digit = digit1 + digit2 + lastCarry
+	carry = digit / 10
+	return digit % 10, carry
+}
+
+func testMultiply(num1 string, num2 string) {
+	n1, _ := strconv.Atoi(num1)
+	n2, _ := strconv.Atoi(num2)
+	fmt.Printf("%s * %s = %s | %d\n", num1, num2, multiply(num1, num2), n1*n2)
+	slice := str2IntSlice("12347876")
+	intSlice2Str(slice)
+}
+
+func main() {
+	testMultiply("782", "3908982")
 }
 }

+ 0 - 1
medium/79.go

@@ -3,7 +3,6 @@ package main
 import "fmt"
 import "fmt"
 
 
 func exist(board [][]byte, word string) bool {
 func exist(board [][]byte, word string) bool {
-
 	return false
 	return false
 }
 }
 
 

+ 75 - 4
medium/8.go

@@ -2,15 +2,86 @@ package main
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"math"
 	"strings"
 	"strings"
 )
 )
 
 
 func myAtoi(str string) int {
 func myAtoi(str string) int {
-	str = strings.TrimSpace(str)
-	fmt.Println(str)
-	return 0
+	str = strings.Trim(str, " ")
+	if len(str) == 0 {
+		return 0
+	}
+	chars := []rune(str)
+	beg, end := 0, 0
+	isNegative := false
+	// judge the sign of the integer
+	switch chars[beg] {
+	case '-':
+		isNegative = true
+		fallthrough
+	case '+':
+		beg, end = 1, 1
+		// deal with "+" or "-"
+		if len(str) == 1 {
+			return 0
+		}
+	default:
+		beg, end = 0, 0
+	}
+	// find the first non-zero digit and the last valid digit
+	for ; end <= len(chars); end++ {
+		if chars[beg] == '0' {
+			beg++
+			// for str like "000000000000", return 0
+			if beg == len(chars) {
+				return 0
+			}
+			continue
+		}
+		if end == len(chars) || chars[end] < '0' || '9' < chars[end] {
+			break
+		}
+	}
+	if beg == end {
+		// no valid digit
+		return 0
+	} else if end-beg > 10 { // overflow (MaxInt32 & MinInt32 have 10 digits only)
+		if isNegative {
+			return math.MinInt32
+		}
+		return math.MaxInt32
+	}
+	sum, base := int64(0), int64(1)
+	for ; end > beg; end-- {
+		num := int64(chars[end-1] - '0')
+		sum += num * base
+		base *= int64(10)
+	}
+	if isNegative {
+		sum *= int64(-1)
+	}
+	if sum < math.MinInt32 {
+		return math.MinInt32
+	} else if sum > math.MaxInt32 {
+		return math.MaxInt32
+	}
+	return int(sum)
+}
+
+func testMyAtoi(str string) {
+	fmt.Printf("\"%s\" -> %d\n", str, myAtoi(str))
 }
 }
 
 
 func main() {
 func main() {
-	myAtoi("   -0.1    ")
+	testMyAtoi("")
+	testMyAtoi("       00000000000000000000           ")
+	testMyAtoi("+")
+	testMyAtoi("   -0.1    ")
+	testMyAtoi("   +1.1    ")
+	testMyAtoi("   234.1    ")
+	testMyAtoi("42")
+	testMyAtoi("9223372036854775808")
+	testMyAtoi("   -2.1    ")
+	testMyAtoi("   - 0.1    ")
+	testMyAtoi("   num 0.1    ")
 }
 }