8. String to Integer (atoi)
问题描述:Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
解题思路:这道题主要的难点有二:
第一在于合法的输入样例不明确,例如整数的开头能不能带0,带0的整数要不要算做八进制数等都是需要尝试才知道的问题。
第二,如何判断当前的数字是否越界。在这里本人使用的方法是:
以INT_MAX 2147483647为例,其是一个10位数。 对于一个10进制数a1a2a3...an,可以将其拆分为a1 * 10 ^(n-1) + a2a3...an。这样当n==10时,若a1 >= 2 且 a1a2....an >= 147483647,我们可以知道a1....an >= INT_MAX,我们可以直接返回INT_MAX。
代码如下:
class Solution {
public:
int myAtoi(string str) {
bool isPositive = true;
int index = 0;
while (str[index] == ' ')
++index;
if (index == str.length()) return 0;
if (str[index] != '+' && str[index] != '-' && (str[index] < '0' || str[index] > '9')) return 0;
if (str[index] == '+' || str[index] == '-'){
if (!(index + 1 != str.length() && str[index + 1] >= '0' && str[index + 1] <= '9'))return 0;
}
if (str[index] == '+' || str[index] == '-'){
isPositive = str[index] == '+' ? true : false;
++index;
}
//if (str[index] == '0') return 0;
int res = 0;
int bits = 0;
while (index != str.length() && str[index] >= '0' && str[index] <= '9'){
if (bits >= 10){
return isPositive ? INT_MAX : INT_MIN;
}
if (bits == 9){
int tmp = (int)pow(10, 8);
int head = res / tmp;
int rem = res % tmp;
rem = rem * 10 + str[index] - '0';
if (isPositive == true && head >= 2 && rem >= 147483647){
return INT_MAX;
}
if (isPositive == false && head >= c && rem >= 147483648){
return INT_MIN;
}
}
res = res * 10 + (str[index] - '0');
++index;
++bits;
}
//cout << (isPositive) << endl;
int as = (isPositive == true ? 1 : -1);
return as * res;
}
};
结果如下:
151. Reverse Words in a String
题目描述:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
这道题是一道简单题,主要的疑惑点是不知道题目对于分割符的限定以及处理方式,WA几次后终于知道分隔符全是空格,且重组后每个单词间有且只有一个空格;但是题目中有一行要求是C程序员需要在O(1)的辅助空间下完成这个操作,这就真的让我百思不得其解了。
代码如下:
using namespace std;
class Solution {
public:
void reverseWords(string &s) {
bool isNull = false;
for (int i = 0; i < s.length(); ++i){
if (s[i] != ' '){
isNull = true;
break;
}
}
if (isNull == false){
s = "";
return;
}
string res(s);
int index = 0;
int begin = s.length() - 1;
int end = begin;
for (int i = s.length() - 1; i >= 0; --i){
if (s[i] == ' '){
for (int j = i + 1; j <= end; j++){
res[index++] = s[j];
}
res[index++] = ' ';
while (i >= 0 && s[i] == ' '){
//res[index++] = s[i];
--i;
}
end = i;
}
}
if (s[0] != ' '){
for (int i = 0; i <= end; ++i){
res[index++] = s[i];
}
}
if (s[0] == ' ' && s[s.length() - 1] == ' '){
s = res.substr(1, index - 2);
}
else if (s[0] == ' ' && s[s.length() - 1] != ' '){
s = res.substr(0, index - 1);
}
else if (s[0] != ' ' && s[s.length() - 1] == ' '){
s = res.substr(1, index - 1);
}
else{
s = res;
}
}
};
运行结果如下:
125. Valid Palindrome
问题描述:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama"
is a palindrome.
"race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
解题思路:简单题,直接在字符串头尾设置两个指针,当指针指向的字符不是合法字符时一直滑动到下一个合法字符出现的位置。当前两个指针指向的合法字符若不相等返回false,否则当头指针begin >= 尾指针end时返回true。代码如下:
using namespace std;
#include<algorithm>
using namespace std;
class Solution {
public:
bool isPalindrome(string s) {
int begin = 0;
int end = s.length() - 1;
for (int i = 0; i < s.length(); ++i){
if (s[i] >= 'A' && s[i] <= 'Z'){
s[i] += 'a' - 'A';
}
}
while (begin < end){
while (!IsValidC(s[begin])){
++begin;
}
while (!IsValidC(s[end])){
--end;
}
if (end <= begin) return true;
if (s[begin] != s[end]) {
return false;
}
++begin;
--end;
}
return true;
}
bool IsValidC(char c){
return ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9')) ? true : false;
}
};
程序运行结果如下: