LeetCode Reverse Words in a String III
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description
题目:
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
题解:
遇到空格就把之前标记的部分reverse.
Note: 最后一次reverse别忘了.
Time Complexity: O(n), n = s.length().
Space: O(n), char array.
AC Java:
class Solution {
public String reverseWords(String s) {
if(s == null || s.length() == 0){
return s;
} int len = s.length();
char [] charArr = s.toCharArray(); int lo = 0;
for(int i = 0; i<=len; i++){
if(i==len || charArr[i]==' '){
reverse(charArr, lo, i-1);
lo = i+1;
}
} return new String(charArr);
} private void reverse(char [] s, int i, int j){
while(i < j){
swap(s, i++, j--);
}
} private void swap(char [] s, int i, int j){
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
LeetCode Reverse Words in a String III的更多相关文章
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- [LeetCode] 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 ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- 53. leetcode557. Reverse Words in a String III
557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...
- ledecode Reverse Words in a String III
557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...
- 557. Reverse Words in a String III【easy】
557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...
随机推荐
- asp.net IRequiresSessionState
在一般处理程序中,使用context.Session对象,必须先继承IRequiresSessionState接口. System.Web.SessionState.IRequiresSessionS ...
- JS字符串数组转换
字符串转数组: str.split(';') 数组转字符串: arr.join(';')
- 【leetcode刷题笔记】Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- Linux串口编程(中断方式和select方式)
Linux下的串口编程,在嵌入式开发中占据着重要的地位,因为很多的嵌入式设备都是通过串口交换数据的.在没有操作系统的我们可以使用UART的中断来出来数据的接受和发送,而在Linux操作系统下,我们也可 ...
- Swift中的? ! as as? as!
?: 代表这是个可选类型(optional)的.如下,如果num有就为Int类型的,如果没有值那么就是nil. let num:Int? 当我对number进行显示赋值时那么number就是Int类型 ...
- INSPIRED启示录 读书笔记 - 第2章 产品管理与产品营销
两者不是一回事 1.产品经理的工作是从细节上定义开发团队开发什么产品 2.市场营销的职责是对外宣传产品 产品公司常常会陷入的三种误区 1.由市场营销人员定义产品:由产品营销经理或所谓的产品经理负责收集 ...
- java基础之常量与变量
概要:通过这段时间的工作,发现自己的基础还是很薄弱的,so,you know 常量 一种特殊的变量,程序运行过程中不能改变的值 语法格式:final 数据类型 常量名称 = 常量值 例子:fina i ...
- Python的return self和return一个新的对象区别
目的:设计一个有理数相加.如3/5 + 7/15 = 80/75 return self 输入: class Rational0: def __init__(self, num, den=1): se ...
- 如何开启和禁止Linux系统的ping功能
在日常的网络维护和使用过程中,ping命令是最为常用的一个检测命令,它所使用的是ICMP协议,但是为了保护主机,很多时候我们需要禁止ICMP协议,在这种情况下,终端再使用ping命令检测,服务器是不会 ...
- PAT1022. Digital Library (30)
两个坑. 一个是一直用的malloc不行了.因为malloc分配的是固定大小,之前做的题没遇到过是因为一般string都不长(malloc分配string为24个Byte),这次直接报段错误,呢们了半 ...