66. Plus One
题目
这题很简单,直接代码:
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int a = ;
vector<int> ans;
vector<int>::iterator it;
for(int i = digits.size() - ;i >= ;i--)
{
it = ans.begin();
int b = (a + digits[i]) % ;
a = (a + digits[i]) / ;
ans.insert(it, b);
}
if(a != )
{
it = ans.begin();
ans.insert(it, a);
} return ans;
}
};
------------------------------------------------------------------------------------分割线--------------------------------------------------------------
67. Add Binary
题目
直接代码
class Solution {
public:
string addBinary(string a, string b) {
int lenA,lenB;
lenA = a.length();
lenB = b.length();
string res=""; if ( == lenA)
{
return b;
}
if ( == lenB)
{
return a;
}
int ia=lenA-,ib=lenB-;
int count=,temp;//进位
char c;
while (ia>=&&ib>=)
{
temp = a[ia]-''+b[ib]-''+count;
count = temp/;
c = temp%+'';
res = c+res;
ia--;
ib--;
} while (ia>=)
{
temp = a[ia]-''+count;
count = temp/;
c = temp%+'';
res = c+res;
ia--;
} while (ib>=)
{
temp = b[ib]-''+count;
count = temp/;
c = temp%+'';
res = c+res;
ib--;
}
if(count != )
{
c=count+'';
res = c+res;
}
return res; }
};