LeetCode 306. Additive Number
原题链接在这里:https://leetcode.com/problems/additive-number/
题目:
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
Constraints:
num
consists only of digits'0'-'9'
.1 <= num.length <= 35
题解:
Get the first two numbers. And check if their sum could be got from the rest.
DFS state needs first number, second number and rest string.
If first 2 numbers are legal, calculate the sum.
If sum is equal to rest, return true.
Otherwise, if sum could be get from rest string. Continue DFS with 2nd, sum and new rest.
When getting the first 2 numbers, neither of them length could be larger than half of num length.
Time Complexity: exponential.
Space: O(n). n =sum.length(). stack space.
AC Java:
1 class Solution { 2 public boolean isAdditiveNumber(String num) { 3 if(num == null || num.length() < 3){ 4 return false; 5 } 6 7 for(int i = 1; i<=num.length()/2; i++){ 8 for(int j = 1; j<=num.length()/2; j++){ 9 if(dfs(num.substring(0, i), num.substring(i, i+j), num.substring(i+j))){ 10 return true; 11 } 12 } 13 } 14 15 return false; 16 } 17 18 private boolean dfs(String a, String b, String c){ 19 if((a.length() > 1 && a.charAt(0) == '0') || (b.length() > 1 && b.charAt(0) == '0')){ 20 return false; 21 } 22 23 String sum = getSum(a, b); 24 if(sum.equals(c)){ 25 return true; 26 } 27 28 if(sum.length() >= c.length() || !c.substring(0, sum.length()).equals(sum)){ 29 return false; 30 } 31 32 return dfs(b, c.substring(0, sum.length()), c.substring(sum.length())); 33 } 34 35 private String getSum(String a, String b){ 36 StringBuilder sb = new StringBuilder(); 37 int i = a.length()-1; 38 int j = b.length()-1; 39 int carry = 0; 40 while(i>=0 || j>=0 || carry>0){ 41 int temp = (i>=0 ? a.charAt(i)-'0' : 0) + (j>=0 ? b.charAt(j)-'0' : 0) + carry; 42 sb.insert(0, temp%10); 43 carry = temp/10; 44 i--; 45 j--; 46 } 47 48 return sb.toString(); 49 } 50 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· Supergateway:MCP服务器的远程调试与集成工具
· C# 13 中的新增功能实操
2015-11-12 LeetCode 126. Word Ladder II