当前位置:网站首页>Leetcode January 10 daily question 306 Additive number
Leetcode January 10 daily question 306 Additive number
2022-06-12 06:21:00 【Well, let me see】
306. Additive number 
Thought analysis :
Enumerate the first and second numbers , The third number is calculated by high-precision addition , And then compare them
AC Code :
// C++
class Solution {
public:
// High precision addition board
string add(string x, string y) {
vector<int> A, B, C;
// First the x,y Reverse deposit vector
for(int i = x.size() - 1; i >= 0; i--) A.push_back(x[i] - '0');
for(int i = y.size() - 1; i >= 0; i--) B.push_back(y[i] - '0');
// Analog addition begins
for(int i = 0, t = 0; i < A.size() || i < B.size() || t; i++) {
if(i < A.size()) t += A[i];
if(i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
string z;
for(int i = C.size() - 1; i >=0; i--) z += to_string(C[i]);
return z;
}
bool isAdditiveNumber(string num) {
for(int i = 0; i < num.size(); i++)
for(int j = i + 1; j + 1 < num.size(); j++) {
// First number a+1 To b
// The second number b+1 To c
// The third number c+1 To ...
// therefore a Initial value of from -1 Start
int a = -1, b = i, c = j;
while(true) {
if((b - a > 1 && num[a + 1] == '0') || (c - b > 1 && num[b + 1] == '0')) break; // A leading 0 illegal
auto x = num.substr(a + 1, b - a), y = num.substr(b + 1, c - b);
auto z = add(x, y);
if(num.substr(c + 1, z.size()) != z) break;
a = b, b = c, c += z.size();
if(c + 1 == num.size()) return true;
}
}
return false;
}
};
边栏推荐
- Storing texture2d to hard disk JPG file with script under unity3d
- Video summary with long short term memory
- Video fire detection based on Gaussian mixture model and multi-color
- Get the size of the picture
- Leetcode-1706. Where does the club fall
- 2D human pose estimation for pose estimation - pifpaf:composite fields for human pose estimation
- UE4 4.27 modify the mobile forward pipeline to support cluster multi light source culling
- Simulateur nightGod ADB View log
- Research Report on truffle fungus industry - market status analysis and development prospect forecast
- Analysis of memory management mechanism of (UE4 4.26) UE4 uobject
猜你喜欢

Automatic modeling of Interchange

Directx11 advanced tutorial tiled based deffered shading

Jetson TX2 machine brushing jetpack4.2 (self test successful version)

Opencv_100问_第五章 (21-25)

Word2Vec

Leetcode-1535. Find the winner of the array game

MNIST handwritten data recognition by RNN

AI作业ch8

Houdini & UE4 programmed generation of mountains and multi vegetation scattering points

Book classification based on Naive Bayes
随机推荐
Cross compile libev
Leetcode 第 80 场双周赛题解
Single channel picture reading
Overview of camera image quality
English grammar_ Adverb_ With or without ly, the meaning is different
Cause analysis of motion blur / smear caused by camera shooting moving objects
Bert Chinese classification model training + reasoning + deployment
Leetcode personal question solution (Sword finger offer3-5) 3 Duplicate number in array, 4 Find in 2D array, 5 Replace spaces
JS预解析
PDF. js FAQs
Bert use
Leetcode-1535. Find the winner of the array game
AI作业ch8
468. verifying the IP address
RNN model
Analysis of memory management mechanism of (UE4 4.26) UE4 uobject
Leetcode-1043. Separate arrays for maximum sum
Highlight detection with pairwise deep ranking for first person video summary (thesis translation)
The unity3d script searches for colliders with overlaps within the specified radius
LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格