add longest_continuious_number

This commit is contained in:
nonstriater
2015-12-02 16:46:09 +08:00
parent 08b93b3f85
commit 168d7ca6d1
2 changed files with 148 additions and 18 deletions
+92 -18
View File
@@ -333,6 +333,33 @@ char *delete_occurence_characterset(char *source,const char *del);
#### 删除字符串中的数字并压缩字符串
如字符串”abc123de4fg56”处理后变为”abcdefg”。注意空间和效率。(下面的算法只需要一次遍历,不需要开辟新空间,时间复杂度为O(N))
这道题跟上一道题也是一个意思。
示例代码:
```
char *trim_number(char *source){
char *start = source;
char *end = source;
if (source == NULL) return NULL;
while(*end != '\0'){
if (*end < '0' || *end > '9' ){
*start = *end;
start++;
}
end++;
}
*start = '\0';
return source;
}
```
#### 字符串原地压缩
题目描述:“abeeeeegaaaffa" 压缩为 "abe5ag3f2a",请编程实现。
@@ -356,13 +383,69 @@ char *compress(const char *src,char *dest){
#### 删除字符串中的数字并压缩字符串
如字符串”abc123de4fg56”处理后变为”abcdefg”。注意空间和效率。
(下面的算法只需要一次遍历,不需要开辟新空间,时间复杂度为O(N))
#### 字符串中找出连续最长的数字串
写一个函数,功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串赋值给其中一个函数参数outputstr所指内存。
例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789
它的原形是:
```
void trim_number(char *source);
int longest_continuious_number(const char *input,char *output)
```
应该有3个指针,第一个指针指向一个当前最长数字串的第一个数字,第二个指针指向第二个数字串的第一个数字,第三个指针是遍历指针,且统计第二个数字串的长度;当统计出来的长度大于第一个数字串的长度,第一个指针指向第二个指针指向的数字,相反,第二个指针和第三个指针继续向后查找。
当end首次碰到数字时,且max=0,说明是首次出现数字,第一个/二个指针移到该数字,继续遍历
end首次碰到数字,且max!=0,说明出现第二个数字串,tmp=1
如果数字后面还是数字,tmp!=0 就是第二个数字串,因此 tmp += 1; 否则还是max += 1;
当end从数字到普通字符时,如果tmp!=0,就要比较 tmp 和 max
```
int longest_continuious_number(const char *input,char *output){
int max = 0;
char *start= input;
char *mid = input;
char *end = input;
int tmp = 0;
if (input == NULL || output == NULL) return 0;
while (*end != '\0'){
if (*end < '0' || *end < '9'){//字母
if(tmp > max){
max = tmp;
start = mid;
}
tmp = 0;
}else{//数字
if (tmp == 0){//发现数字
mid = end;
}
tmp++;
}
end++;
}
//修改已数字结尾的bug
if(tmp > max){
max = tmp;
start = mid;
}
//copy
int i=0;
while(i<max){
*(output+i) = *(start+i);
i++;
}
*(output+i)='\0';
return max;
}
```
@@ -378,7 +461,11 @@ int max_symmetrical_char_length(const char *scr);
思路一:蛮力法,3重循环(类似 求子数组的最大和 fmax(i,j)问题), fmax(i,j)区间i,j是最长的对称字符
思路二:遍历所有子串,然后判读是否对称 O(n^2)
思路三:有个O(n)复杂度的 http://www.jb51.net/article/48090.htm
思路三:有个O(n)复杂度的算法 http://www.cnblogs.com/McQueen1987/p/3559497.html 分析过程如下:
1.
2.
3.
```
@@ -388,19 +475,6 @@ int max_symmetrical_char_length(const char *scr){
```
#### 字符串中找出连续最长的数字串
写一个函数,功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串赋值给其中一个函数参数outputstr所指内存。
例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789
它的原形是:
```
int continumax(char *outputstr,char *intputstr)
```
#### 子串匹配的个数
@@ -0,0 +1,56 @@
#include "stdio.h"
int longest_continuious_number(const char *input,char *output){
int max = 0;
char *start= input;
char *mid = input;
char *end = input;
int tmp = 0;
if (input == NULL || output == NULL) return 0;
while (*end != '\0'){
if (*end < '0' || *end > '9'){//字母
if(tmp > max){
max = tmp;
start = mid;
}
tmp = 0;
}else{//数字
if (tmp == 0){//发现数字
mid = end;
}
tmp++;
}
end++;
}
//修改已数字结尾的bug
if(tmp > max){
max = tmp;
start = mid;
}
//copy
int i=0;
while(i<max){
*(output+i) = *(start+i);
i++;
}
*(output+i)='\0';
return max;
}
int main(int argc, char const *argv[])
{
const char test[] = "abcd12345ed125ss123456789";
char output[100] = {};
int max = longest_continuious_number(test,output);
printf("max=%d,number=%s\n", max,output);
return 0;
}