zoukankan      html  css  js  c++  java
  • 翻转字符串中单词的顺序(腾讯面试)

    例如:输入“i come from tianjin”,输出“tianjin. from come i”

    step1:翻转所有字母的顺序,变成“.nijnait morf emoc i”

    step2:翻转单词,以空格为界,输出正确答案

    java代码:

    //最新的。翻转句子。通过测试。by myself
    //I am a student. --> student. a am I
    public class ReverseSentence{
      public static void main(String[] args){
            String s="zhangxueyuan am   student";
            System.out.println(s);
            System.out.println(reverseSentence(s));
        }
        public static void reverse(char c[], int start, int end){
            while(start<end){
                char temp = c[start];
                c[start] = c[end];
                c[end] = temp;
                start++;
                end--;
            }
        }
        public static char[] reverseSentence(String s){
            char c[]=s.toCharArray();
            reverse(c, 0, c.length-1);//c[c.length-1]!=''
            int start = 0;
            int end = 0;
            while(end<=c.length-1){
                if(c[start]==' '){
                    start++;
                    end=start;
                }else if(c[end]==' '){
                    reverse(c, start, end-1);
                    end++;
                    start=end;
                }else{
                    end++;
                }
            }
            //注意最后一个单词后面可能没有空格!所以需要下面一句来处理
            if(start!=end&&start<end-1){
                reverse(c, start, end-1);
            }
            return c;
        }
    }

     

    c代码:

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    void reverse(char *pBegin, char *pEnd)
        //翻转字符串
    {
        if(pBegin==NULL||pEnd==NULL)
        {
            return;
        }
        char temp;
        while(pBegin<pEnd)
        {
            temp=*pBegin;
            *pBegin=*pEnd;
            *pEnd=temp;
    
            pBegin++;
            pEnd--;
        }
    }//reverse
    
    char* reverseSentence(char *pData)
    {
        if(pData==NULL)
        {
            return NULL;
        }
        char *pBegin=pData;
        char *pEnd=pData;
    
        while((*pEnd)!='')
        {
            pEnd++;
        }
        pEnd--;
    
        reverse(pBegin,pEnd);//翻转所有
        pBegin=pEnd=pData;
        /*
        //翻转每个单词法一(书中)        
        while(*pBegin!='')
        {
            if(*pBegin==' ')
            {
                pBegin++;
                pEnd++;
            }
            else if(*pEnd==' '||*pEnd=='')
            {
                reverse(pBegin,--pEnd);
                pBegin=++pEnd;    
            }
            else
            {
                pEnd++;
            }
        }
        */
        //翻转每个单词法二(自己写的)
        while((*pEnd)!='')
        {
            if(*pEnd==' ')
            {
                pBegin=++pEnd;
            }
            else
            {
                while((*pEnd)!=' '&&(*pEnd)!='')
                {
                    pEnd++;
                }
                reverse(pBegin,pEnd-1);
            }
        }
    
        return pData;
    }//reverseSentence
    
    int main(){
        char s[]="i come from tianjin";
        cout<<s<<endl;
        char *p=reverseSentence(s);
        cout<<p<<endl;
    }
  • 相关阅读:
    设置zookeeper开机自启动
    安装zookeeper
    Elasticsearch 5.6.5 安装head插件
    [redis] Node is not empty. Either the node already knows other nodes
    【redis】 redis 创建集群时,Waiting for the cluster to join.... 一直等待
    [redis] redis.clients.jedis.exceptions.JedisDataException: MOVED 13102 127.0.0.1
    [linux] FastDFS访问文件,400 Bad Request
    [linux] Nginx编译安装错误error: the HTTP rewrite module requires the PCRE library
    [java] java解析txt文件
    【java】 java 解压tar.gz读取内容
  • 原文地址:https://www.cnblogs.com/seven7seven/p/3668847.html
Copyright © 2011-2022 走看看