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. JUnit之持续集成(CI,Continuous Integration)

    序,测试驱动开发告诉我们,要尽早测试,经常测试.如果我们进行一点小改动时,都把所有的单元测试.集成测试和功能测试执行一遍,这就会非常浪费时间.为了避免这一点,在开发期间我们只执行单元测试,那么集成测试 ...

  2. [Linux] Chang DNS Setting on Linux

    主机的虚拟机使用 NAT 模式时, NAT的DNS不好用.于是需要将虚拟机的DNS改成和主机的一样,这样虚拟机也可以请求互联网资源. Linux的DNS 服务器定义在 /etc/resolv.conf

  3. import()函数

  4. Hdu.1325.Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  5. 关系型数据库ACID

    关系型数据库ACID 一.事务 定义:所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位. 准备工作:为了说明事务的ACID原理,我们使用银行账户及资金管理的案例 ...

  6. 教你如何删除tomcat服务器的stdout.log文件

    用Tomcat做WEB服务器的人都知道,有个很让人头痛的问题,就是stdout.log日志文件会自动增长,而且增长得很快. 先来看看我的痛处吧,公司有个WEB应用,就是用Tomcat作为服务器的,由于 ...

  7. C 文件读写 容易疏忽的一个问题

    今天需要解决一个问题,将影像瓦片(一堆jpg文件)分别进行读取,并将所有数据以文件流的方式存入一个.db的文件中, 同时将每个jpg数据在db文件中的位置保存下来,作为index存在.idx文件中. ...

  8. CDN技术发展趋势

    智能化 存储智能化 关键技术点:文件分片热度的判断策略,跨节点快速文件调度 存储向以文件切片为基础的全网共享存储发展:把一个整片切成n个小块,按用户访问热度分散存储在不同节点.当用户访问的时候,由边缘 ...

  9. mysql workbench建表时PK,NN,UQ,BIN,UN,ZF,AI

    1. [intrinsic column flags] (基本字段类型标识) - PK: primary key (column is part of a pk) 主键 - NN: not null ...

  10. 关于ext3,ext4,xfs和btrfs文件系统性能对比

    关于ext3,ext4,xfs和btrfs文件系统性能对比 应为原文:http://www.ilsistemista.net/index.php/linux-a-unix/6-linux-filesy ...