ledecode Reverse Words in a String III
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.
package leedcode;
class Solution {
public static void main(String[] args) {
// String s = "Let's take LeetCode contest";
String s = "12345";
System.out.println(s);
System.out.println(new Solution().reverseWords(s));
}
public static void reverseWord(char[] chars, int begin, int end) {
for (int i = begin; i <= end; i++) {
char temp = chars[i];
chars[i] = chars[end];
chars[end] = temp;
end--;
}
}
public String reverseWords(String s) {
char[] chars = s.toCharArray();
int start = 0;
int end = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
end = i;
reverseWord(chars, start, end-1);
start = end+1;
}
end++;
}
reverseWord(chars,start,end-1);
return new String(chars);
}
}
重新整理逻辑
class Solution {
public static void reverseWord(char[] chars, int begin, int end) {
for (int i = begin; i <= end; i++) {
char temp = chars[i];
chars[i] = chars[end];
chars[end] = temp;
end--;
}
}
public String reverseWords(String s) {
char[] chars = s.toCharArray();
int start = 0;
int end = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
end = i;
reverseWord(chars, start, end-1);
start = end+1;
}
end++;
}
reverseWord(chars,start,end-1);
return new String(chars);
}
}
ledecode Reverse Words in a String III的更多相关文章
- 53. leetcode557. Reverse Words in a String III
557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 557. Reverse Words in a String III【easy】
557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...
- 【leetcode_easy】557. Reverse Words in a String III
problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...
- Week4 - 500.Keyboard Row & 557.Reverse Words in a String III
500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...
- [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 ...
- 557. Reverse Words in a String III 翻转句子中的每一个单词
[抄题]: Given a string, you need to reverse the order of characters in each word within a sentence whi ...
随机推荐
- scala,spark练习题提高
1.求每家公司有哪些产品 val arr3 = List("Apache" -> "Spark", "Apache" -> &q ...
- CheckBox和richTextBox
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { Initialize ...
- Idea上配置btm
1. 先在eclipse中配置好项目,再讲配置好的项目导入到idea中
- PHP——字符串
<?php //strlen("aaa");取字符串的长度 *** echo strlen("aaaaa"); echo "<br /&g ...
- 关于HTTP的长连接和短连接
1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议. IP协议主要解决网络路由和寻址 ...
- 服务器的svnserver修改密码
VisualSVN Server是一个集成的svn服务端工具,是一款svn服务端不可多得的好工具.可以先安装好VisualSVN Server后,运行VisualSVN Server Manger,然 ...
- jquery mobile小经验
现在网站上关于jquery mobile的demo和帖子可真少啊,我刚开始接触,遇到了一些问题,都找不到人请教. 这是我的个人经验总结,或多或少会对刚入门的童鞋有点帮助吧. 如果想一开始进入页面的时候 ...
- php 显示一个干净的,易被解析的json
header("Content-type: text/html; charset=utf-8"); //试着从数据库里读取一条数据放进来 $con = mysql_connect( ...
- MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得。
MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得. 之前的项目比较简单,多是用JSP .Servlet + JDBC 直接搞定,在项目中尝试用 Strut ...
- Thinkphp新增字段无法插入到数据库问题
Thinkphp框架开发过程中,因需求需要改动数据表,新增了几个字段. 调用 M(‘xxx’)->add($data) 插入值时,新增的字段数据总是插入不进去,每次都是默认的值,于是恍然—-缓存 ...