【剑指offer】面试题58(1):翻转单词顺序
来源:互联网 发布:Tomcat中数据库配置 编辑:程序博客网 时间:2024/05/22 08:20
题目
牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。
同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。
例如,“student. a am I”。
后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。
Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
思路
先对整个句子翻转:
“student. a am I” -> “I ma a .tneduts”
再对每个单词进行逐个翻转(标点符号和普通字母一样处理):
“I ma a .tneduts” - > “I am a student.”
代码
public class _58_01_ReverseWordsInSentence { public String ReverseSentence(String str) { if(str == null || str.trim().length() <= 1) return str; char[] sentence = str.toCharArray(); // 翻转整个句子 reverse(sentence, 0, sentence.length - 1); int low = 0; int high = 0; // 翻转每个单词 while(low != sentence.length && high != sentence.length) { // 找到第一个非空格字符 while(low != sentence.length && sentence[low] == ' ') ++low; if(low == sentence.length) break; // 找到low后面的第一个空格字符 high = low + 1; while(high != sentence.length && sentence[high] != ' ') ++high; reverse(sentence, low, high - 1); low = high + 1; } return new String(sentence); } private void reverse(char[] str, int startIndex, int endIndex) { for(int low = startIndex, high = endIndex; low < high; ++low, --high) { char t = str[low]; str[low] = str[high]; str[high] = t; } }}
测试
public class _58_01_Test { public static void main(String[] args) { test1(); test2(); test3(); } private static void test1() { _58_01_ReverseWordsInSentence rwis = new _58_01_ReverseWordsInSentence(); String s = rwis.ReverseSentence("student. a am I"); MyTest.equal(s, "I am a student."); s = rwis.ReverseSentence("hello, peyton lin."); MyTest.equal(s, "lin. peyton hello,"); // 两个空格 s = rwis.ReverseSentence("hello "); MyTest.equal(s, " hello"); } private static void test2() { _58_01_ReverseWordsInSentence rwis = new _58_01_ReverseWordsInSentence(); String s = rwis.ReverseSentence("a"); MyTest.equal(s, "a"); s = rwis.ReverseSentence("abc"); MyTest.equal(s, "abc"); s = rwis.ReverseSentence(""); MyTest.equal(s, ""); } private static void test3() { _58_01_ReverseWordsInSentence rwis = new _58_01_ReverseWordsInSentence(); String s = rwis.ReverseSentence(null); System.out.println(s); }}
阅读全文
0 0
- 【剑指offer】面试题58(1):翻转单词顺序
- 《剑指Offer》面试题:翻转单词顺序
- 剑指Offer----面试题42(1):翻转单词顺序
- (剑指Offer)面试题45:翻转单词顺序列
- 剑指offer 面试题42 翻转单词顺序
- 【剑指】 面试题58 翻转单词顺序
- 剑指offer面试题42_1翻转单词顺序(java实现)
- 翻转单词顺序(面试题 42)
- 剑指offer面试题42翻转单词顺序VS左旋字符串
- [剑指offer][面试题42]翻转单词顺序 VS 左旋转字符串
- 【剑指offer】面试题42:单词翻转顺序&左右旋转字符串
- [剑指offer]面试题42:翻转单词顺序,左旋转字符串
- 剑指offer 面试题42 翻转单词顺序 | 左旋转字符串
- 剑指offer 面试题42—翻转单词顺序VS左旋转字符串
- 《剑指Offer》学习笔记--面试题42:翻转单词顺序VS坐旋转字符串
- 【剑指Offer学习】【面试题42:翻转单词顺序vs左旋转字符串】
- 剑指offer面试题 翻转单词顺序 VS 左旋转字符串
- 剑指offer-面试题42:翻转单词顺序VS左旋转字符串
- ACM集训日记-8月3日
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
- String的indexOf(),lastIndexOf(),substring(int x),substring(int x , int y)方法 StringBuffer
- redis面试题
- HALCON学习之select_shape的Feature详解
- 【剑指offer】面试题58(1):翻转单词顺序
- 将一个二维数组合并成一个一维数组
- CodeForces
- C++ 取得系统当前时间
- caffe层配置参数解释
- Servlet知识点回顾(不按顺序)
- LightOJ
- dubbo源码环境搭建
- 转载:http://blog.csdn.net/u010128736/ ORB-SLAM2详解(五)局部建图