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& ...
随机推荐
- find 以及linux 和windows 文件互传
1. find 命令 查找文件或目录 同时也会用到的有 which whereis locate 经常也会遇到一些快捷键 ctrl + l e a w u k ...
- Map 按Key排序 和 按Value排序
https://www.cnblogs.com/binz/p/6671917.html 一.根据value排序 通用方法 public class MapUtil { public static &l ...
- 远程登录多用户同时访问Win7系统远程桌面
https://jingyan.baidu.com/article/ca00d56c384ea0e99febcf45.html 一,远程电脑上新增三个用户名 1 在远程电脑桌面,右击[计算机]图标 ...
- 【传输协议】什么是CA证书
1.什么是CA证书. 看过一些博客,写的比较形象具体. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 B 公司的所有人都不认识他,他咋办捏?常 ...
- SpringCloud学习
1.SpringCloud的参考博客1 首先主要遇到的问题就是1.写好项目然后放到tomcat或者其他的容器中,然后稍微一点修改就要整个项目重新发布,非常麻烦,这就是微服务出现的契机了 基础知识 PS ...
- day21-22Redis Mahout
PS: Redis 在博客的 JavaEE PS:大数据实时执行3个特性,Storm,kafka,Redis PS:比如在系统中,1s中有大量的请求涌入的系统中,那么请求就存入数据库就挂了,这就需要到 ...
- 从 Godaddy 转移域名到 Namesilo
域名本来是在 Godaddy 上注册的,首付很便宜,但是续费时发现是个坑,续费一年是 102 元,再加上隐私保护 60元/年,总共一年需要 160 元,续费贵而且一点优惠也没. 对比下其他商家一年只要 ...
- Lua 程序设计 (Roberto,Ierusalimschy 著)
1 开始 2 类型与值 3 表达式 4 语句 5 函数 6 深入函数 7 迭代器与泛型for 8 编译,执行与错误 9 协同程序(coroutine) 10 完整的示例 11 数据结构 12 数据文件 ...
- JQury基础(一)样式篇
1 初识jQury 1.1 环境搭建 jQuery是一个JavaScript脚本库,不需要特别的安装,只需要我们在页面 标签内中,通过 script 标签引入 jQuery 库即可. <head ...
- IntelliJ IDEA使用心得
前言:我原来一直使用的是Eclipse,但是发现有的教程上使用的是Intellij这个IDE,而且我发现Eclipse在Web编程上特别是页面上的自动补全上确实有些不足,而且Intellij这个软件的 ...