【leetcode】Reverse Words in a String(hard)☆
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)☆的更多相关文章
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 【leetcode】Reverse Words in a String
今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...
- 【leetcode80】Reverse Vowels of a String(元音字母倒叙)
题目描述: 写一个函数,实现输入一个字符串,然后把其中的元音字母倒叙 注意 元音字母包含大小写,元音字母有五个a,e,i,o,u 原文描述: Write a function that takes a ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- 【字符串】Reverse Words in a String(两个栈)
题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is b ...
- 【LeetCode】Reverse digits of an integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...
- 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)
这是LeetCode里的第25道题. 题目要求: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最 ...
- 【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 ...
- 【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 ...
随机推荐
- 处理dataTable的行和列数据
DataTable dt = null; foreach (DataRow dr in dt.Rows) { ; j < dr.ItemArray.Length; j++) { tempColu ...
- 如何编写可维护的面向对象JavaScript代码
能够写出可维护的面向对象JavaScript代 码不仅可以节约金钱,还能让你很受欢迎.不信?有可能你自己或者其他什么人有一天会回来重用你的代码.如果能尽量让这个经历不那么痛苦,就可以节省不少时 间.地 ...
- Javascript高级程序设计——客户端检测
ECMAScript虽然是Javascript的核心,但是要在web中使用Javascript,那么BOM才是核心,BOM为我们提供了操作访问浏览器对象的借口, 但是由于BOM没有标准规范,导致存在不 ...
- Effective Java 学习笔记之创建和销毁对象
一.考虑用静态工厂方法代替构造器 1.此处的静态工厂方法是指返回指为类的对象的静态方法,而不是设计模式中的静态工厂方法. 2.静态工厂方法的优势有: a.使用不同的方法名称可显著地表明两个静态工厂方法 ...
- junk
var mydate1 = new Date($('.stt').html()); var mydate2 = new Date($('.ett').html()); if (window.Activ ...
- js 判断鼠标进去方向
function fx(id){ var obj= document.getElementById(id); var fun=function(e){ var w=obj.offsetWidth; v ...
- 第3次作业,c语言
<C语言程序设计>实验报告 学 号 160809201 姓 名 王浩然专业.班 计科16-2班 学 期 2016-2017 第1学期 指导教师 黄俊莲 ...
- 计蒜客 删除字母'c'
删除字母'c' 右侧的程序实现的功能是从字符串s中删除所有的小写字母c,请改正程序错误的地方. 注意:main函数不可以改动,程序结构也不能修改. 很简单的哦,加油吧- 样例输入 abccabcn 样 ...
- vSphere、Hyper-V与XenServer 你选哪个?
vSphere.Hyper-V与XenServer 你选哪个? 当在VMware vSphere.Microsoft Hyper-V与Citrix Systems XenServer之间做出选择时,v ...
- 2016年11月2日--Window.document对象
一.找到元素: docunment.getElementById("id"): 根据id找,最多找一个: var a =docunment ...