[LC] 151. Reverse Words in a String
Given an input string, reverse the string word by word.
Example 1:
Input: "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: " hello world! "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces. Solution 1:
class Solution {
public String reverseWords(String s) {
if (s == null || s.length() == 0) {
return "";
}
char[] charArr = s.toCharArray();
swap(charArr, 0, s.length() - 1);
int i = 0, start = 0;
while (i < s.length()) {
if (i == 0 || charArr[i - 1] == ' ') {
start = i;
}
if (i == charArr.length - 1 || charArr[i + 1] == ' ') {
swap(charArr, start, i);
}
i += 1;
}
// need to trim space inside
int slow = 0;
for (int j = 0; j < charArr.length; j++) {
if (charArr[j] == ' ' && (j == 0 || charArr[j - 1] == ' ')) {
continue;
}
charArr[slow++] = charArr[j];
}
return new String(charArr, 0, slow).trim();
}
private void swap(char[] charArr, int i, int j) {
while (i < j) {
char tmp = charArr[i];
charArr[i] = charArr[j];
charArr[j] = tmp;
i += 1;
j -= 1;
}
}
}
Solution 2:
class Solution {
public String reverseWords(String s) {
if (s == null || s.length() == 0) {
return "";
}
String[] strArr = s.split("\\s+");
StringBuilder sb = new StringBuilder();
for (int i = strArr.length - 1; i >= 0; i--) {
sb.append(strArr[i] + " ");
}
return sb.toString().trim();
}
}
[LC] 151. Reverse Words in a String的更多相关文章
- 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 ...
- 151. Reverse Words in a String(java 注意细节处理)
题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...
- [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
Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
- (String)151. Reverse Words 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. For example,Given s = "the sky is blue& ...
随机推荐
- js事件函数中function(e){}
简单来说就是指向了当前发生的事件(click.mouseover等等),保存了当前事件的信息.如鼠标点击事件,有鼠标的坐标信息.其中,e是标准浏览器传递进去的事件参数,低版本IE不会传递,事件参数放置 ...
- Vue-router的介绍
1.路由基础介绍 (1)什么是前端路由: 路由是根据不同的URL地址展示不同的内容或页面. 前端路由就是把不同路由对应不同的内容或页面的任务交给前端来做.之前是通过服务端根据URL的不同返回不同的页面 ...
- 使用 prototype 定义方法和属性
除了可以在类的构造器方法中定义方法和属性外,也可以使用 prototype 定义方法和属性.每个类都有这个属性,该属性是一个静态属性,因此无需实例化,只需使用类引用该属性即可. 1.1 使用 prot ...
- CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)
题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...
- Windbg 大改版,值得期待
早上从twitter上面看到一篇文章,看到windbg会提供一个Time Travel Debugging(TTD) 功能,该功能会在未来的版本引入. Time travel debugging: I ...
- Vue-router(4)之路由跳转
路由传参 案例:现在需要展示一个电影列表页,点击每一部电影,会跳转到该部电影详情页(跳转时携带type和id) 代码实现(未携带type): index.js import Vue from 'vue ...
- python Mysql数据库连接池组件封装(转载)
以前一直在用Java来开发,数据库连接池等都是有组件封装好的,直接使用即可,最近在尝试Python的学习,碰到了和数据库打交道的问题,和数据库打交道我们都知道,数据库连接池必不可少,不然要么就是程序异 ...
- 分块&莫队模板
最裸的莫队:https://www.luogu.org/problemnew/show/P1494 #include<bits/stdc++.h> #define ll long long ...
- Anaconda Installation on Mac: conda command not found 环境变量配置
Mac系统安装完Anaconda 3.7后在terminal输入conda --version,返回command not found 原因可能是没有配置环境变量 在terminal输入vi ~/.b ...
- CertUtil: -hashfile 失败: 0xd00000bb (-805306181)
使用CertUtil验证Python安装文件的时候出现了这个错误. CertUtil: -hashfile 失败: 0xd00000bb (-805306181) 代码是这样 certutil -ha ...