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
or 0
.
Example 1:
Input: a = "11", b = "1" Output: "100"
提示:使用carry保存每一位的合,%2即为该位的值,/2即为进位。
答案:
class Solution {
public:
string addBinary(string a, string b) {
string result = "";
int i = a.size() - 1, j = b.size() - 1, carry = 0;
while(i >= 0 || j >= 0 || carry == 1) {
carry += i >=0 ? a[i--] - '0' : 0;
carry += j >=0 ? b[j--] - '0' : 0;
result += char(carry % 2 + '0');
carry /= 2;
}
std::reverse(result.begin(), result.end());
return result;
}
};
69. Sqrt(x)
Implement int sqrt(int x)
.
Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
Example 1:
Input: 4 Output: 2
提示:牛顿迭代式
答案:
class Solution {
public:
int mySqrt(int x) {
long r = x;
while (r*r > x)
r = (r + x/r) / 2;
return r;
}
};
168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ...
Example 1:
Input: 1 Output: "A"
Example 2:
Input: 28 Output: "AB"
Example 3:
Input: 701 Output: "ZY"
提示:%26为最后一位,/26为剩下的
答案:
class Solution {
public:
string convertToTitle(int n) {
return n == 0 ? "" : convertToTitle(n / 26) + (char) (--n % 26 + 'A');
}
};
class Solution {
public:
string convertToTitle(int n) {
string dict("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
string ret;
while(n>0)
{
n-=1;
int remain = n%26;
n = n /26;
ret = dict[remain]+ ret;
}
return ret;
}
};
171. Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
Example 1:
Input: "A" Output: 1
Example 2:
Input: "AB" Output: 28
Example 3:
Input: "ZY" Output: 701
提示:result = result * 26 + curChar
答案:
class Solution {
public:
int titleToNumber(string s) {
int result = 0;
for (int i = 0; i < s.length(); result = result * 26 + (s.at(i) - 'A' + 1), i++);
return result;
}
};
172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Example 1:
Input: 3 Output: 0 Explanation: 3! = 6, no trailing zero.
Example 2:
Input: 5 Output: 1 Explanation: 5! = 120, one trailing zero.
Note: Your solution should be in logarithmic time complexity.
提示:求阶乘中结尾含有几个零,每10个数,就有贡献两个0(一个结尾是5一个结尾是0),另外25额外贡献1个,125比25额外 贡献 1个.......以此类推
答案:
class Solution {
public:
int trailingZeroes(int n) {
return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
}
};
class Solution {
public:
int trailingZeroes(int n) {
int count = 0;
for (long long i = 5; n / i; i *= 5)
count += n / i;
return count;
}
};
28. Implement strStr()
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba" Output: -1
Clarification:
What should we return when needle
is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle
is an empty string. This is consistent to C's strstr()and Java's indexOf().
提示:蛮力算法:从haystack坐标0开始找,注意length() - length()在C++里是无符号整数,与int比较会出错
KMP
答案:
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle.empty()) return 0;
int m = haystack.length(), n = needle.length();
for(int i = 0; i <= m - n; i++)
for(int j = 0; j < n && haystack[i+j] == needle[j]; j++)
if(j == n - 1) return i;
return -1;
}
};
125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama" Output: true
Example 2:
Input: "race a car" Output: false
提示:isalnum()判断是否是数字或者字母,toupper()转换为大写
答案:
bool isPalindrome(string s) {
for (int i = 0, j = s.size() - 1; i < j; i++, j--) { // Move 2 pointers from each end until they collide
while (isalnum(s[i]) == false && i < j) i++; // Increment left pointer if not alphanumeric
while (isalnum(s[j]) == false && i < j) j--; // Decrement right pointer if no alphanumeric
if (toupper(s[i]) != toupper(s[j])) return false; // Exit and return error if not match
}
return true;
}