效果:

输入: "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翻转字符串中的单词的更多相关文章

  1. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  2. [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& ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  8. [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 ...

  9. [LintCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

随机推荐

  1. event.currentTarget和event.target的区别

    currentTarget始终是监听事件者,而target是事件的真正发出者.

  2. 51Nod 1084:矩阵取数问题 V2(多维DP)

    1084 矩阵取数问题 V2  基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励 ...

  3. python selenium自动化点击页面链接测试

    python selenium自动化点击页面链接测试 需求:现在有一个网站的页面,我希望用python自动化的测试点击这个页面上所有的在本窗口跳转,并且是本站内的链接,前往到链接页面之后在通过后退返回 ...

  4. LeetCode - Two Sum IV - Input is a BST

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  5. 尚硅谷【SpringBoot】web(源码讲解太多不建议阅读)

    四.Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可 ...

  6. HttpClient-传入url得到json字符串( PostMethod method = new PostMethod(url)是个好方法)

    import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ...

  7. deno学习三 官方提供的方便deno 安装方式

    早起deno 使用了golang 开发,同时需要protobuf 进行数据的序列化以及反序列化处理 当前的deno 已经使用rust 进行了开发,同时官方提供的安装方式也很方便了,不需要 那么复杂的编 ...

  8. redis使用问题总结

    1.redis使用过多内存导致其他进程无法正常运行情况:      解决方案:限制redis的最大使用内存,修改redis.conf中的maxmemory(一般不要超过空闲内存的3/5,如果不设置ma ...

  9. Unity3D\2D手机游戏开发 学习

    using UnityEngine; using System.Collections; [AddComponentMenu("Game/AutoDestroy")] public ...

  10. 自动化部署--shell脚本--2

    node1和node2都装apache   [root@linux-node1 ~]# yum install httpd -y Loaded plugins: fastestmirror Loadi ...