Skip to content

add solution for 066 plus one #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions C++/066_Plus_One.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//66. Plus One
/*
*Given a non-negative number represented as an array of digits, plus one to the number.
*
*The digits are stored such that the most significant digit is at the head of the list.
*
*Tag: Array, Math
*
*Author: Linsen Wu
*/

#include "stdafx.h"
#include "iostream"
#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int n = digits.size();
vector<int> result (digits.size(),0);

int sum = 0;
int carry = 1;

for (int i = n - 1; i >=0; i--)
{
sum = carry + digits[i];
carry = sum/10;
result[i] = sum%10;
}

if (carry > 0)
{
result.insert(result.begin(), carry);
}

return result;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}