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 ...
随机推荐
- List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user
当我在subsite里点击"Add a document",报这个错,后来一看event log: 在AAM里加上一条: 问题搞定:
- 青蛙跳台阶(C、Python)
C语言: /* ----------------------------------- 当n = 1, 只有1中跳法:当n = 2时,有两种跳法:当n = 3 时,有3种跳法:当n = 4时,有5种跳 ...
- git 子模块
为了方便各个子模块独立开发,或使用第三方不断更新的仓库,可以使用子模块来引用. 子模块对应的源码是子模块仓库的克隆. git submodule 与 subtree对比 git submodule 允 ...
- 支持向量机(SVM)的推导(线性SVM、软间隔SVM、Kernel Trick)
线性可分支持向量机 给定线性可分的训练数据集,通过间隔最大化或等价地求解相应的凸二次规划问题学习到的分离超平面为 \[w^{\ast }x+b^{\ast }=0\] 以及相应的决策函数 \[f\le ...
- 线程Thread类
进程:资源分配与调动的基本单位.如QQ.迅雷等每个独立运行的程序就是一个进程. 每一个进程可以有多个线程,如QQ可以收发信息.下载上传文件等. 多线程同时工作时,由CPU分配处理. public cl ...
- gdb调试5--工程项目的断点调试
之前说过断点调试,但是针对的是单文件的断点调试.在实际应用中,一个项目是多目录多文件的 参考资料:gdb debugger 目录结构: $ tree .├── gdbSomeFiles.cpp├── ...
- HSRP vs VRRP
HSRP:(Hot Standby Router Protocol)-热备份路由协议 是cisco平台一种特有的技术,是cisco的私有协议. VRRP:(Virtual Router Redunda ...
- java中各种时间公式
/** * 返回当前时间 * * @return 返回当前时间 */ public static Date getCurrentDateTime() { java.util.Calendar calN ...
- Jenkins插件安装实战篇
Jenkins插件安装实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 上篇博客我介绍了Jenkins是啥,以及持续集成,持续交付,持续部署的概念,那么问题来了:你知道CI和C ...
- strace常用参数详解
strace常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. strace命令大家应该比我熟悉吧,如果你不知道,呵呵,会可能跟我一样被人说:“我怀疑你是假运维”,不过没关 ...