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& ...
随机推荐
- HDU 1716:排列2(全排列)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 动手动脑-Java的继承与多态
一. class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); ...
- 莫队算法 [国家集训队]小Z的袜子
题目链接 洛古 https://www.luogu.org/problemnew/show/P1494 大概说下自己的理解 先来概率的计算公式 ∑C(2,f(i)) / C(2,r−l ...
- plot 函数常用参数
- 《DSP using MATLAB》Problem 5.27
代码: %% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Outp ...
- log4net 自定义日志级别记录多个日志
程序中原来只记录一个日志,现在我要写一个用户操作日志,需要与原来的日志分开,在config文件中一阵折腾无果(要么写不全,要么写重了,反正没办法完美分离,要么与现存代码没办法完美兼容),差点放弃准备自 ...
- web.xml配置Servlet出错(DispatcherServlet)
想当然的把servlet-mapping当成servlet的属性 后台也不报错,但是输入url,就是死活不执行那个servlet servlet和servlet-mapping是同一级的对象
- 主机-配件-接口-整机-3c-2
pc机,服务器,智能手机与各种嵌入式乃至物联网 http://www.mifalife.net/hk/mall.html MIFA F5 户外无线蓝牙音箱2.0声道高保真可通话插卡便携低音炮迷你iph ...
- taro 事件处理
https://nervjs.github.io/taro/docs/event.html Taro 元素的事件处理和 DOM 元素的很相似.但是有一点语法上的不同: Taro 事件绑定属性的命名采用 ...
- python通过xlwt模块直接在网页上生成excel文件并下载
urls: from django.conf.urls import url, include from . import views urlpatterns = [ ... url(r'^domai ...