[leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word.
Example:
Input: "the sky is blue",
Output: "blue is sky the".
Note:
A word is defined as a sequence of non-space characters.
Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
You need to reduce multiple spaces between two words to a single space in the reversed string.
Follow up: For C programmers, try to solve it in-place in O(1) space.
思路
1. split by a single or multiple spaces, making sure remove leading or trailing spaces
2. to reverse, traverse the input from right to left, append each item into StringBuilder
代码
public class Solution {
public String reverseWords(String s) {
// corner case
if(s == null || s.length() == 0) return "";
// s.trim() 去除leading or trailing spaces
// s.split("\\s+")根据a single or multiple spaces 来split字符串
String[] words = s.trim().split("\\s+");
// use StringBuilder to save result
StringBuilder sb = new StringBuilder();
// to reverse, from right to left
for(int i = words.length-1; i >= 0; i--){
sb.append(words[i]+" ");
}
// 确保结果中去除了leading or trailing spaces
return sb.toString().trim();
}
}
[leetcode]151. Reverse Words in a String翻转给定字符串中的单词的更多相关文章
- 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] 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】151. Reverse Words in a String 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- 151 Reverse Words in a String 翻转字符串里的单词
给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...
- LeetCode 151 reverse word in a string
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- Java for 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 151. Reverse Words in a String --------- java
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
- Leetcode#151 Reverse Words in a String
原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...
随机推荐
- python流程控制for循环
流程控制 for循环 #首先我们用一例子看下用while循环取出列表中值的方法 l=['a','b','c'] i=0 while i<len(l): print(l[i]) i+=1 #whi ...
- 【373】LabelEncoder 相关
OneHotEncoder独热编码和 LabelEncoder标签编码 pandas.DataFrame.groupby
- [ SHELL编程 ] 自动删除操作系统用户
Linux中经常需要删除用户,通常手工操作执行userdel操作即可,如果删除失败出现错误提示按照提示错误进行操作即可.如果是脚本需要调用删除用户操作呢?利用如下实例中drop_user删除用户函数, ...
- 计算机网络协议包头赏析-IP
上次和大家聊了聊以太网的帧格式,本文会讲解IP数据报格式的定义. == 开门见山,先上图: 任何一个IP数据报都是由首部和数据两部分组成,而且首部基本是固定长度的,长度为20字节.这一点很重要,其他都 ...
- 如何使用JDBC连接Mysql数据库
//java类名BaseDaopublic class BaseDao { private Connection conn = null; // 声明Connection对象,Connectio ...
- ajax分页代码
<meta charset="utf-8"><?php//连接数据库$link = mysqli_connect('127.0.0.1','root','root ...
- [jQ]使用jQuery将多条数据插入模态框的方法
---------------------------------------------------------------------------------------------------- ...
- 二:Recovery models(恢复模式)
For each database that you create in SQL Server, with the exception of the system databases, you can ...
- 360sdk网游支付服务
网游支付服务 目录 1.流程介绍2.接口介绍2.1支付接口[客户端调用](必接)2.2支付结果通知接口–应用服务器提供接口, 由360服务器回调(必接)2.3订单核实接口– 服务器端接口, 应用服 ...
- C# 集合转换为DataTable
该类就用了几个类型,如int,int?,string,所以其它类型就先没管. 用到的类: public class tb_Projects { public int ProID { get; set; ...