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 ...
随机推荐
- 【探秘ES6】系列专栏(一):ES6简介
摘要:新一代JavaScript标准,ES6即将发布.[探秘ES6]系列专栏将一一剖析ES6的诸多新特性,让Web开发者对此有清晰全面的了解.本文为系列的第一篇,带你了解ES6到底是什么以及有哪些令人 ...
- SQL Server 2008 R2导出数据脚本的方法
以前看到有些朋友说必须SQL Server 2008才能导出包含数据的脚本,后来仔细研究发现其实SQL Server 2008 R2也是可以的,只需在导出的时候在高级中设置一下即可. 1.首先在数据库 ...
- request.getAttribute() 和 request.getParameter() 有何区别?
HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别: (1)HttpServletRequest类有setAttri ...
- POJ3233 Matrix Power Series
Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. ...
- Opencv显示图片并监听鼠标坐标
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include & ...
- 人工神经网络(ANN)
参考资料:http://www.cnblogs.com/subconscious/p/5058741.html 从函数上来看,神经网络是回归方程的级联叠加,用来逼近目标函数的,本质是一种模拟特征与目标 ...
- DedeCMS 5.7 后门漏洞
简要描述: DedeCMS V5.7 SP1正式版 UTF-8 GBK版本疑似被植入一句话后门 前几日下载并不存在此代码 详细说明: shopcar.class.php被植入一句话@eval(file ...
- java开发微信公众平台备忘
简单记录下前段时间开发的电子书的 公众平台的一些备忘及开发心得经验等 eclipse的一些技巧: 1.ctrl+shift+o 自动添加必要import空间及移除无用import 项目备忘+说明 1. ...
- Python代码性能优化技巧
摘要:代码优化能够让程序运行更快,可以提高程序的执行效率等,对于一名软件开发人员来说,如何优化代码,从哪里入手进行优化?这些都是他们十分关心的问题.本文着重讲了如何优化Python代码,看完一定会让你 ...
- __set(),__get() 魔术方法示例
<?php class Ren{private $name;private $age;private $sex;function __set($name,$value){ if($name == ...