Skip to content

Update ReplaceSpaces.cpp for array bounds #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions 05_ReplaceSpaces/ReplaceSpaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ Distributed under the BSD license.
*******************************************************************/

//==================================================================
// ����ָOffer�����������Թپ������ͱ���⡷����
// ���ߣ��κ���
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================

// ������5���滻�ո�
// ��Ŀ����ʵ��һ�����������ַ����е�ÿ���ո��滻��"%20"���������롰We are happy.����
// �������We%20are%20happy.����
// 面试题5:替换空格
// 题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入“We are happy.”,
// 则输出“We%20are%20happy.”。

#include <cstdio>
#include <cstring>

/*length Ϊ�ַ�����str�������������ڻ�����ַ���str��ʵ�ʳ���*/
/*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/
void ReplaceBlank(char str[], int length)
{
if(str == nullptr && length <= 0)
return;

/*originalLength Ϊ�ַ���str��ʵ�ʳ���*/
/*originalLength 为字符串str的实际长度*/
int originalLength = 0;
int numberOfBlank = 0;
int i = 0;
Expand All @@ -39,9 +39,9 @@ void ReplaceBlank(char str[], int length)
++ i;
}

/*newLength Ϊ�ѿո��滻��'%20'֮��ij���*/
/*newLength 为把空格替换成'%20'之后的长度*/
int newLength = originalLength + numberOfBlank * 2;
if(newLength > length)
if((newLength+1) > length)
return;

int indexOfOriginal = originalLength;
Expand All @@ -63,7 +63,7 @@ void ReplaceBlank(char str[], int length)
}
}

// ====================���Դ���====================
// ====================测试代码====================
void Test(char* testName, char str[], int length, char expected[])
{
if(testName != nullptr)
Expand All @@ -81,7 +81,7 @@ void Test(char* testName, char str[], int length, char expected[])
printf("failed.\n");
}

// �ո��ھ����м�
// 空格在句子中间
void Test1()
{
const int length = 100;
Expand All @@ -90,7 +90,7 @@ void Test1()
Test("Test1", str, length, "hello%20world");
}

// �ո��ھ��ӿ�ͷ
// 空格在句子开头
void Test2()
{
const int length = 100;
Expand All @@ -99,7 +99,7 @@ void Test2()
Test("Test2", str, length, "%20helloworld");
}

// �ո��ھ���ĩβ
// 空格在句子末尾
void Test3()
{
const int length = 100;
Expand All @@ -108,7 +108,7 @@ void Test3()
Test("Test3", str, length, "helloworld%20");
}

// �����������ո�
// 连续有两个空格
void Test4()
{
const int length = 100;
Expand All @@ -117,13 +117,13 @@ void Test4()
Test("Test4", str, length, "hello%20%20world");
}

// ����nullptr
// 传入nullptr
void Test5()
{
Test("Test5", nullptr, 0, nullptr);
}

// ��������Ϊ�յ��ַ���
// 传入内容为空的字符串
void Test6()
{
const int length = 100;
Expand All @@ -132,7 +132,7 @@ void Test6()
Test("Test6", str, length, "");
}

//��������Ϊһ���ո���ַ���
//传入内容为一个空格的字符串
void Test7()
{
const int length = 100;
Expand All @@ -141,7 +141,7 @@ void Test7()
Test("Test7", str, length, "%20");
}

// ������ַ���û�пո�
// 传入的字符串没有空格
void Test8()
{
const int length = 100;
Expand All @@ -150,7 +150,7 @@ void Test8()
Test("Test8", str, length, "helloworld");
}

// ������ַ���ȫ�ǿո�
// 传入的字符串全是空格
void Test9()
{
const int length = 100;
Expand Down
27 changes: 14 additions & 13 deletions 17_Print1ToMaxOfNDigits/Print1ToMaxOfNDigits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ Distributed under the BSD license.
*******************************************************************/

//==================================================================
// ����ָOffer�����������Թپ������ͱ���⡷����
// ���ߣ��κ���
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================

// ������17����ӡ1������nλ��
// ��Ŀ����������n����˳���ӡ����1����nλʮ����������������3����
// ��ӡ��1��2��3һֱ������3λ����999��
// 面试题17:打印1到最大的n位数
// 题目:输入数字n,按顺序打印出从1最大的n位十进制数。比如输入3,则
// 打印出1、2、3一直到最大的3位数即999。

#include <cstdio>
#include <memory>
#include <cstring>

void PrintNumber(char* number);
bool Increment(char* number);
void Print1ToMaxOfNDigitsRecursively(char* number, int length, int index);

// ====================����һ====================
// ====================方法一====================
void Print1ToMaxOfNDigits_1(int n)
{
if (n <= 0)
Expand All @@ -41,8 +42,8 @@ void Print1ToMaxOfNDigits_1(int n)
delete[]number;
}

// �ַ���number��ʾһ�����֣��� number������1
// ������ӷ�������򷵻�true������Ϊfalse
// 字符串number表示一个数字,在 number上增加1
// 如果做加法溢出,则返回true;否则为false
bool Increment(char* number)
{
bool isOverflow = false;
Expand Down Expand Up @@ -76,7 +77,7 @@ bool Increment(char* number)
return isOverflow;
}

// ====================������====================
// ====================方法二====================
void Print1ToMaxOfNDigits_2(int n)
{
if (n <= 0)
Expand Down Expand Up @@ -109,9 +110,9 @@ void Print1ToMaxOfNDigitsRecursively(char* number, int length, int index)
}
}

// ====================��������====================
// �ַ���number��ʾһ�����֣����������ɸ�0��ͷ
// ��ӡ��������֣������Կ�ͷ��0
// ====================公共函数====================
// 字符串number表示一个数字,数字有若干个0开头
// 打印出这个数字,并忽略开头的0
void PrintNumber(char* number)
{
bool isBeginning0 = true;
Expand All @@ -131,7 +132,7 @@ void PrintNumber(char* number)
printf("\t");
}

// ====================���Դ���====================
// ====================测试代码====================
void Test(int n)
{
printf("Test for %d begins:\n", n);
Expand Down
16 changes: 8 additions & 8 deletions 41_StreamMedian/StreamMedian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ Distributed under the BSD license.
*******************************************************************/

//==================================================================
// ����ָOffer�����������Թپ������ͱ���⡷����
// ���ߣ��κ���
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================

// ������41���������е���λ��
// ��Ŀ����εõ�һ���������е���λ����������������ж�����������ֵ����ô
// ��λ������������ֵ����֮��λ���м����ֵ��������������ж���ż������ֵ��
// ��ô��λ������������ֵ����֮���м���������ƽ��ֵ��
// 面试题41:数据流中的中位数
// 题目:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么
// 中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,
// 那么中位数就是所有数值排序之后中间两个数的平均值。

#include <cstdio>
#include <algorithm>
Expand Down Expand Up @@ -73,7 +73,7 @@ template<typename T> class DynamicArray
if((size & 1) == 1)
median = min[0];
else
median = (min[0] + max[0]) / 2;
median = (min[0] + max[0]) / 2.0;

return median;
}
Expand All @@ -83,7 +83,7 @@ template<typename T> class DynamicArray
vector<T> max;
};

// ====================���Դ���====================
// ====================测试代码====================
void Test(char* testName, DynamicArray<double>& numbers, double expected)
{
if(testName != nullptr)
Expand Down