Skip to content

Instantly share code, notes, and snippets.

@domfarolino
Created August 23, 2016 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domfarolino/b86e7e9b6c1666e0561435aeda2c2b08 to your computer and use it in GitHub Desktop.
Save domfarolino/b86e7e9b6c1666e0561435aeda2c2b08 to your computer and use it in GitHub Desktop.
vector<int> addVectors(const vector<int>& a, const vector<int>& b) {
int maxSize = max(a.size(), b.size());
vector<int> outputVec(maxSize);
int aIndex = a.size()-1, bIndex = b.size()-1, carry = 0;
int aVal, bVal;
for (int i = maxSize-1; i >= 0; --i) {
if (aIndex < 0) aVal = 0;
else aVal = a[aIndex];
if (bIndex < 0) bVal = 0;
else bVal = b[bIndex];
outputVec[i] = (aVal + bVal + carry)%10;
if (aVal + bVal + carry > 9) carry = 1;
else carry = 0;
aIndex--;
bIndex--;
}
if (carry) outputVec.insert(outputVec.begin(), 1);
return outputVec;
}
vector<int> multiplyVectorByIntegerNaive(const vector<int>& numVector, int n) {
vector<int> returnVec = numVector;
while (n != 1) {
returnVec = addVectors(returnVec, numVector);
n--;
}
return returnVec;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment