Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created February 13, 2019 12:15
Show Gist options
  • Save fpdjsns/5dc08ca806c6d65e32e6fae6d56ed131 to your computer and use it in GitHub Desktop.
Save fpdjsns/5dc08ca806c6d65e32e6fae6d56ed131 to your computer and use it in GitHub Desktop.
[leetcode] 989. Add to Array-Form of Integer : https://leetcode.com/problems/add-to-array-form-of-integer/
/*
* 시간복잡도 : O(max(A.length, |K|))
*/
class Solution {
public:
vector<int> addToArrayForm(vector<int>& A, int K) {
int size = A.size();
int c = 0;
vector<int> answer;
for(int i=size-1;i>=0;i--){
c += A[i] + K%10;
answer.push_back(c%10);
K /= 10;
c /= 10;
}
K += c;
while(K > 0){
answer.push_back(K%10);
K/=10;
}
for(int i=0;i<answer.size()/2;i++){
int tmp = answer[i];
answer[i] = answer[answer.size()-i-1];
answer[answer.size()-i-1] = tmp;
}
return answer;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment