-
-
Save domfarolino/b86e7e9b6c1666e0561435aeda2c2b08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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