当前位置:网站首页>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;
}
};
边栏推荐
- Trunet: short videos generation from long videos via story preserving truncation (thesis translation)
- Modifying theme styles in typora
- Automatic modeling of Interchange
- C # converts the hexadecimal code form of text to text (ASCII)
- 哈工大信息内容安全实验
- (UE4 4.27) customize primitivecomponent
- AI作业ch8
- 获取图片的尺寸
- How do I get the date and time from the Internet- How to get DateTime from the internet?
- Overview of camera image quality
猜你喜欢

In unity3d, billboard effect can be realized towards another target

MNIST handwritten data recognition by RNN

SQLite cross compile dynamic library

Bert use

Redis problem (I) -- cache penetration, breakdown, avalanche

Getting started with houdininengine HDA and UE4

Houdini script vex learning

Using hidden Markov model to mark part of speech

Tips for using the potplayer video player

夜神模擬器adb查看log
随机推荐
How to increase heap size of JVM [duplicate] - how to increase heap size of JVM [duplicate]
Computer composition and design work05 ——fifth verson
Image processing: image[:,:,:: -1], image[:,: -1,:], image[:,: -1,:]
Why doesn't the database use binary tree, red black tree, B tree and hash table? Instead, a b+ tree is used
Dlib face detection
Using hidden Markov model to mark part of speech
Word2Vec
Introduction to the method of diligently searching for the alliance procedure
C # converts the hexadecimal code form of text to text (ASCII)
Leetcode-1512. Number of good pairs
Unity3d display FPS script
QT--实现TCP通信
Three years of sharpening a sword: insight into the R & D efficiency of ant financial services
Leetcode sword finger offer II 033 Modified phrase
CONDA create use virtual environment
. Net core and Net framework comparison
哈工大信息内容安全实验
Modifying theme styles in typora
cv2.fillPoly coco annotator segment坐标转化为mask图像
(UE4 4.27) UE4 adds a customized meshpass to realize the edge illumination of the mobile terminal