[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 ...
随机推荐
- 访问Nginx报错
今天新装Nginx,一切妥善后,访问虚拟机服务器的IP,结果发现响应超时 这是因为防火墙的80端口没有打开,在新装的Linux上搭服务器一般会遇到这个问题,重新开放80端口即可解决: (1)firew ...
- CSS: Position Introduction.
brief introduction: detailed introduction: ①absolute locate:http://www.runoob.com/try/try.php?filena ...
- oracle第四天笔记
游标 /* 序列: ORACLE使用来模拟ID自动增长的 */ create sequence seq_test4; create table test2( tid number primary ke ...
- jenkins系列_使用scp命令进行远程文件复制遇到的坑
转自:https://blog.csdn.net/kingboyworld/article/details/78905553 一.场景介绍 项目为微服务项目,使用jenkins进行统一部署.基本思路是 ...
- Linux find命令使用方法
Linux中find命令用来在指定目录下查找文件.通过组合不同参数可以在linux系统中快速查找需要的文件或目录. find命令语法 格式:find pathname -options [ -pr ...
- Hibernate 再接触 继承映射
用一张 每一个类一张表 建立外键 第一种 一张总表 Person package com.bjsxt.hibernate; import javax.persistence.Discriminator ...
- 逻辑回归 logit
[方法转]http://www.powerxing.com/logistic-regression-in-python/ http://blog.csdn.net/lipengcn/article/d ...
- 微信小程序--分享报错(thirdScriptError Cannot read property 'from' of undefined;at pages/index/index page onShareAppMessage function TypeError: Cannot read property 'from' of undefined)
分享功能: onShareAppMessage: function (res) { if (res.from === 'button') { // 来自页面内转发按钮 console.log(res. ...
- iview 表单验证
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- prompt
[prompt] prompt() 方法用于显示可提示用户进行输入的对话框. prompt(text,defaultText) 参数 描述 text 可选.要在对话框中显示的纯文本(而不是 HTML ...