zoukankan      html  css  js  c++  java
  • 186. Reverse Words in a String II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.

    The input string does not contain leading or trailing spaces and the words are always separated by a single space.

    For example,
    Given s = "the sky is blue",
    return "blue is sky the".

    Could you do it in-place without allocating extra space?

    可以先把每一个单词reverse,然后整个list reverse。

    public void ReverseWords(char[] s) {
           if(s.Length == 0) return ;
           int start = 0;
           int end = 0;
           int i = 0;
           while(end <= s.Length)
           {
               if(end ==s.Length ||  s[end] == ' ')
               {
                   ReverseWord(s,start,end-1);
                   start =  ++end;
               }
               else end++;
           }
           ReverseWord(s,0,s.Length-1);
        }
        
        public void ReverseWord(char[] s, int start, int end)
        {
             while(start<end)
             {
                 char temp = s[start];
                 s[start++] = s[end];
                 s[end--] = temp;
             }
        }
  • 相关阅读:
    非线性数据结构——树
    排序算法之插入排序
    web框架之environment处理
    web开发之http和wsgi
    python os模块和shutil模块
    python路径操作
    stringIO和bytesIO
    python文件操作
    设计模式
    设计模式
  • 原文地址:https://www.cnblogs.com/renyualbert/p/5871786.html
Copyright © 2011-2022 走看看