面试题58(一):翻转单词顺序(简单)

一、题目

输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student. ",则输出"student. a am I"。

二、关键

1.反转两次。第一次:“.tneduts a ma i”,第二次,每个单词反转:“student. a am i”

2.反转字符串的一段的函数reverse。

三、解释

四、代码

#include <cstdio>
#include "..\Utilities\StringUtil.h"
#include <string>

void Reverse(char*pBegin, char* pEnd)
{
    if(pBegin==nullptr||pEnd == nullptr)
        return ;
    while(pBegin<pEnd)
    {
        char temp = *pBegin;
        *pBegin = *pEnd;
        *pEnd = temp;
        
        pBegin++,pEnd++;
    }
}
    
char* ReverseSentence(char *pData)
{
    if(pData == nullptr)
        return nullptr;

    char *pBegin = pData;

    char *pEnd = pData;
    while(*pEnd != '\0')
        pEnd ++;
    pEnd--;

    // 翻转整个句子
    Reverse(pBegin, pEnd);

    // 翻转句子中的每个单词
    pBegin = pEnd = pData;
    while(*pBegin != '\0')
    {
        if(*pBegin == ' ')
        {
            pBegin ++;
            pEnd ++;
        }
        else if(*pEnd == ' ' || *pEnd == '\0')
        {
            Reverse(pBegin, --pEnd);
            pBegin = ++pEnd;
        }
        else
            pEnd ++;
    }

    return pData;
}

// ====================测试代码====================
void Test(const char* testName, char* input, const char* expectedResult)
{
    if(testName != nullptr)
        printf("%s begins: ", testName);

    ReverseSentence(input);

    if((input == nullptr && expectedResult == nullptr)
        || (input != nullptr && strcmp(input, expectedResult) == 0))
        printf("Passed.\n\n");
    else
        printf("Failed.\n\n");
}

// 功能测试,句子中有多个单词
void Test1()
{
    char input[] = "I am a student.";
    char expected[] = "student. a am I";

    Test("Test1", input, expected);
}

// 功能测试,句子中只有一个单词
void Test2()
{
    char input[] = "Wonderful";
    char expected[] = "Wonderful";

    Test("Test2", input, expected);
}

// 鲁棒性测试
void Test3()
{
    Test("Test3", nullptr, nullptr);
}

// 边界值测试,测试空字符串
void Test4()
{
    Test("Test4", "", "");
}

// 边界值测试,字符串中只有空格
void Test5()
{
    char input[] = "   ";
    char expected[] = "   ";
    Test("Test5", input, expected);
}

int main(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你要我回答的是关于分治法的面试题。 题目描述:有一个大矩形和若干个小矩形,小矩形可能重叠,求用这些小矩形覆盖大矩形的最小面积。 解题思路:这是一个经典的分治问题,我们可以将大矩形分成四个小矩形,然后分别求出每个小矩形被覆盖的最小面积,最终的答案就是这四个小矩形被覆盖的最小面积的和。 具体实现:我们可以使用递归来实现这个算法,具体步骤如下: 1. 如果小矩形集合为空,则返回大矩形的面积。 2. 将大矩形分成四个小矩形 A、B、C、D。 3. 对于每个小矩形,计算它被覆盖的最小面积。 4. 返回四个小矩形被覆盖的最小面积的和。 代码实现如下: ```python class Rectangle: def __init__(self, x1, y1, x2, y2): self.x1, self.y1, self.x2, self.y2 = x1, y1, x2, y2 def min_area(rectangles): def helper(rectangles): if not rectangles: return float('inf') x1 = min(rectangle.x1 for rectangle in rectangles) y1 = min(rectangle.y1 for rectangle in rectangles) x2 = max(rectangle.x2 for rectangle in rectangles) y2 = max(rectangle.y2 for rectangle in rectangles) if x1 >= x2 or y1 >= y2: return 0 area = (x2 - x1) * (y2 - y1) for i in range(len(rectangles)): if rectangles[i].x1 >= x2 or rectangles[i].x2 <= x1 or rectangles[i].y1 >= y2 or rectangles[i].y2 <= y1: continue area -= (rectangles[i].x2 - rectangles[i].x1) * (rectangles[i].y2 - rectangles[i].y1) return min(helper([rectangle for rectangle in rectangles if rectangle.x2 <= x1 or rectangle.y2 <= y1 or rectangle.x1 >= x2 or rectangle.y1 >= y2]), helper([rectangle for rectangle in rectangles if rectangle.x2 <= x1 or rectangle.y2 <= y1 or rectangle.x1 >= x2 or rectangle.y1 >= y1]), helper([rectangle for rectangle in rectangles if rectangle.x2 <= x1 or rectangle.y2 <= y2 or rectangle.x1 >= x2 or rectangle.y1 >= y1]), helper([rectangle for rectangle in rectangles if rectangle.x2 <= x2 or rectangle.y2 <= y1 or rectangle.x1 >= x1 or rectangle.y1 >= y2])) + area return helper(rectangles) ``` 这个算法的时间复杂度是 O(nlogn),其中 n 是小矩形的数量,因为每次递归都会将小矩形的数量减半。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值