LeetCode(306) 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.
For example:
“112358” is an additive number because 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
“199100199” is also an additive number, the additive sequence is: 1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199
Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.
Given a string containing only digits ‘0’-‘9’, write a function to determine if it’s an additive number.
分析
给定一个字符串,题目要求判断该字符串是否为约束规则下的可加字符串。
手动判断很容易,但是转为程序实现开始不知从何入手。
查阅一些资料,最终搞定。详见代码。
AC代码
class Solution {
public:
bool isAdditiveNumber(string num) {
if (num.empty())
return false;
int len = num.size();
for (int i = 1; i < len - 1; ++i)
{
string a = num.substr(0, i);
//非首个以0开头的加数违反规则
if (a[0] == '0' && i > 1)
continue;
for (int j = 1; j < len; ++j)
{
string b = num.substr(i, j);
if (b[0] == 0 && j > 1)
continue;
string ret = add(a, b);
if (i + j + ret.length() > len)
continue;
//存储原字符串中和上一和 同样长度的子串
string val = num.substr(i + j, ret.length());
//当前已经相加的末尾下标
int pass = i + j;
string tmp;
while (ret == val)
{
//判断是否到字符串末尾
pass += val.length();
if (len == pass)
return true;
tmp = b;
b = ret;
//下一步骤加法实现
ret = add(tmp, b);
val = num.substr(pass, ret.length());
}//while
}//for
}//for
return false;
}
//字符串加法实现
string add(string a, string b)
{
int len_a = a.size(), len_b = b.size();
string ret = "";
int i = len_a - 1, j = len_b - 1, carry = 0;
while (i>=0 && j>=0)
{
int tmp = a[i] + b[j] - 2 * '0' + carry;
carry = tmp / 10;
char c = tmp % 10 + '0';
ret += c;
--i;
--j;
}//while
while (i >= 0)
{
int tmp = a[i] - '0' + carry;
carry = tmp / 10;
char c = tmp % 10 + '0';
ret += c;
--i;
}//while
while (j >= 0)
{
int tmp = b[j] - '0' + carry;
carry = tmp / 10;
char c = tmp % 10 + '0';
ret += c;
--j;
}//while
if (carry != 0)
ret += carry + '0';
reverse(ret.begin(), ret.end());
return ret;
}
};
LeetCode(306) Additive Number的更多相关文章
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(202) Happy Number
题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- LeetCode(260) Single Number III
题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...
- LeetCode(268) Missing Number
题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...
- LeetCode(136) Single Number
题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...
- LeetCode(9)Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- Leetcode(4)寻找两个有序数组的中位数
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
随机推荐
- Java基本语法和变量
1基本语法 1.1 标识符.关键字 在程序中用于定义名称的都为标识符,如文件名称.类名称.方法名称或变量名称等. 在Java中标识符的定义格式由字母.数字._(下划线),$所组成,不能以数字开头, 不 ...
- java的三大特性之一多态概述
多态---概念 所谓多态就是一个引用在不同情况下的多种状态.多态是指通过指向父亲的指针,来调用在不同的子类中实现的方法. 多态---注意事项 00.java允许父类的引用变量引用它的子类的实例(对象) ...
- MATLAB 添加自定义的模块到simulink库浏览器
在simulink 浏览器窗口File->new->library,打开编辑窗口,将自定义的模块托人编辑窗口.保存为DC_MOTOR_sub_lib.mdl文件. 新建function文件 ...
- 【转】Java Cipher类 DES算法(加密与解密)
Java Cipher类 DES算法(加密与解密) 1.加密解密类 import java.security.*; import javax.crypto.*; import java.io.*; / ...
- 想系统化快速学习人工智能?上Azure School就够了!
要说目前最热门的技术,非人工智能莫属了,让计算机程序能够看懂.听懂.读懂.理解我们的世界!想想就激动!! 上至高大上的个人数字化助理,下至P图软件,各种应用都开始增加AI相关的功能,试问又有哪个技术爱 ...
- 【虚拟机-远程连接】Azure Linux 虚拟机常见导致无法远程的操作
对Azure虚拟机的一些操作可能会导致无法远程连接,本文罗列了以下导致不能远程连接的场景: 场景1 - 在虚拟机配置IP地址或MAC地址 场景2 - 错误地修改服务的配置文件 场景3 - 误设置防火墙 ...
- Python3+Selenium3+webdriver学习笔记5(模拟常用键盘和鼠标事件)
#!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriverfrom selenium.webdriver.co ...
- jsp另外五大内置对象之out输出
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- [论文理解] Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks 简介 Faster R-CNN是很经典的t ...
- 使用ErrorProvider组件验证文本框输入
实现效果: 知识运用: ErrorProvider组件的BlinkStyle属性 //指示错误图标的闪烁时间 public ErrorBlinkStyle BlinkStyle{ get;set; } ...