JZ-C-42-Plus

剑指offer第四十二题-拓展:左旋转字符串:左旋转操作是把字符串前面的若干个字符转移到字符串的尾部,如输入”abcdefg”和数字2,左旋转2位后为:”cdefgab”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//============================================================================
// Name        : JZ-C-42-Plus.cpp
// Author      : Laughing_Lz
// Version     :
// Copyright   : All Right Reserved
// Description : 左旋转字符串:左旋转操作是把字符串前面的若干个字符转移到字符串的尾部,如输入"abcdefg"和数字2,左旋转2位后为:"cdefgab"
//============================================================================
 
#include <iostream>
#include <stdio.h>
#include "StringUtil.h"
#include <string.h>
using namespace std;
 
char* LeftRotateString(char* pStr, int n) {
    if (pStr != NULL) {
        int nLength = static_cast<int>(strlen(pStr));
        if (nLength > 0 && n > 0 && n < nLength) {
            char* pFirstStart = pStr;
            char* pFirstEnd = pStr + n - 1;
            char* pSecondStart = pStr + n;
            char* pSecondEnd = pStr + nLength - 1;
            // 翻转字符串的前面n个字符
            Reverse(pFirstStart, pFirstEnd);
            // 翻转字符串的后面部分
            Reverse(pSecondStart, pSecondEnd);
            // 翻转整个字符串
            Reverse(pFirstStart, pSecondEnd);
        }
    }
 
    return pStr;
}
 
// ====================测试代码====================
void Test(char* testName, char* input, int num, char* expectedResult) {
    if (testName != NULL)
        printf("%s begins: ", testName);
 
    char* result = LeftRotateString(input, num);
 
    if ((input == NULL && expectedResult == NULL)
            || (input != NULL && strcmp(result, expectedResult) == 0))
        printf("Passed.\n\n");
    else
        printf("Failed.\n\n");
}
 
// 功能测试
void Test1() {
    char input[] = "abcdefg";
    char expected[] = "cdefgab";
 
    Test("Test1", input, 2, expected);
}
 
// 边界值测试
void Test2() {
    char input[] = "abcdefg";
    char expected[] = "bcdefga";
 
    Test("Test2", input, 1, expected);
}
 
// 边界值测试
void Test3() {
    char input[] = "abcdefg";
    char expected[] = "gabcdef";
 
    Test("Test3", input, 6, expected);
}
 
// 鲁棒性测试
void Test4() {
    Test("Test4", NULL, 6, NULL);
}
 
// 鲁棒性测试
void Test5() {
    char input[] = "abcdefg";
    char expected[] = "abcdefg";
 
    Test("Test5", input, 0, expected);
}
 
// 鲁棒性测试
void Test6() {
    char input[] = "abcdefg";
    char expected[] = "abcdefg";
 
    Test("Test6", input, 7, expected);
}
 
int main(int argc, char** argv) {
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
 
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注