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

进阶:

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

class Solution {
public boolean isAdditiveNumber(String num) {
if (num.length() < 3) return false;
int n = num.length();
for (int i = 1; i <= (n - 1) / 2; i++) {
if (i > 1 && num.charAt(0) == '0') break;
for (int j = i + 1; (n - j) >= i && (n - j) >= j - i; j++) {
if (j - i > 1 && num.charAt(i) == '0') break;
long num1 = Long.parseLong(num.substring(0, i));
long num2 = Long.parseLong(num.substring(i, j));
if (isAdditive(num.substring(j), num1, num2))
return true;
}
}
return false;
} private boolean isAdditive(String remain, long num1, long num2) {
if (remain.equals("")) return true;
long sum = num1 + num2;
String str = String.valueOf(sum);
if (!remain.startsWith(str)) return false;
return isAdditive(remain.substring(str.length()), num2, sum);
}
}

Java实现 LeetCode 306 累加数的更多相关文章

  1. Leetcode 306.累加数

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

  2. LeetCode:累加数【306】

    LeetCode:累加数[306] 题目描述 累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相 ...

  3. [Leetcode] 第306题 累加数

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

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

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

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. 一阶RC高通滤波器详解(仿真+matlab+C语言实现)

    文章目录 预备知识 关于电容 HPF的推导 simulink 仿真 simulink 运行结果 matlab 实现 matlab 运行结果 C语言实现 如果本文帮到了你,帮忙点个赞: 如果本文帮到了你 ...

  2. u-boot: Not enough room for program headers, try linking with -N

    在编译u-boot的时候出现了以下错误: arm-linux-gnueabi-ld.bfd: u-boot: Not enough room for program headers, try link ...

  3. layui批量传值到后台操作时出现传值为空的问题

    如图,前台的样子,data的参数为 [ {"good_id":1,"good_name":"标样-总磷","good_num&qu ...

  4. Android fragment 使用replace并保存状态

    Fragment的地位在开发中可是举足轻重的,掌握它的的生命周期以及使用特性是非常重要的,例如在开发中常使用的模板: 点击菜单,中心内容跟随菜单变化,但是在菜单间切换时,需要保存之前输入的信息或其他状 ...

  5. 《学习笔记》Layui-WPF窗体美化

    一睹为快: 1.创建自定义控件,并取名为LayuiWPFStyle 2.在当前目录中创建Fonts和WindowStyle文件加用来存放字体文件和自定义窗体,字体用fontawesome字体当然你们可 ...

  6. 【图机器学习】cs224w Lecture 10 - PageRank

    目录 PageRank Problems Personalized PageRank 转自本人:https://blog.csdn.net/New2World/article/details/1062 ...

  7. Python创建一个简单的区块链

    区块链(Blockchain)是一种分布式账本(listributed ledger),它是一种仅供增加(append-only),内容不可变(immutable)的有序(ordered)链式数据结构 ...

  8. 浙工大新生赛莫队处理+区间DP+KMP+分析题

    题目描述 读入一个长度为n的整数数列a1,a2,…,an,以及一个整数K. q组询问. 每组询问包含一个二元组(l, r), 其中1≤l≤r≤ n, 求所有满足以下条件的二元组(l2, r2)的数目: ...

  9. 容器技术之Docker网络

    上一篇博客我们主要聊了下docker镜像相关的说明以及怎样基于现有镜像制作镜像.分发镜像到docker仓库中的相关测试:回顾请参考https://www.cnblogs.com/qiuhom-1874 ...

  10. 【Java8新特性】Stream API有哪些中间操作?看完你也可以吊打面试官!!

    写在前面 在上一篇<[Java8新特性]面试官问我:Java8中创建Stream流有哪几种方式?>中,一名读者去面试被面试官暴虐!归根结底,那哥儿们还是对Java8的新特性不是很了解呀!那 ...