字符串
来源:互联网 发布:盛世下的蝼蚁 知乎 编辑:程序博客网 时间:2024/06/06 01:41
比较两个字符串,用O(n)时间和恒量空间。
ANSWER:
What is “comparing two strings”? Just normal string comparison? The natural way use O(n) time and O(1) space.
int strcmp(char * p1, char * p2) {
while (*p1 != ‘\0’ && *p2 != ‘\0’ && *p1 == *p2) {
p1++, p2++;
}
if (*p1 == ‘\0’ && *p2 == ‘\0’) return 0;
if (*p1 == ‘\0’) return -1;
if (*p2 == ‘\0’) return 1;
return (*p1 - *p2); // it can be negotiated whether the above 3 if’s are necessary, I don’t like to omit them.
}
ANSWER:
What is “comparing two strings”? Just normal string comparison? The natural way use O(n) time and O(1) space.
int strcmp(char * p1, char * p2) {
while (*p1 != ‘\0’ && *p2 != ‘\0’ && *p1 == *p2) {
p1++, p2++;
}
if (*p1 == ‘\0’ && *p2 == ‘\0’) return 0;
if (*p1 == ‘\0’) return -1;
if (*p2 == ‘\0’) return 1;
return (*p1 - *p2); // it can be negotiated whether the above 3 if’s are necessary, I don’t like to omit them.
}
0 0
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- Win32消息处理API---GetMessage
- 60个常用linux 的命令
- UVA 11077 - Find the Permutations(递推)
- CSDN开源夏令营 百度数据可视化实践 ECharts(2)
- ruby on rails出现的问题ActiveModel::ForbiddenAttributesError
- 字符串
- 【Unity3D】【NGUI】如何动态给EventDelegate添加参数
- C# - CSV file reader
- OCP exam notes
- 数据挖掘笔记-分类-支持向量机SVM-1
- webApp前端开发技巧总结
- Azure编程笔记(5):长时间的异步操作带来的问题
- jQuery操作input改变value属性值
- ROR incompatible character encodings: UTF-8 and ASCII-8BIT 错误的 解决办法