2014-03-18 02:12

题目:判断一个字符串是否由另一个字符串循环移位而成。

解法:首先长度必须相等。然后将第一个串连拼两次,判断第二个串是否在这个连接串中。

代码:

 // 1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (i.e., “waterbottle” is a rotation of “erbottlewat”).
#include <cstdio>
#include <cstring>
using namespace std; class Solution {
public:
bool isStringRotation(char *s1, char *s2) {
if (s1 == nullptr || s2 == nullptr) {
return false;
} int len1, len2; len1 = strlen(s1);
len2 = strlen(s2);
if (len1 != len2) {
return false;
} const int MAXLEN = ;
static char tmp[MAXLEN]; tmp[] = ;
strcat(tmp, s1);
strcat(tmp, s1);
return strstr(tmp, s2) != nullptr;
}
private:
bool isSubstring(char *haystack, char *needle) {
if (haystack == nullptr || needle == nullptr) {
return false;
} return strstr(haystack, needle) != nullptr;
}
}; int main()
{
char s1[];
char s2[];
Solution sol; while (scanf("%s%s", s1, s2) == ) {
printf("\"%s\" is ", s2);
if (!sol.isStringRotation(s1, s2)) {
printf("not ");
}
printf("a rotation of \"%s\".\n", s1);
} return ;
}

《Cracking the Coding Interview》——第1章:数组和字符串——题目8的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  7. Cracking the Coding Interview 第一章

    第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. Cracking the Coding Interview 150题(一)

    1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假设不允许使用额外的数据结构,又该如何处理? 1.2 用C或C++实现void reverse(char* str)函数, ...

  10. C语言 第七章 数组与字符串

    一.数组 1.1.数组的概念 用来存储一组相同类型数据的数据结构.有点像班上放手机的手机袋,超市的储物柜. 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. ...

随机推荐

  1. 笨办法学Python(三十)

    习题 30: Else 和 If 前一习题中你写了一些 “if 语句(if-statements)”,并且试图猜出它们是什么,以及实现的是什么功能.在你继续学习之前,我给你解释一下上一节的加分习题的答 ...

  2. 在windows bat脚本(batch)中延时

    编写bat脚本时,有事我们希望在指令和指令之间,加入延时.例如当一条指令执行后,windows需要一定时间来响应的情况. 以下是一种实现方法,通过ping 指令来实现,5表示ping5次,就是延时5秒 ...

  3. Selenium入门系列1 打开浏览器访问网页,退出浏览器

    对于功能自动化的理解就是用测试工具替代手工.手工怎么操作的,工具也如何操作. 手工测试:在前置条件下,执行一定的操作步骤>与预期结果对比 功能自动化:在前置条件下,识别对象 >操作对象&g ...

  4. windows 右健添加cmd快捷通道

    windows 右健添加cmd快捷 - Windows - geektown极客堂 - Powered by Discuz!. 把横线下面的文本copy保存到一个注册表文件中,比如cmd.reg,然后 ...

  5. [转载]Memcached缓存服务的简单安装

    1.Linux下的安装方法 下载:wget http://memcached.org/latest tar -zxvf memcached-1.x.x.tar.gz cd memcached-1.x. ...

  6. hiho 第135周 九宫

    题目链接:http://hihocoder.com/contest/hiho135/problem/1 由于是九宫格,全排列也就是9! (362880)种方式,我就直接暴力枚举排列好了. #inclu ...

  7. 木棒,POJ(1011)

    题目链接:http://poj.org/problem?id=1011 解题报告: #include <cstdio> #include <cstring> #include ...

  8. 【转】你是不是也被Android Private Libraries、Referenced Libraries、android Dependency搞晕了~~

    一.v4.v7.v13的作用和用法 1.Android Support V4, V7, V13是什么? 本质上就是三个java library. 2.为什么要有support库?   是为了解决软件的 ...

  9. JSON对象与XML相互转换工具类

    依赖jar <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId ...

  10. python selenium 模块的安装及使用

    安装 pip install selenium 或者到https://pypi.python.org/pypi/selenium 下载setup安装包,之后进入目录后运行python setup.py ...