problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::additive_number::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static bool dfs (unsigned long long n1, unsigned long long n2, const char *, unsigned short length, unsigned short current)
 
static bool equal (string, const char *, unsigned short start, unsigned short length)
 判断一个字符串与另一个字符串的子串是否相等 更多...
 
static bool isAdditiveNumber (string num)
 
static unsigned long long str2ui (const char *, unsigned short start, unsigned short length)
 将字符串的一个子串转换为整数 更多...
 

详细描述

在文件 leetcode.h320 行定义.

成员函数说明

◆ dfs()

bool leetcode::additive_number::Solution::dfs ( unsigned long long  n1,
unsigned long long  n2,
const char *  nums,
unsigned short  length,
unsigned short  current 
)
static
参数
n1第一个数字
n2第二个数字
length总长度
current现在的位置
返回

在文件 leetcode.cpp769 行定义.

769 {
770 const auto sum = to_string(n1 + n2);
771 if(sum.length() > length - current) {
772 //前两位和的位数超过剩余的位数
773 return false;
774 }
775 if(!equal(sum, nums, current, length)) {
776 //不包含下一个数字
777 return false;
778 }
779 if(current + sum.length() == length) {
780 //终止条件
781 return true;
782 }
783 const auto n3 = str2ui(nums, current, sum.length());
784 return dfs(n2, n3, nums, length, current + sum.length());
785 }
static bool dfs(unsigned long long n1, unsigned long long n2, const char *, unsigned short length, unsigned short current)
Definition: leetcode.cpp:769
static unsigned long long str2ui(const char *, unsigned short start, unsigned short length)
将字符串的一个子串转换为整数
Definition: leetcode.cpp:787
static bool equal(string, const char *, unsigned short start, unsigned short length)
判断一个字符串与另一个字符串的子串是否相等
Definition: leetcode.cpp:796

引用了 dfs(), equal() , 以及 str2ui().

被这些函数引用 dfs(), isAdditiveNumber() , 以及 leetcode::additive_number::TEST().

◆ equal()

bool leetcode::additive_number::Solution::equal ( string  sum,
const char *  nums,
unsigned short  start,
unsigned short  length 
)
static

判断一个字符串与另一个字符串的子串是否相等

参数
start另一个字符串的子串起始位置
length另一个字符串的总长度
返回
一个字符串与另一个字符串的子串是否相等

在文件 leetcode.cpp796 行定义.

796 {
797 int j = 0;
798 for(int i = start; j < sum.length() && i < length; i++, j++) {
799 if(sum[j] != nums[i]) {
800 return false;
801 }
802 }
803 return j == sum.length();
804 }

被这些函数引用 dfs() , 以及 leetcode::additive_number::TEST().

◆ isAdditiveNumber()

bool leetcode::additive_number::Solution::isAdditiveNumber ( string  num)
static

在文件 leetcode.cpp742 行定义.

742 {
743 if(num.length() < 3) {
744 return false;
745 }
746 auto *const nums = new char[num.length()];
747 for(int i = 0; i < num.length(); i++) {
748 nums[i] = num[i];
749 }
750 for(int i = 1; i <= num.length() - i; i++) {
751 const auto n1 = str2ui(nums, 0, i);
752 for(int j = i + 1; j - i <= num.length() - j; j++) {
753 const auto n2 = str2ui(nums, i, j - i);
754 if(dfs(n1, n2, nums, num.length(), j)) {
755 return true;
756 }
757 if(n2 == 0) {
758 break;
759 }
760 }
761 if(n1 == 0) {
762 break;
763 }
764 }
765 delete[] nums;
766 return false;
767 }

引用了 dfs() , 以及 str2ui().

被这些函数引用 leetcode::additive_number::TEST().

◆ str2ui()

unsigned long long leetcode::additive_number::Solution::str2ui ( const char *  str,
unsigned short  start,
unsigned short  length 
)
static

将字符串的一个子串转换为整数

参数
start起始位置
length转换长度
返回
从字符串转换来的整数

在文件 leetcode.cpp787 行定义.

787 {
788 unsigned long long ans = 0;
789 for(int i = start; i < start + length; i++) {
790 ans *= 10;
791 ans += str[i] - '0';
792 }
793 return ans;
794 }

被这些函数引用 dfs(), isAdditiveNumber() , 以及 leetcode::additive_number::TEST().


该类的文档由以下文件生成: