58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
Example:
Input: "Hello World"
Output: 5
Solution in C++:
- **关键点:**只有空串才没有最后一个单词,最后一个单词后面的空格不算没有最后一个单词。
- **思路:**空串没有最后一个单词,return 0;其他的从字符串最后开始扫描,扫描到第一个不为空格的单词到下一个空格之间的长度就为最后一个单词的长度。
int lengthOfLastWord(string s) {
size_t size = s.size();
// 空串
if (size == 0)
return 0;
int count = 0;
int i = size - 1;
while(s[i] == ' ')
--i;
for(; s[i] != ' ' && i >= 0; --i)
++count;
return count;
}
66. Plus One
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Solution in C++:
- 关键点:进位记忆及最高位处理。
- 思路:只有最低位需要+1,其他位都是根据进位来判断是否需要加,这里没有进行进位为0不需要进行加法操作的优化,图代码简单直接都进行加法处理。每次讲得到的数不用再反转。
vector<int> plusOne(vector<int>& digits) {
vector<int> result;
size_t size = digits.size();
if ( size == 0){
result.push_back(1);
return result;
}
size_t carry = 0;
int tmp;
for (int i = size - 1; i >= 0; --i){
if (i == size - 1)
tmp = digits[i] + 1;
else
tmp = digits[i] + carry;
if(tmp >= 10){
carry = 1;
result.insert(result.begin(), tmp % 10);
} else{
carry = 0;
result.insert(result.begin(), tmp);
}
}
if (carry == 1)
result.insert(result.begin(), 1);
return result;
}
67. Add Binary
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1
or0
.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
Solution in C++:
- 关键点:两个字符串不同位数的处理&&进位最高位的处理。
- 思路:和上一题的思路差不多就是具体的加法操作不同,从尾部开始处理存入字符串,然后将字符串反转即可得到最终的字符串。
string addBinary(string a, string b) {
string s = "";
string table = "01";
size_t sizea = a.size(), sizeb = b.size();
// 相加
size_t carry = 0;
int i, j;
for (i = sizea - 1, j = sizeb - 1; i >= 0 && j >= 0; --i, --j){
int tmp = a[i] - '0' + b[j] - '0' + carry;
if ( tmp >= 2){
carry = 1;
s += table[tmp % 2];
} else{
carry = 0;
s += table[tmp];
}
}
int tmp = 0;
if (i >= 0){ // i的位数多于j的位数
if (carry == 0){
while(i >= 0){
s += a[i];
--i;
}
} else{
while(i >= 0){
int tmp = a[i] - '0' + carry;
if ( tmp >= 2){
carry = 1;
s += table[tmp % 2];
} else{
carry = 0;
s += table[tmp];
}
--i;
}
}
} else if (j >= 0){ // j的位数多于i的位数
if (carry == 0){
while(j >= 0){
s += b[j];
--j;
}
} else{
while(j >= 0){
int tmp = b[j] - '0' + carry;
if ( tmp >= 2){
carry = 1;
s += table[tmp % 2];
} else{
carry = 0;
s += table[tmp];
}
--j;
}
}
} else{ // i和j位数相同
tmp = carry;
}
if ( carry == 1)
s += table[1];
// 反转
size_t sizes = s.size();
for (int i = 0; i < sizes / 2; ++i){
char tmp = s[i];
s[i] = s[sizes-i-1];
s[sizes-i-1] = tmp;
}
return s;
}
小结
今天时间比较早,而且题目比较简单,就做了三题,感觉基本没什么难度,可能也是因为自己没有做什么优化。技能上可能没什么大的收获,但是确定了以后刷题得以时间为单位去定,而不是以题目去定了。