Add to Array-Form of Integer Solutions in C++
Number 989
Difficulty Easy
Acceptance 44.3%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/add-to-array-form-of-integer/// Author : Hao Chen// Date : 2019-03-25class Solution {public:vector<int> addToArrayForm(vector<int>& A, int K) {int carry = K;for (int i = A.size() - 1; i >= 0 && carry > 0; i--) {int a = A[i] + carry;A[i] = a % 10 ;carry = a / 10;}while ( carry > 0 ) {A.insert(A.begin(), carry%10);carry /= 10;}return A;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/add-to-array-form-of-integer//// Author : liuyubobobo/// Time : 2019-02-09#include <iostream>#include <vector>using namespace std;/// Two array's addition/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:vector<int> addToArrayForm(vector<int>& A, int K) {reverse(A.begin(), A.end());// Solution::print_vec(A);vector<int> B;while(K)B.push_back(K % 10), K /= 10;if(B.empty()) B.push_back(0);// Solution::print_vec(B);while(A.size() < B.size()) A.push_back(0);while(B.size() < A.size()) B.push_back(0);// Solution::print_vec(A);// Solution::print_vec(B);int carry = 0;for(int i = 0; i < A.size(); i ++){int t = A[i] + B[i] + carry;A[i] = t % 10;carry = t / 10;}if(carry) A.push_back(1);// Solution::print_vec(A);while(!A.empty() && A.back() == 0) A.pop_back();if(A.empty()) A.push_back(0);reverse(A.begin(), A.end());// Solution::print_vec(A);return A;}static void print_vec(const vector<int>& vec){for(int e: vec) cout << e;cout << endl;}};int main() {vector<int> A1 = {0};int K1 = 0;Solution::print_vec(Solution().addToArrayForm(A1, K1));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/add-to-array-form-of-integer//// Author : liuyubobobo/// Time : 2019-02-13#include <iostream>#include <vector>using namespace std;/// Array and number addition/// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:vector<int> addToArrayForm(vector<int>& A, int K) {vector<int> res;int carry = 0, i = A.size() - 1;while(i >= 0 || K){int t = K % 10 + carry;K /= 10;if(i >= 0) t += A[i];res.push_back(t % 10);carry = t / 10;i --;}if(carry) res.push_back(1);reverse(res.begin(), res.end());return res;}};void print_vec(const vector<int>& vec){for(int e: vec) cout << e;cout << endl;}int main() {vector<int> A1 = {0};int K1 = 0;print_vec(Solution().addToArrayForm(A1, K1));return 0;}