Given an input string, reverse the string word by word.

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

For C programmers: Try to solve it in-place in O(1) space.

Clarification:

    • What constitutes a word?
      A sequence of non-space characters constitutes a word.
    • Could the input string contain leading or trailing spaces?
      Yes. However, your reversed string should not contain leading or trailing spaces.
    • How about multiple spaces between two words?
      Reduce them to a single space in the reversed string.

思路:翻转的思路是很清楚的,就是卡在空格上了。结果专门先循环一遍来去掉空格。

注意,必须改变指针中所对应的值才能改变字符串。

void reverse(char * s, char * e)
{
while(s < e)
{
char tmp = *s;
*s = *e;
*e = tmp;
s++; e--;
}
} void reverseWords(char *s) {
//先专门处理空格
char * p = s;
char * snew = s;
while(*p != '\0')
{
if(*p != ' ') *snew++ = *p++;
else if(snew == s) p++; //开始处遇到空格
else if(*(snew - ) == ' ') p++; //已经有了一个空格
else *snew++ = *p++;
}
*snew = '\0';
if(*(snew - ) == ' ') *(snew - ) = '\0'; //翻转
char *ss = s;
int start = , end = ;
while(*ss != '\0')
{
while(*ss != ' ' && *ss != '\0')
{
ss++; end++;
}
reverse(s + start, s + end - );
if(*ss != '\0')
{
ss++; end++; start = end;
}
}
reverse(s, s + end - );
}

看看大神的:不用先去除空格,而是在遍历的过程中用end来更新字符串,去掉空格。

// reverses the part of an array and returns the input array for convenience
public char[] reverse(char[] arr, int i, int j) {
while (i < j) {
char tmp = arr[i];
arr[i++] = arr[j];
arr[j--] = tmp;
}
return arr;
} public String reverseWords(String s) {
// reverse the whole string and convert to char array
char[] str = reverse(s.toCharArray(), 0, s.length()-1);
int start = 0, end = 0; // start and end positions of a current word
for (int i = 0; i < str.length; i++) {
if (str[i] != ' ') { // if the current char is letter
str[end++] = str[i]; // just move this letter to the next free pos
} else if (i > 0 && str[i-1] != ' ') { // if the first space after word
reverse(str, start, end-1); // reverse the word
str[end++] = ' '; // and put the space after it
start = end; // move start position further for the next word
}
}
reverse(str, start, end-1); // reverse the tail word if it's there
// here's an ugly return just because we need to return Java's String
// also as there could be spaces at the end of original string
// we need to consider redundant space we have put there before
return new String(str, 0, end > 0 && str[end-1] == ' ' ? end-1 : end);
}

【leetcode】Reverse Words in a String(hard)☆的更多相关文章

  1. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

  2. 【leetcode】Reverse Words in a String

    今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...

  3. 【leetcode80】Reverse Vowels of a String(元音字母倒叙)

    题目描述: 写一个函数,实现输入一个字符串,然后把其中的元音字母倒叙 注意 元音字母包含大小写,元音字母有五个a,e,i,o,u 原文描述: Write a function that takes a ...

  4. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  5. 【字符串】Reverse Words in a String(两个栈)

    题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is b ...

  6. 【LeetCode】Reverse digits of an integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...

  7. 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)

    这是LeetCode里的第25道题. 题目要求: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最 ...

  8. 【leetcode】Reverse Nodes in k-Group

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...

  9. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

随机推荐

  1. CentOS系统操作mysql的常用命令

    MySQL名字的来历MySQL是一个小型关系型数据库管理系统,MySQL被广泛地应用在Internet上的中小型网站中.由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了 ...

  2. OC第五节 ——点语法和@property

    一.setter和getter函数     1.回忆:如何访问对象中的成员变量    2.setter和getter函数的作用            setter  方法:   修改对象的字段/实例变 ...

  3. Python命令 (if __name__=="__main__":)

    1. 语法 1.以#号开头的语句是注释 2.请务必注意,Python程序是大小写敏感的,如果写错了大小写,程序会报错. 3.按照约定俗成的管理,应该始终坚持使用4个空格的缩进. 4.当语句以冒号:结尾 ...

  4. An error I have completed recently

    在上学期开发javaweb的项目中,遇见一个字符串池的问题. 大致如下: 在上传一篇文章的时候,通过字符串的截取获取该篇文章的后缀名,如doc.pdf.txt....然后规定只能上传pdf和doc格式 ...

  5. shell简单使用

    最近需要用到shell脚本实现关机保护作用,总结下语法 要点: 1.linux下编写的shell脚本不能在window下编写,否则会出现^M的错误,用window编写保存,在linux用vim打开,每 ...

  6. js 模块化编程

    Javascript模块化编程(一):模块的写法   作者: 阮一峰 日期: 2012年10月26日 随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞 ...

  7. 15天学会jquery

    第二章 15 Days of jQuery 比window.onload 更快一些的载入 window.onload()是传统javascript 里一个能吃苦耐劳的家伙.它长久以来一直 被程序员们作 ...

  8. linux安装包地址备忘

    64位系统安装包: http://mirrors.163.com/centos/5/os/x86_64/CentOS/ 32位系统安装包: http://mirrors.163.com/centos/ ...

  9. OOP复习笔记

    /*OOP相关的代名词不做讲解*/ OOP的三大特征: 封装 - 继承 - 多态 -----------------------------------目录---------------------- ...

  10. 数据结构大二课程设计:QT实现线段树

    源码以及编译文件下载地址:http://download.csdn.net/detail/zhiyanpianyu1234/9445909#comment 加入了一些小东西,一直觉得课设是做给自己看的 ...