翻转句子中单词顺序

题目描述:
<div class="line number2 index1 alt1" style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; color: rgb(57, 57, 57); margin: 0px !important; padding: 0px 1em 0px 0px !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><code class="cpp comments" style="white-space: pre-wrap; margin: 0px !important; padding: 0px !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; color: rgb(0, 130, 0) !important; background: none !important;">题目描述:翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。</code></div><div class="line number3 index2 alt2" style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; color: rgb(57, 57, 57); margin: 0px !important; padding: 0px 1em 0px 0px !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; background: none rgb(244, 244, 244) !important;"><code class="cpp comments" style="white-space: pre-wrap; margin: 0px !important; padding: 0px !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; color: rgb(0, 130, 0) !important; background: none !important;">为简单起见,标点符号和普通字母一样处理。如:"I am a student."翻转成"student. a am I"。</code></div>

具体算法C++实现如下:
#include<iostream>
#include<vector>
#include<assert.h>
#include<cstring>
using namespace std;
 
void swap(char &a, char &b)
{
    char tmp = b;
    b = a;
    a = tmp;
}
 
void swap_str(char* str, int start, int end)
{
    assert(str!=NULL && start <= end);
    int low = start;
    int high = end;
 
    //整个句子按字符翻转
    while (low < high)
    {
        swap(str[low], str[high]);
        low++;
        high--;
    }
 
}
 
//方法一:依次读入句子中的每个单词,并将它们放入一个栈中。然后再将单词出栈。
//时间复杂度:O(n),空间复杂度:O(n);
 
//方法二:首先将整个句子按字符翻转,然后再将其中每个单词的字符旋转。
//时间复杂度:O(n),空间复杂度:O(1);
void reverse_word(char str[])
{
    int len = strlen(str);
 
    //翻转整个句子
    swap_str(str, 0, len-1);
 
    int s = 0;
    int e = 0;
    //翻转每个单词
    for (int i=0; i<len; i++)
    {
        e = i;
        if (str[e] == ' ')
        {
            //str[e]为空格,所以范围是[s,e-1].
            swap_str(str, s, e-1);
            s = e + 1;
        }
    }
}
 
int main()
{
    char str[] = "I am a student.";
    reverse_word(str);
    cout<<str<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值