java翻转字符串中的单词
效果:
输入: "java and python"
输出: "avaj dna nohtyp"
代码:
版本1: 不考虑字符串开头有空格,单词间有多个空格空格的情况
public class StringReverse {
// 翻转一段字符串
public static void swapStr(char[] arr, int begin, int end) {
while (begin < end) {
char tmp = arr[begin];
arr[begin] = arr[end];
arr[end] = tmp;
begin++;
end--;
}
}
public static String swapWords(String s) {
if (s == null) {
return null;
}
String ret = "";
if (!s.endsWith(" ")) {
s += " ";
}
char[] charArr = s.toCharArray();
int begin = 0;
for (int i = 0; i < charArr.length; ++i) {
if (charArr[i] == ' ') {
swapStr(charArr, begin, i - 1);
begin = i + 1;
}
}
ret = new String(charArr);
return ret;
}
}
版本2:考虑开头的空格,单词间有多个空格
public static String swapWords(String s) {
if (s == null) {
return null;
}
String ret = "";
if (!s.endsWith(" ")) {
s += " ";
}
char[] charArr = s.toCharArray();
int begin = 0;
int i = 0;
while (i < charArr.length) {
while (charArr[i] == ' ' && i < charArr.length) {
i++;
}
begin = i; // 获取单词的第一个字母对应的位置
while (charArr[i] != ' ') { // 找到单词后第一个空格对应的位置
i++;
}
swapStr(charArr, begin, i - 1);
++i;
}
ret = new String(charArr);
return ret;
}
java翻转字符串中的单词的更多相关文章
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- [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 III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- [Swift]LeetCode186. 翻转字符串中的单词 II $ 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 ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
随机推荐
- Redis 当成数据库在使用和可靠的分布式锁,Redlock 真的可行么?
怎样做可靠的分布式锁,Redlock 真的可行么? https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html ...
- java中类加载时机
java虚拟机规范虽然没有强制性约束在什么时候开始类加载过程,但是对于类的初始化,虚拟机规范则严格规定了有且只有四种情况必须立即对类进行初始化,遇到new.getStatic.putStatic或in ...
- 编译Spring源码
近期,学习Spring源码.会陆续记录这次学习历程. Spring源码下载,环境中需要准备好的东西,git,gradle,eclipse (需要自己安装好) 1.Git clone Spring源码: ...
- python 字符串,列表,元组,字典相互转换
1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} 字典转为字符串,返回:<type 'str'> {'age': 7, 'n ...
- 【java编程-Javassist】秒懂Java动态编程(Javassist研究)
作者:ShuSheng007 来源:CSDN 原文:https://blog.csdn.net/ShuSheng0007/article/details/81269295 版权声明:本文为博主原创文章 ...
- Spring通知方法错误
错误提示,主要最后一句话 ,花了2个小时 org.springframework.beans.factory.BeanCreationException: Error creating bean ...
- 合并两个sorted ranges(merge和inplace_merge)
merge //版本一:用operator <比较元素 template <class InputerIterator1,class InputerIterator2,class Outp ...
- 20165308 《Java程序设计》第9周学习总结
20165308 <Java程序设计>第9周学习总结 教材学习内容总结 13章知识总结 获取地址 1.获取Internet上主机的地址 可以使用InetAddress类的静态方法getBy ...
- 图像识别的前期工作——使用pillow进行图像处理
pillow是个很好用的python图像处理库,可以到官方网站下载最新的文件.如果官网的任何PIL版本都不能与自己的python版本对应,或安装成功后发现运行出错,可以尝试从一个非官方的whl网站下载 ...
- mysql之 重建GTID下主从关系
主库:mysqldump -uroot -pmysql -S /tmp/mysql.sock1 --single-transaction --add-drop-database --master-da ...