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",
return "blue is sky the".
解题思路:
本题方法多多,最简单的方式直接按“ ” spilt即可,JAVA实现如下:
public String reverseWords(String s) {
if (s == null || s.length() == 0)
return s;
String[] str = s.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = str.length - 1; i >= 0; i--)
if (str[i].length() >= 1)
sb.append(str[i] + " ");
if (sb.length() > 1)
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
Java for LeetCode 151 Reverse Words in a String的更多相关文章
- [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& ...
- [leetcode]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 word 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
原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...
- [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [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 ...
- Java for LeetCode 092 Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...
- 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 ...
随机推荐
- Tomcat 404
原因:servlet没有配置正确 ,查看web.xml确认正确,以及自己的请求路径正确 在IE中提示"404"错误有以下三种情况 1.未部署Web应用 2.URL输入错误 排错方法 ...
- 运行 appium 自带实例报错:unresolved import:webdriver
python demo 中from appium import webdriver报错unresolved import:webdriver 之所以会报这样的error是因为没有装clientclie ...
- JS 瀑布流布局
瀑布流布局 HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...
- 锋利的jQuery-4--阻止事件冒泡和阻止默认行为
阻止事件冒泡: 如果嵌套元素分别有自己的click事件,当点击内层元素时外层元素的事件也会被触发. $("span").bind("click", functi ...
- TCP的那些事儿(上)
TCP的那些事儿(上) 原文链接:http://coolshell.cn/articles/11564.html TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面. ...
- lnmp常见问题
常见问题地址:http://lnmp.org/faq.html 如何修改安装LNMP时输入的域名? 修改方法:编辑/usr/local/nginx/conf/nginx.conf 查找安装时输入的域名 ...
- matlab 之cov 协方差
COV 1.cov(x) 如果x为向量,返回x的方差 计算方法为: S为方差. 2.cov(X) 如果X为矩阵,把矩阵X的行作为观察值,把列作为变量,返回X的协方差矩阵: diag(cov(X))是每 ...
- POJ 1191 棋盘分割
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11213 Accepted: 3951 Description 将一个 ...
- HDOJ 4731 Minimum palindrome
Minimum palindrome Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- PHP的SQL注入攻击的技术实现以及预防措施
最近在折腾 PHP + MYSQL 的编程.了解了一些 PHP SQL 注入攻击的知识,总结一下经验.在我看来,引发 SQL 注入攻击的主要原因,是因为以下两点原因: 1. php 配置文件 php. ...