-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ZQKC
committed
May 23, 2017
1 parent
4cfea08
commit 697a671
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
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
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]) |
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
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; | ||
} | ||
}; |
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
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; | ||
} | ||
}; |