1.题目截图(把字符串转换成整数----atoi)
1.1题目解析(在代码里)
class Solution {
public:
int myAtoi(string str) {
// 100% 97.45%
int len = str.size();
if(len == 0)
return 0;
int i = 0, flag = 1, isSignal = 0, res = 0;
while(i < len && str[i] == ' ') //跳过空格
i++;
if(i >= len) //如果全是空格
return 0;
while(i < len && str[i] == '-' || str[i] == '+'){ //取符号
if(str[i] == '-')
flag = -1;
i++;
isSignal++;
if(isSignal > 1) //不能超过一个符号
return 0;
}
while(isdigit(str[i])){ //只要前面的那些数字
if(res > INT_MAX / 10 || (res == INT_MAX / 10 && str[i] > '7')) //用int来控制不溢出
return flag == 1? INT_MAX : INT_MIN;
res = res * 10 - '0' + str[i];
i++;
}
return flag * res;
}
};
2.Leetcode415--字符串相加
class Solution {
public:
string addStrings(string num1, string num2)
{
int end1 = num1.size() - 1;
int end2 = num2.size() - 1;
string str;
int next = 0; //用来进位
while(end1 >= 0 || end2 >= 0)
{
int x1 = end1 >= 0 ? num1[end1--] - '0' : 0;
int x2 = end2 >= 0 ? num2[end2--] - '0' : 0;
int x = x1 + x2 + next;
//处理一下进位
next = x / 10;
x = x % 10;
//尾插
str += ('0' + x);
}
if(next == 1)
{
str += '1';
}
reverse(str.begin() , str.end());
return str;
}
};
3.Leetcode----344.反转字符串
3.1用双指针解决问题
class Solution {
public:
void reverseString(vector<char>& s)
{
int n = s.size();
for(int left = 0,right = n - 1;left < right;left ++ , right--)
{
swap(s[left] , s[right]);
}
}
};
4.Leetcode387--- 字符串中的第一个唯一字符
class Solution {
public:
int firstUniqChar(string s)
{
//统计次数
int count[26] = {0};
for(auto ch : s)
{
count[ch-'a']++;
}
for(size_t i = 0;i < s.size();++i)
{
if(count[s[i] - 'a' ]== 1)
{
return i;
}
}
return -1;
}
};
5.Leetcode----125 验证回文串
class Solution {
public:
bool isLetterOrNumber(char ch)
{
return (ch>='0'&&ch <='9')
|| (ch >='a'&& ch <='z')
|| (ch>='A' && ch <= 'Z');
}
bool isPalindrome(string s)
{
for(auto& ch : s)
{
if(ch >='A' && ch <= 'Z')
{
ch += 32;
}
}
int begin = 0 , end = s.size()-1;
while(begin < end)
{
while(begin < end && !isLetterOrNumber(s[begin]))
++begin;
while(begin < end && !isLetterOrNumber(s[end]))
--end;
if(s[begin] != s[end])
{
return false;
}
else
{
++begin;
--end;
}
}
return true;
}
};
6.Leetcode---43.字符串相乘
class Solution
{
public:
void MulItem(string &tmp, string &num1, char a)
{
int i = 0, sign=0;
int mul = 0;
while(i < num1.size())
{
mul = (num1[i]-'0') * (a-'0') + sign;
if(mul >= 10)
{
sign = mul / 10;
mul %= 10;
}
else
sign = 0;
tmp.push_back(mul+'0');
i++;
}
if(sign > 0)
tmp.push_back(sign+'0');
}
//对应为相加,sign进位采用引用传递
int AddItem(int a, int b, int &sign)
{
int add = a+b+sign;
if(add >= 10)
{
sign = 1;
add -= 10;
}
else
sign = 0;
return add;
}
//错位相加
void MoveAdd(string &result, string &tmp, int k)
{
int i, j;
i = k;
j = 0;
int sign = 0;
while(i<result.size() &&j<tmp.size())
{
result[i] = AddItem(result[i]-'0', tmp[j]-'0', sign) + '0';
i++;
j++;
}
while(i<result.size() && sign)
{
result[i] = AddItem(result[i]-'0', 0, sign)+'0';
i++;
}
while(j < tmp.size())
{
int v = AddItem(0, tmp[j]-'0', sign);
result.push_back(v+'0');
j++;
}
if(sign)
result.push_back(sign+'0');
}
string multiply(string num1, string num2)
{
//先翻转数据,方便进位处理
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
string tmp, result;
for(int i=0; i<num2.size(); ++i)
{
//使用num2的每一个数据乘以num1
MulItem(tmp, num1, num2[i]);
//将乘得的结果进行错位相加
MoveAdd(result, tmp, i);
tmp.clear();
}
while(result.size()!=1 && result.back()=='0')
result.pop_back();
//翻转数据,恢复数据
reverse(result.begin(), result.end());
return result;
}
};