leetcode刷题笔记306题 累加数

leetcode刷题笔记306题 累加数

源地址:306. 累加数

问题描述:

累加数是一个字符串,组成它的数字可以形成累加序列。

一个有效的累加序列必须至少包含 3 个数。除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和。

给定一个只包含数字 '0'-'9' 的字符串,编写一个算法来判断给定输入是否是累加数。

说明: 累加序列里的数不会以 0 开头,所以不会出现 1, 2, 03 或者 1, 02, 3 的情况。

示例 1:

输入: "112358"
输出: true
解释: 累加序列为: 1, 1, 2, 3, 5, 8 。1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
示例 2:

输入: "199100199"
输出: true
解释: 累加序列为: 1, 99, 100, 199。1 + 99 = 100, 99 + 100 = 199

import scala.collection.mutable.ListBuffer
import scala.util.control.Breaks._
object Solution {
    def isAdditiveNumber(nums: String): Boolean = {
        def strAdd(x: String, y: String): String = {
            val A = new ListBuffer[Int]
            val B = new ListBuffer[Int]
            val C = new ListBuffer[Int]
            var t = 0
            var cur = 0
            for (i <- (0 to x.length-1).reverse) A.append(x(i) - '0')
            for (i <- (0 to y.length-1).reverse) B.append(y(i) - '0')

            breakable{
            while (true) {
                if (cur < A.length || cur < B.length || t > 0){
                    if (cur < A.length) t += A(cur)
                    if (cur < B.length) t += B(cur)
                    C.append(t%10)
                    t /= 10
                    cur += 1
                } else {
                    break()
                }
             }
            }
            return C.mkString.reverse
        }

            for (i <- 0 to nums.length-1){
                for (j <- i+1 to nums.length-2){
                    var a = -1
                    var b = i
                    var c = j
                    breakable{
                        while (true) {
                            if (b-a > 1 && nums(a+1) == '0' || c-b > 1 && nums(b+1) == '0') break()
                            val x = nums.substring(a+1, b+1)
                            val y = nums.substring(b+1, c+1)
                            val z = strAdd(x, y)
                            //println("------------------------------")
                            //println("a: " + a +", b: "+ b + ", c: " + c)
                            //println("x: " + x +", y: "+ y + ", z: " + z)
                            if (c + z.length + 1 > nums.length) break()
                            if (nums.substring(c+1, c+z.length+1) != z) break()
                            //println("success!")
                            a = b
                            b = c
                            c += z.length
                            //println("a: " + a +", b: "+ b + ", c: " + c)
                            //println("x: " + x +", y: "+ y + ", z: " + z)
                            if (c + 1 == nums.length) return true
                        }   
                    }
                }
            }
        return false
    }
}
import (
    "fmt"
    "strconv"
)
func isAdditiveNumber(num string) bool {
    for i := 0; i < len(num); i++ {
        for j := i+1; j+1 < len(num); j++ {
            a := -1
            b := i
            c := j
            //fmt.Printf("a: %d b: %d c: %d\n", a, b, c)
            for (true) {
                if b-a > 1 && num[a+1] == '0' || c-b > 1 && num[b+1] == '0' {break}
                x := num[a+1:b+1]
                y := num[b+1:c+1]
                z := StrAdd(x, y)
                //fmt.Printf("x: %s y: %s z: %s\n",x, y, z)
                if c + 1 + len(z) > len(num) {break}
                if num[c+1: c+1+len(z)] != z {break}
                a = b
                b = c 
                c += len(z)

                if c + 1 == len(num) {return true}
            }
        }
    }
    return false
}

func StrAdd(str1 string, str2 string) string {
    var A = []int{}
    var B = []int{}
    var C = []int{}

    for i := len(str1)-1; i >= 0; i-- {
        temp, _ := strconv.Atoi(string(str1[i]))
        A = append(A, temp)
    }
    for i := len(str2)-1; i >= 0; i-- {
        temp, _ := strconv.Atoi(string(str2[i]))
        B = append(B, temp)
    }

    for i, t := 0, 0; i < len(A) || i < len(B)|| t > 0; i++ {
        if i < len(A) {t += A[i]}
        if i < len(B) {t += B[i]}
        C = append(C, t%10)
        t /= 10
        //fmt.Printf("i: %d, A[i]: %d, B[i]: %d\n", i, A[i], B[i])
    }

    res := ""
    for i := len(C)-1 ; i >= 0; i-- {
        //fmt.Printf("i: %d, C[i]: %d\n", i, C[i])
        res += strconv.Itoa(C[i])
    }
    //fmt.Printf("res: %s\n", res)  
    return res
}
posted @ 2020-11-29 22:43  ganshuoos  阅读(73)  评论(0编辑  收藏  举报