Skip to content
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

Fix bug in SortArrayForMinNumber.cpp #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 13 additions & 13 deletions 45_SortArrayForMinNumber/SortArrayForMinNumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ Distributed under the BSD license.
*******************************************************************/

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

// ������45���������ų���С����
// ��Ŀ������һ�����������飬����������������ƴ�������ų�һ��������ӡ��ƴ
// �ӳ���������������С��һ����������������{3, 32, 321}�����ӡ����3����
// �����ųɵ���С����321323��
// 面试题45:把数组排成最小的数
// 题目:输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼
// 接出的所有数字中最小的一个。例如输入数组{3, 32, 321},则打印出这3个数
// 字能排成的最小数字321323。

#include "cstdio"
#include <string>
#include <algorithm>

int compare(const void* strNumber1, const void* strNumber2);

// int��������ʮ���Ʊ�ʾ���ֻ��10λ
// int型整数用十进制表示最多只有10位
const int g_MaxNumberLength = 10;

char* g_StrCombine1 = new char[g_MaxNumberLength * 2 + 1];
Expand All @@ -34,7 +34,7 @@ void PrintMinNumber(const int* numbers, int length)
if(numbers == nullptr || length <= 0)
return;

char** strNumbers = (char**)(new int[length]);
char** strNumbers = (char**)(new char*[length]);
for(int i = 0; i < length; ++i)
{
strNumbers[i] = new char[g_MaxNumberLength + 1];
Expand All @@ -52,9 +52,9 @@ void PrintMinNumber(const int* numbers, int length)
delete[] strNumbers;
}

// ���[strNumber1][strNumber2] > [strNumber2][strNumber1], ����ֵ����0
// ���[strNumber1][strNumber2] = [strNumber2][strNumber1], ����ֵ����0
// ���[strNumber1][strNumber2] < [strNumber2][strNumber1], ����ֵС��0
// 如果[strNumber1][strNumber2] > [strNumber2][strNumber1], 返回值大于0
// 如果[strNumber1][strNumber2] = [strNumber2][strNumber1], 返回值等于0
// 如果[strNumber1][strNumber2] < [strNumber2][strNumber1], 返回值小于0
int compare(const void* strNumber1, const void* strNumber2)
{
// [strNumber1][strNumber2]
Expand All @@ -68,7 +68,7 @@ int compare(const void* strNumber1, const void* strNumber2)
return strcmp(g_StrCombine1, g_StrCombine2);
}

// ====================���Դ���====================
// ====================测试代码====================
void Test(const char* testName, int* numbers, int length, const char* expectedResult)
{
if(testName != nullptr)
Expand Down Expand Up @@ -107,7 +107,7 @@ void Test4()
Test("Test4", numbers, sizeof(numbers)/sizeof(int), "111111");
}

// ������ֻ��һ������
// 数组中只有一个数字
void Test5()
{
int numbers[] = {321};
Expand Down