《Cracking the Coding Interview》——第1章:数组和字符串——题目8
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- 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 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview 第一章
第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(一)
1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假设不允许使用额外的数据结构,又该如何处理? 1.2 用C或C++实现void reverse(char* str)函数, ...
- C语言 第七章 数组与字符串
一.数组 1.1.数组的概念 用来存储一组相同类型数据的数据结构.有点像班上放手机的手机袋,超市的储物柜. 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. ...
随机推荐
- VMware下,windows7无法自动更新,故障80072EE2
手动安装更新 1) KB3020369 2) KB3172605
- Sublime插件支持Sass编译和Babel解析ES6 & .sublime-build文件初探(转载自imwtr)
原文请看:http://www.cnblogs.com/imwtr/p/6010550.html 用Sublime Text蛮久了,配置配来配去的,每次换电脑都得重头再配过,奈何人老了脑子不中用了 ...
- Ubuntu 系统安装
1.首先下载一个Ubuntu系统文件,以.ios后缀结尾的系统镜像文件压缩包. 2,下载一个ultraiso软件,用于制作u盘启动盘 3,将电脑重启,进入BOIS界面,调整系统顺序, 将启动盘系统设置 ...
- javascript 时间格式化方法
对jquery进行扩展的方法: //对时间格式化(jquery方法扩展) Date.prototype.Format = function (fmt) { //author: meizz var o ...
- python3中使用HTMLTestRunner.py报ImportError: No module named 'StringIO'的解决办法
.原因是官网的是python2语法写的,看官手动把官网的HTMLTestRunner.py改成python3的语法: 参考:http://bbs.chinaunix.net/thread-415474 ...
- memcache 基本操作
输入 telnet localhost 11211 步骤: 1.输入 set hans 0 0 3 回车 2. 输入 123 回车 3. get hans 回车 删除操作,输入 delete h ...
- hbase的coprocessor使用(转)
http://www.360doc.com/content/13/0320/09/4675893_272623864.shtml
- git fetch和push的区别
获取fetch的用法 git-fetch用于从另一个reposoitory下载objects和refs. 命令格式为:git fetch … 其中表示远端的仓库路径.git remote add or ...
- 1、SpringBoot------表单校验
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/Springboot/tree/1ef5e597a6f866e73387c0238dbcdf46cf ...
- django+xadmin在线教育平台(十四)
7-1 django templates模板继承1 机构可以筛选类别 机构可以根据所在地区进行分类 右侧我要学习功能: form表单提交 右下:授课机构排名 页面头部与底部为全局头和全局底部. Dja ...