一、题目一:翻转单词顺序

1.1 题目说明

题目一:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student.",则输出"student. a am I"。

1.2 解题思路

  第一步翻转句子中所有的字符。比如翻转"I am a student."中所有的字符得到".tneduts a ma I",此时不但翻转了句子中单词的顺序,连单词内的字符顺序也被翻转了。

  这个步骤我们大多数都很熟悉,看看下面这个Reverse方法,是不是非常熟悉?

    public static void Reverse(char[] array, int start, int end)
{
if (array == null || start < || end > array.Length - )
{
return;
} while (start < end)
{
char temp = array[start];
array[start] = array[end];
array[end] = temp; start++;
end--;
}
}

  第二步再翻转每个单词中字符的顺序,就得到了"student.a am I"。这正是符合题目要求的输出。因此,我们可以将上述思路实现为以下代码:

    public static string ReverseSentense(string sentense)
{
if (string.IsNullOrEmpty(sentense))
{
return null;
} char[] array = sentense.ToCharArray();
int start = ;
int end = array.Length - ; // Step1.先翻转整个句子
Reverse(array, start, end);
// Step2.再翻转句中的每个单词
start = end = ;
while (start < array.Length)
{
if (array[start] == ' ')
{
start++;
end++;
}
else if (end == array.Length || array[end] == ' ')
{
Reverse(array, start, --end);
start = end + ;
end++;
}
else
{
end++;
}
} return new string(array);
}

1.3 单元测试

  (1)测试用例

    // 功能测试,句子中有多个单词
[TestMethod]
public void ReverseTest1()
{
string input = "I am a student.";
string actual = ReverseWordsHelper.ReverseSentense(input);
string expected = "student. a am I"; Assert.AreEqual(actual, expected);
} // 功能测试,句子中只有一个单词
[TestMethod]
public void ReverseTest2()
{
string input = "Wonderful";
string actual = ReverseWordsHelper.ReverseSentense(input);
string expected = "Wonderful"; Assert.AreEqual(actual, expected);
} // 边界值测试,测试空字符串
[TestMethod]
public void ReverseTest3()
{
string input = "";
string actual = ReverseWordsHelper.ReverseSentense(input); Assert.AreEqual(actual, null);
} // 边界值测试,字符串中只有空格
[TestMethod]
public void ReverseTest4()
{
string input = " ";
string actual = ReverseWordsHelper.ReverseSentense(input);
string expected = " "; Assert.AreEqual(actual, expected);
} // 鲁棒性测试
[TestMethod]
public void ReverseTest5()
{
string actual = ReverseWordsHelper.ReverseSentense(null); Assert.AreEqual(actual, null);
}

  (2)测试结果

  ①测试用例通过情况

  ②代码覆盖率统计

二、题目二:左旋转字符串

2.1 题目说明

题目二:字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如输入字符串"abcdefg"和数字2,该函数将返回左旋转2位得到的结果"cdefgab"。

2.2 解题思路

  这两个问题是非常相似的,我们同样可以通过翻转字符串的办法来解决第二个问题。

  以"abcdefg"为例,我们可以把它分为两部分。由于想把它的前两个字符移到后面,我们就把前两个字符分到第一部分,把后面的所有字符都分到第二部分。我们先分别翻转这两部分,于是就得到"bagfedc"。接下来我们再翻转整个字符串,得到的"cdefgab"刚好就是把原始字符串左旋转2位的结果。

  通过分析可以发现,我们只需要调用三次Reverse方法就可以实现字符串的左旋转功能

    public static string LeftRotateString(string str, int num)
{
if (string.IsNullOrEmpty(str))
{
return null;
} int strLength = str.Length;
char[] array = str.ToCharArray(); if (strLength > && num > && num < strLength)
{
int firstStart = ;
int firstEnd = num - ;
int secondStart = num;
int secondEnd = strLength - ; // 翻转字符串的前面n个字符
Reverse(array, firstStart, firstEnd);
// 翻转字符串的后面部分
Reverse(array, secondStart, secondEnd);
// 翻转整个字符串
Reverse(array, , strLength - );
} return new string(array);
}

2.3 单元测试

  (1)测试用例

    // 功能测试
[TestCategory("LeftRotate")]
[TestMethod]
public void RotateTest1()
{
string input = "abcdefg";
string actual = ReverseWordsHelper.LeftRotateString(input, );
string expected = "cdefgab"; Assert.AreEqual(actual, expected);
} // 边界值测试
[TestCategory("LeftRotate")]
[TestMethod]
public void RotateTest2()
{
string input = "abcdefg";
string actual = ReverseWordsHelper.LeftRotateString(input, );
string expected = "bcdefga"; Assert.AreEqual(actual, expected);
} // 边界值测试
[TestCategory("LeftRotate")]
[TestMethod]
public void RotateTest3()
{
string input = "abcdefg";
string actual = ReverseWordsHelper.LeftRotateString(input, );
string expected = "gabcdef"; Assert.AreEqual(actual, expected);
} // 鲁棒性测试
[TestCategory("LeftRotate")]
[TestMethod]
public void RotateTest4()
{
string actual = ReverseWordsHelper.LeftRotateString(null, ); Assert.AreEqual(actual, null);
} // 鲁棒性测试
[TestCategory("LeftRotate")]
[TestMethod]
public void RotateTest5()
{
string input = "abcdefg";
string actual = ReverseWordsHelper.LeftRotateString(input, );
string expected = "abcdefg"; Assert.AreEqual(actual, expected);
} // 鲁棒性测试
[TestCategory("LeftRotate")]
[TestMethod]
public void RotateTest6()
{
string input = "abcdefg";
string actual = ReverseWordsHelper.LeftRotateString(input, );
string expected = "abcdefg"; Assert.AreEqual(actual, expected);
}

  (2)测试结果

  ①用例通过情况

  ②代码覆盖率

作者:周旭龙

出处:http://edisonchou.cnblogs.com

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

剑指Offer面试题:34.翻转单词顺序VS左旋转字符串的更多相关文章

  1. 剑指Offer - 九度1361 - 翻转单词顺序

    剑指Offer - 九度1361 - 翻转单词顺序2013-11-23 02:45 题目描述: JOBDU最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fi ...

  2. 【面试题042】翻转单词顺序VS左旋转字符串

    [面试题042]翻转单词顺序VS左旋转字符串 题目一:     输入一个英文句子,反转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标点符号和普通字母一样处理.     例如输入字符串“I a ...

  3. 《剑指offer》第五十八题(左旋转字符串)

    // 面试题58(二):左旋转字符串 // 题目:字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部. // 请定义一个函数实现字符串左旋转操作的功能.比如输入字符串"abcde ...

  4. 【剑指Offer面试编程题】题目1362:左旋转字符串--九度OJ

    题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=&qu ...

  5. 翻转单词顺序 VS 左旋转字符串

    全部内容来自<剑指offer>. 题目一: 输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标点符号和普通字符一样处理.例如输入字符串“I am a stude ...

  6. 【剑指offer】面试题42:翻转单词顺序 VS 左旋转字符串

    题目: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”abcX ...

  7. 面试题42:翻转单词顺序VS左旋转字符串

    题目一:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标点符号和普通字母一样处理. 例如输入字符串“I am a student.",则输出"stud ...

  8. 翻转单词顺序VS左旋转字符串

    题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理.例如输入“I am a student.”,则输出“student ...

  9. 剑指offer——翻转单词顺序VS左旋转字符串

    字符串的交换等,注意判断字符串的是否为NULL,以及判断边界等. #include <iostream> #include <string> using namespace s ...

随机推荐

  1. Ext.js的store里放model,还是field?

    按别人的经验, 一般来说,如果通用性强的应用,STORE里存放MODEL,便于重用代码. 如果通用性较弱的(报告,图表),则考虑使用field进行定制.

  2. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  3. PyQt5+Python3.5.2-32bit开发环境搭建

      1.基本环境. Window 8.1 64bit Python3.5.2-32bit.exe PyQt5 2.安装python. 去官网下载32位版本的python3.5.2(就是x86那个) 备 ...

  4. HEAP CORRUPTION DETECTED :after Normal block 错误

    http://blog.csdn.net/zhccl/article/details/7889590

  5. eclipse启动不了,出现“Java was started but returned exit code=13......”对话框

    eclipse启动不了,出现"Java was started but returned exit code=13......"对话框如下 解决方案:1.使用的是java jdk6 ...

  6. 用 IIS 实现请求转发

    最近部门要开发一个简单的APP,部分数据是现有项目已经存在的,为了方便维护,希望只提供一个交互的入口,并且协议的规则不变. 基于这个需求,有两套解决方案: 1.用代码将现有的api封装一层,对请求数据 ...

  7. C语言中使用系统自带的快排函数

    题目 . 德才论 () 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取 ...

  8. 第一天ci框架开发商城1

    ci框架开发商城1 1/28/2016 9:43:52 PM userguide删除 system application controllers 控制器 models 模型 views 视图 模板 ...

  9. DXUT源码阅读笔记

    14.GetCapture() 函数功能:该函数取得捕获了鼠标的窗口(如果存在)的句柄.在同一时刻,只有一个窗口能捕获鼠标:此时,该窗口接收鼠标的输入,无论光标是否在其范围内.函数原型:HWND Ge ...

  10. ITree诞生啦!

    经过一个月的码码码,一个面向OIer的ITree终于来辣! ... (似乎把OI遗弃在了某个角落了........... 一个月里,从只会py到写出ITree,真是不容易呢(其实就是两个多礼拜而已= ...