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 ...
随机推荐
- python操作word(改课文格式)【最终版】
python操作word的一些方法,前面写了一些感悟,有点跑题,改了下题目,方便能搜索到.心急的可以直接拉到最后看代码,我都加了比较详细的注释. 从8.3号早上9点,到8.8号下午5点半下班,终于把这 ...
- LinQ的简单使用
1.LinQ to Sql类(NET Language Integrated Query (LINQ) ) LINQ定义了大约40个查询操作符,如select.from.in.where以及order ...
- lua工具库penlight--09技术选择
模块化和粒度 在理想的世界,一个程序应该只加载它需要的库.Penlight需要额外100 Kb 的字节码来工作.它是简单但却乏味要加载你需要什么: local data = require 'pl.d ...
- 小结:trie
复杂度: 查找O(n),维护O(n),空间O(sum(len[i])) 概要: 就是每个节点对应一个字母,然后儿子有26个,查找和维护时进入对应儿子即可. 应用:在字符串匹配中多模匹配做基础结构:可以 ...
- linux下面bin,sbin不理解的查阅
在一下的文件中得到答案, 突然想想自己有点傻,自己有代码,为什么不自己查看一下代码呢 http://blog.csdn.net/ithomer/article/details/9839957
- 在其模块列表中有一个错误模块“ManagedPipelineHandler”。
C:\Windows\Microsoft.NET\Framework\v4.0.30319 命令行: aspnet_regiis -i
- PHP curl_setopt函数用法介绍中篇
此篇已实例为主. 一.一般的实例 demo1.php <?php $user = "admin123"; $pass = "admin456"; // $ ...
- js math atan2
在双十二活动中,视觉要求实现一个鼠标跟随运动的的效果,就像“觉”的那个效果类似 其实原理很简单,看鼠标从哪个方向进的及从哪个方向出的,然后区块里绝对定位的浮层就可以根据鼠标方向 运动; 如:在鼠标进入 ...
- Excel随机生成数据2
200万耗时大约 10秒以内,输出结果到txt文件. Sub GetPassword() 'by kagawa Dim i&, j&, k&, l&, m&, ...
- angular使用codemirror ui-codemirror在模态框或者tab中没有缩进,内容也会在点击之后才显示的问题
<textarea ui-codemirror="{ mode: 'javascript', lineNumbers: true, theme: 'solarized dark', l ...