字符串匹配
1. brute force
char *strstr(const char *str1, const char *str2)
{
char *cp = (char*)str1;
char *s1, *s2;
if (*str2 == '\0')
return ((char *)str1);
while (*cp)
{
s1 = cp;
s2 = (char *) str2;
while (*s1 && *s2 && !(*s1 - *s2))
s1++, s2++;
if (!*s2)
return(cp);
cp++;
}
return(NULL);
}
2. KMP
void getnext(const char* str, int* next) {
int k = -1 , i = 0;
next[0] = -1;
int len = strlen(str);
while ( i < len) {
if (k == -1 || str[i] == str[k] ) {
next[++i] = ++k;
}
else {
k = next[k];
}
}
}
int strStr(const char* source, const char* target, int* next) {
if (source == NULL || target == NULL)
return -1;
if (!(*source) && !(*target))
return 0;
getnext(target, next);
int i = 0, k = 0;
int slen = strlen(source);
int tlen = strlen(target);
while ( i < slen && k < tlen) {
if (k == -1 || source[i] == target[k]) {
++i; ++k;
}
else {
k = next[k];
}
}
return target[k] ? -1 : i - k;
}
字符串替换(变长)
题意 :请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.
则经过替换之后的字符串为We%20Are%20Happy。
class Solution {
public:
void replaceSpace(char *str,int length) {
if (length == 0){
return;
}
int num = 0;
for (int i = 0; i < length; ++i){
if (str[i] == ' '){
num++;
}
}
int newLength = length + 2 * num;
for (int i = length - 1, j = newLength - 1; i >=0 || j >= 0;){
if (str[i] == ' '){
str[j--] = '0';
str[j--] = '2';
str[j--] = '%';
i--;
} else{
str[j--] = str[i--];
}
}
str[newLength] = '\0';
}
};
字符串删除(变短)
题意:删除word和word之间的多余空格,行首和行尾不可以有多余空格
static void delExtraSpace(char *s){
int len = strlen(s);
int newLen = 0;
for(int i = 0, j = 0; i < len;){
while (i < len && s[i] == ' ' ){ // 多余空格删除
i++;
}
while (i < len && s[i] != ' '){
s[j++] = s[i++];
newLen = j;
}
if (i < len){
s[j++] = s[i++]; // 空格
}
}
s[newLen] = '\0';
}
字符串逆置
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
static void reverse(int low, int high, char* s){
for(;low < high; low++, high--){
s[low] ^= s[high];
s[high] ^= s[low];
s[low] ^= s[high];
}
}
void reverseWords(char *s) {
int len = strlen(s);
reverse(0, len - 1, s);
for(int low = -1, i = 0; i < len; ++i){
if (s[i] == ' '){
if (low != -1){
reverse(low, i - 1, s);
low = -1;
}
} else if (low == -1){
low = i;
}
if (i == len - 1 && low != -1){ // easy to lost this part
reverse(low, i, s);
}
}
delExtraSpace(s);
}
字符串 四则运算
double proccess(string& s) {
stringstream sstr("+" + s + "+");
double result = 0;
double partial = 0;
double num;
char op;
while (sstr >> op) {
switch (op) {
case '+':
case '-':
result += partial;
sstr >> partial;
partial *= (op == '+' ? 1 : -1);
break;
case '*':
sstr >> num;
partial *= num;
break;
case '/':
sstr >> num;
partial /= num;
}
}
return result;
}
atoi
int myAtoi(char* str) {
if (str == NULL) {
return 0;
}
while (*str != '\0' && *str == ' '){
++str;
}
int signal = 1;
if (*str == '-') {
signal = -1;
++str;
} else if (*str == '+') {
++str;
}
int res = 0;
while (*str != '\0') {
if (*str >= '0' && *str <= '9') {
res = res * 10;
res += signal * (*str - '0');
} else {
break;
}
++str;
}
return res;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。