★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10246943.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Additive number is a string whose digits can form additive sequence.

A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.

Given a string containing only digits '0'-'9', write a function to determine if it's an additive number.

Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.

Example 1:

Input: "112358"
Output: true
Explanation: The digits can form an additive sequence: 1, 1, 2, 3, 5, 8. 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8

Example 2:

Input: "199100199"
Output: true
Explanation: The additive sequence is: 1, 99, 100, 199. 1 + 99 = 100, 99 + 100 = 199

Follow up:
How would you handle overflow for very large input integers?


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

一个有效的累加序列必须至少包含 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

进阶:
你如何处理一个溢出的过大的整数输入?


16ms

 1 class Solution {
 2     func isAdditiveNumber(_ num: String) -> Bool {
 3         let array = [Character](num)
 4         let n = array.count
 5         if n < 3 { return false }
 6
 7         func helper(s1: String, s2: String, remain: String) -> Bool {
 8             let one = Int(s1)!
 9             let two = Int(s2)!
10             let next = "\(one + two)"
11             if next == remain { return true }
12             var newRemain = remain
13             if remain.hasPrefix(next) {
14                 let range = remain.range(of: next)!
15                 newRemain.replaceSubrange(range, with: "")
16                 return helper(s1: s2, s2: next, remain: newRemain)
17             } else {
18                 return false
19             }
20         }
21
22         //设第一个数是以i结尾的,第二个数是以j结尾的
23         for i in 0 ... (n - 1)/2 - 1 { //i只能取到一半以下的值
24             let one = String(array[0...i])
25             if one != "0" && one.hasPrefix("0") { continue }
26             for j in i + 1 ... n - 2 - i { //j至少取一位,且 n - j >= i
27                 let two = String(array[i+1 ... j])
28                 if two != "0" && two.hasPrefix("0") { continue }
29                 let remain = String(array[j+1 ... n-1])
30                 if remain != "0" && remain.hasPrefix("0") { continue }
31                 if helper(s1: one, s2: two, remain: remain) {
32                     return true
33                 }
34             }
35         }
36         return false
37     }
38 }


24ms

 1 class Solution {
 2     func isAdditiveNumber(_ num: String) -> Bool {
 3
 4         if num.count < 3 {
 5             return false
 6         }
 7
 8         let numArr = Array(num)
 9
10         for i in 1..<numArr.count-1 {
11             for j in i+1..<numArr.count {
12                 let f = String(numArr[0..<i])
13                 let s = String(numArr[i..<j])
14                 var fn = Int(f)!
15                 var sn = Int(s)!
16                 var ln = fn + sn
17
18                 var l = "\(ln)"
19                 var total = f + s + l
20
21                 if (f.count > 1 && f.first == "0" )||(s.count > 1 && s.first == "0") {
22                     continue
23                 }
24                 while total.count < num.count {
25                     let totalArr = Array(total)
26                     if totalArr != Array(numArr[0..<totalArr.count]) {
27                         break
28                     }
29                     fn = sn
30                     sn = ln
31                     ln = fn + sn
32                     l = "\(ln)"
33                     total += l
34                 }
35                 if total == num {
36                     return true
37                 }
38             }
39         }
40         return false
41     }
42 }

转载于:https://www.cnblogs.com/strengthen/p/10246943.html

[Swift]LeetCode306. 累加数 | Additive Number相关推荐

  1. leetcode306. 累加数(回溯)

    累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和. 给定一个只包含数字 '0'-'9' ...

  2. [Swift]LeetCode246.对称数 $ Strobogrammatic Number

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  3. C# LeetCode刷题 - Leetcode 306. 累加数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  4. C#版 - Leetcode 306. 累加数 - 题解

    C#版 - Leetcode 306. 累加数 - 题解 306.Additive Number 在线提交: https://www.gaimor.cn 累加数是一个字符串,组成它的数字可以形成累加序 ...

  5. C语言强数Strong number算法(附完整源码)

    强数Strong number算法 何为Strong number强数 C语言强数Strong number算法完整源码(定义,实现,main函数测试) 何为Strong number强数 强数是一个 ...

  6. C语言实现阿姆斯特朗数armstrong number算法(附完整源码)

    阿姆斯特朗数armstrong number 实现阿姆斯特朗数armstrong number算法的完整源码(定义,实现,main函数测试) 实现阿姆斯特朗数armstrong number算法的完整 ...

  7. LeetCode 306. 累加数(暴力回溯)

    1. 题目 累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和. 给定一个只包含数字 '0 ...

  8. python 查询周数 week number

    python里面查询某一天所处的周数week number时,有比较多的方法, 自己觉得下面这个方法特别棒,在一些方面的处理非常合理. 每年的最后一天必然是12月31日,是12月的最后一天,但这天不一 ...

  9. 卡特兰数 Catalan number

    卡特兰数 Catalan number 卡特兰数前几项为 : 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 74290 ...

  10. LeetCode Additive Number(递归)

    问题:给出一个字符串,问组成它的数字是否可以形成累加序列.序列至少包含32上数,除了最开始的两个数外,字符串中的其他数等于前两个数之和.字符串只包含0-9,同时数字不能以0开头. 思路:先检测前两个数 ...

最新文章

  1. 害怕离职,侧面说明大多数是离职了没人要的废物?
  2. STM32的SPI问题。
  3. 三次握手,四次挥手?
  4. day07 数据类型间的相互转化及字符编码
  5. 汤家凤高等数学基础手写笔记-不定积分
  6. ubuntu 14.04 samba 的 配置
  7. 查看Linux的磁盘使用情况
  8. 捞月狗签约神策数据 数据赋能打造全球玩家生态圈
  9. iPhone5帮助了谁?
  10. This application failed to start because it could not find or load the Qt platform plugin xcb in
  11. linux网络编程(三)select、poll和epoll
  12. 第一课 回归问题与应用
  13. LeetCode 2016. 增量元素之间的最大差值
  14. 准备创建一个自己的校验提示Extender
  15. Linux和windows下多线程的区别
  16. Docker网络基础---Docker跨主机容器访问通信
  17. 高校成绩管理数据库系统
  18. java-opencv 米粒数_Python opencv学习音符的米粒数,返回每个米粒的位置面积和总米粒数的平均面积,pythonopencv,笔记,之数,并,一个,及,个数...
  19. 深入理解Java-GC机制
  20. Kde桌面程序启动器程序图标无法显示

热门文章

  1. Leetcode之合并区间
  2. css之限制文本行数,超出部分显示 “...“
  3. 九、Linux 软件包安装
  4. 自动化运维工具 Ansible ,SaltStack,Salt,Puppet
  5. Apache Commons Compress 文件解压缩库
  6. Android Studio 下载 与 安装 详细步骤
  7. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_汇总
  8. Maven依赖中scope的含义
  9. 阶段3 1.Mybatis_09.Mybatis的多表操作_1 mybatis表之间关系分析
  10. 跨域请求的两种实现方式