Reverse Words in a String I & Reverse Words in a String II
Reverse Words in a String I
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
- 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.
public class Solution {
public String reverseWords(String s) {
if (s == null || s.length() < ) return s;
int i = ;
StringBuilder sb = new StringBuilder();
while (i < s.length()) {
StringBuilder temp = new StringBuilder();
while(i < s.length() && !(s.charAt(i) == ' ')) {
temp.insert(, s.charAt(i));
i++;
}
if (temp.length() != ) {
sb.append(temp.toString());
sb.append(' ');
}
while(i < s.length() && s.charAt(i) == ' ') {
i++;
}
}
if (sb.length() > ) {
sb.deleteCharAt(sb.length() - );
}
char[] arr = sb.toString().toCharArray();
reverse(arr); return new String(arr);
} public void reverse(char[] arr) {
int i = , j = arr.length - ;
while (i < j) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
}
Reverse Words in a String II
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.
public class Solution {
public String reverseWords(String s) {
char[] ca = s.toCharArray();
int start = ;
for (int i = ; i <= ca.length; i++) {
if (i == ca.length || ca[i] == ' ') {
reverse(ca, start, i - );
start = i + ;
}
}
return new String(ca);
} private void reverse(char[] ca, int i, int j) {
for (; i < j; i++, j--) {
char tmp = ca[i];
ca[i] = ca[j];
ca[j] = tmp;
}
}
}
Reverse Words in a String I & Reverse Words in a String II的更多相关文章
- 给String添加reverse方法
我们知道Array有个reverse方法,String则没有,但可以Array来实现,字符串有个split方法可以轻易的将String转换为Array. String.prototype.revers ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- Convert string to binary and binary to string in C#
String to binary method: public static string StringToBinary(string data) { StringBuilder sb = new S ...
- [转] 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧
这帖是用来回复高级语言虚拟机圈子里的一个问题,一道Java笔试题的. 本来因为见得太多已经吐槽无力,但这次实在忍不住了就又爆发了一把.写得太长干脆单独开了一帖. 顺带广告:对JVM感兴趣的同学们同志们 ...
- JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类
<pre name="code" class="java"></pre><pre name="code" cl ...
- Java中String直接赋字符串和new String的区别
解析Java中的String对象的数据类型 1. String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ...
- perl malformed JSON string, neither tag, array, object, number, string or atom, at character offset
[root@wx03 ~]# cat a17.pl use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' = ...
- String的内存模型,为什么String被设计成不可变的
String是Java中最常用的类,是不可变的(Immutable), 那么String是如何实现Immutable呢,String为什么要设计成不可变呢? 前言 关于String,收集一波基础,来源 ...
- Java string和各种格式互转 string转int int转string
Java string和各种格式互转 string转int int转string 简单收集记录下 其他类型转String String s = String.valueOf( value); // 其 ...
- Realm 处理List<String> 问题 Type parameter 'java.lang.String' is not within its bound; should implement 'io.realm.RealmModel
public class InitAppBean extends RealmObject { private String sapling; private String logistics; pri ...
随机推荐
- luogu4267 TamingtheHerd (dp)
设f[i][j]为让前i天发生j次暴动需要改变的最少的值 则f[i][j]=min{f[k][j-1]+(x[k+1]!=0)+(x[k+2]!=1)+...+(x[i]!=(i-k-1))} $O( ...
- Mysql distinct、group by
具体业务场景:根据某些字段组合去重得到所有字段结果. 遇到的error:sql_mode=only_full_group_by. 原因是mysql配置问题. distinct: distinct这个关 ...
- poj2386(简单的dfs/bfs)
题目链接:http://poj.org/problem?id=2386 Description Due to recent rains, water has pooled in various pla ...
- [NOI2015]寿司晚宴——状压dp
题目转化:将2~n的数分成两组,可以不选,使得这两组没有公共的质因子.求方案数. 选择了一个数,相当于选择了它的所有质因子. 30分: 发现,n<=30的时候,涉及到的质因子也就10个.2,3, ...
- 洛谷P4145 上帝造题的⑦minutes ②
又是线段树. 区间开平方求和,套路题. 如果开到了1就不用再开下去了,否则直接到底. 记得 l > r 时交换 l r #include <cstdio> #include < ...
- 硬盘读取速度变慢 — 当前传送模式: PIO模式
网上搜索了一下,找到两篇文章: 标题:硬盘读取速度变慢 当前传输模式pio的解决方法 http://www.veryhuo.com/a/view/52786.html (解决思路:先卸载驱动,重启 ...
- Linux网络基本网络配置
Linux网络基本网络配置方法介绍 网络信息查看 设置网络地址: cat /etc/sysconfig/network-scripts/ifcfg-eth0 你将会看到: DEVICE=eth0 BO ...
- 开启 Hyper-v 后如何使用 Android Emulator?
如果开启了 Hyper-v 时,当需要使用 Android Studio 中 Android Emulator 时,系统会出现蓝屏代码错误. 使用下面的方法,则可以解决冲突. 首先,你需要确保已经开启 ...
- noi.openjudge 2.6.162 Post Office
http://noi.openjudge.cn/ch0206/162/ 总时间限制: 1000ms 内存限制: 65536kB 描述 There is a straight highway wit ...
- git杂记:忽略ssl认证
当你通过HTTPS访问Git远程仓库,如果服务器的SSL证书未经过第三方机构签署,那么Git就会报错.这是十分合理的设计,毕竟未知的没有签署过的证书意味着很大安全风险.但是,如果你正好在架设G ...