Skip to content

Commit

Permalink
Easy Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ZQKC committed May 23, 2017
1 parent 4cfea08 commit 697a671
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 58.Length of Last Word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
str_list = s.strip().split(' ')
if len(str_list) == 0:
return 0
return len(str_list[len(str_list) - 1])
15 changes: 15 additions & 0 deletions 66.Plus One.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
std::vector<int> ans(digits.size());

int tag = 1;
for (int i = digits.size() - 1; i >= 0; --i) {
ans[i] = (digits[i] + tag) % 10;
tag = digits[i] + tag >= 10 ? 1: 0;
}
if (tag == 1)
ans.insert(ans.begin(), 1);
return ans;
}
};
19 changes: 19 additions & 0 deletions 67.Add Binary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
string addBinary(string a, string b) {
if (a.length() > b.length())
b = std::string(a.length() - b.length(), '0') + b;
else
a = std::string(b.length() - a.length(), '0') + a;

std::string ans;
int tag = 0;
for (int i = a.length() - 1; i >= 0; --i) {
ans = (a[i] - '0' + b[i] - '0' + tag & 1? "1": "0") + ans;
tag = a[i] - '0' + b[i] - '0' + tag >= 2 ? 1: 0;
}
if (tag == 1)
ans = '1' + ans;
return ans;
}
};

0 comments on commit 697a671

Please sign in to comment.