字符串算法——反转字符串单词表(Reverse Words in a String)
来源:互联网 发布:xp编程器和vvdi哪个好 编辑:程序博客网 时间:2024/04/30 06:08
问题:
Given an input string, reverse the string word by word.
For example,
Given s = “the sky is blue”,
return “blue is sky the”.
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.
思路一:关于这类问题,要注意空格问题,是否要考虑字符串首尾空格符?是否只考虑单词间得一个空格还是多个空格?是否考虑其他字符,例如制表符等。
public class Solution { public String reverseWords(String s) { String res = ""; String[] words = s.trim().split("\\s+");//多个空格 for (int i = words.length - 1; i > 0; --i) { res += words[i] + " "; } return res + words[0]; }}// \\d表示 0-9 的数字,// \\s表示 空格,回车,换行等空白符,// \\w表示单词字符(数字字母下划线)// +号表示一个或多个的意思,所以.// //
阅读全文
0 0
- 字符串算法——反转字符串单词表(Reverse Words in a String)
- LeetCode | Reverse Words in a String(字符串中的单词序反转)
- LeetCode Reverse Words in a String(反转字符串)
- LeetCode Reverse Words in a String(反转字符串)
- Reverse Words in a String 字符串中单词反转
- 字符串反转 reverse-words-in-a-string @LeetCode
- [leetcode, python] Reverse Words in a String 反转字符串
- Reverse Words in a String 翻转字符串
- 151. Reverse Words in a String 字符串
- 【LeetCode-面试算法经典-Java实现】【151-Reverse Words in a String(反转字符串中的单词)】
- [LeetCode]557. Reverse Words in a String III(反转字符串 III)
- LeetCode 557 Reverse Words in a String III(反转字符串中的单词3)
- leetcode Reverse Words in a String III 反转字符串中的单词
- Leetcod:557.Reverse Words in a String III 反转字符串中的每个单词。
- 字符串-Reverse Words in a String(翻转字符串)
- leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)
- reverse a string反转字符串
- Leetcode【字符串】: Reverse Words in a String 要点分析
- Java多线程从0到1之线程创建(一)
- Git之如何上传代码到github上
- C++ 单例模式中懒汉式与饿汉式
- 19-20-预处理、编译、汇编、连接
- JavaScript 简介
- 字符串算法——反转字符串单词表(Reverse Words in a String)
- Leetcode 每日一道
- 【云栖大会】刚刚成立的达摩院 是马云眼中阿里巴巴留给世界最好的东西之一
- Android Studio Git 修改用户名、密码、URL
- linux多线程编程示例
- Oracle批量更新任务分解(数据量过亿)
- NDK开发基础①使用Android Studio编写NDK
- Spring+mybatis+PageHelper分页插件PageHelper的使用
- Android 组件之ViewFlipper