Comments (2)
现在给出的实现如下:
/*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/ void ReplaceBlank(char str[], int length) { if(str == nullptr && length <= 0) return; /*originalLength 为字符串str的实际长度*/ int originalLength = 0; int numberOfBlank = 0; int i = 0; while(str[i] != '\0') { ++ originalLength; if(str[i] == ' ') ++ numberOfBlank; ++ i; } /*newLength 为把空格替换成'%20'之后的长度*/ int newLength = originalLength + numberOfBlank * 2; if(newLength > length) return; int indexOfOriginal = originalLength; int indexOfNew = newLength; while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal) { if(str[indexOfOriginal] == ' ') { str[indexOfNew --] = '0'; str[indexOfNew --] = '2'; str[indexOfNew --] = '%'; } else { str[indexOfNew --] = str[indexOfOriginal]; } -- indexOfOriginal; } }我的考虑是
originalLength
的计算,在strlen(string)
时,确实像上面的计算长度是没有错的,不用考虑字符数组最后的空字符\0
,在这里的话,考虑边界条件,个人觉得应该添加如下改动:
- 在计算
newLength
之前,将originalLength
给多计算一个字符,算上最后的空字符\0
,其他的保持不变即可;- 如果考虑和
strlen(string)
一样的语义,在计算originalLength
时就用如上的代码计算长度即可,但在判断得到的newLength
是否超出length
时,需要if(newLength +1 > length) return;这样保证替换之后的字符串的最后一位空字符是字符数组提供的,在测试时,传入的字符数组是
char str[100] = "We are happy."
,其13-99索引号均填充为\0
以上 1 和 2 两种方案选择一种即可。
还有,开头的判断:if(str == nullptr && length <= 0) 应该改为: if(str == nullptr || length <= 0)
from codinginterviewchinese2.
边界条件未处理到位的情况,当测试用例1,改为:
// 空格在句子中间
void Test1()
{
const int length = 13;
char str[length] = "hello world";
Test("Test1", str, length, "hello%20world");
}
按作者的意思是要return ;的,可实际并没有,导致后面程序崩溃。1楼,2楼讲的很好。
from codinginterviewchinese2.
Related Issues (20)
- 面试题 3-2 的错误
- 面试题60 第二种解法是不是有问题啊,会int溢出
- Partition function has extremely bad performance for array with identical values
- 面试题14剪绳子动态规划 HOT 5
- 面试题三2 题解有问题 HOT 4
- #面试题37
- 面试题30 是否有必要总是将min(value, m_min.top())压入辅助栈中? HOT 1
- 面试68题答案错误
- 面试题17的本题扩展
- c
- 面试题54:二叉搜索树的第K大节点 HOT 1
- 第二版勘误
- Singleton
- 面试题19的题目有误? HOT 1
- 面试题3 题目二部分测试用例过不了 HOT 3
- pro
- 面试题10:斐波那契数列测试用例过不去 HOT 1
- 51题,数组中的逆序对
- List.cpp 头文件 #include "list.h" 有误
- How to build and run CommonParentInTree.cpp on macOS?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from codinginterviewchinese2.