首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >挑战数据结构和算法面试题——左旋转字符串

挑战数据结构和算法面试题——左旋转字符串

作者头像
felixzhao
发布2018-03-15 13:43:23
5900
发布2018-03-15 13:43:23
举报
文章被收录于专栏:null的专栏null的专栏

题目来源“数据结构与算法面试题80道”。在此给出我的解法,如你有更好的解法,欢迎留言。

这里写图片描述
这里写图片描述

问题分析:本题是常见的旋转字符串的问题,解决的方法是两步旋转的方法:

这里写图片描述
这里写图片描述

方法:

void do_reverse(char *p_start, char *p_end){
    if (NULL == p_start || NULL == p_end || p_start > p_end) return;
    char tmp;
    while(p_start < p_end){
        tmp = *p_start;
        *p_start = *p_end;
        *p_end = tmp;
        p_start ++;
        p_end --;
    }
}

void reverse(char *s, int len){
    if (NULL == s || len == 0) return;

    char *p1_start = s;
    char *p2_end = p1_start + strlen(s) - 1;

    char *p1_end = p1_start;
    int index = 1;
    while (index < len){
        index ++;
        p1_end ++;
    }
    char *p2_start = p1_end + 1;
    do_reverse(p1_start, p1_end);
    do_reverse(p2_start, p2_end);
    do_reverse(p1_start, p2_end);
}      
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年03月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档