《Cracking the Coding Interview》——第1章:数组和字符串——题目3
2014-03-18 01:32
题目:对于两个字符串,判断它们是否是Anagrams。
解法:统计俩单词字母构成是否相同即可。
代码:
// 1.3 Given two strings, write a method to decide if one is a permutation of the other.
// count them.
#include <cstdio>
#include <cstring>
using namespace std; class Solution {
public:
bool isPermutation(const char *s, const char *p) {
if (nullptr == s || nullptr == p) {
return false;
} size_t len = strlen(s);
if (len != strlen(p)) {
return false;
} int a[];
memset(a, , * sizeof(int)); size_t i;
for (i = ; i < len; ++i) {
++a[s[i]];
--a[p[i]];
}
for (i = ; i < ; ++i) {
if (a[i]) {
return false;
}
}
return true;
}
}; int main()
{
char s[], p[];
Solution sol; while (scanf("%s%s", s, p) == ) {
printf("\"%s\" is ", s);
if (!sol.isPermutation(s, p)) {
printf("not ");
}
printf("a permutation of \"%s\".\n", p);
} return ;
}
《Cracking the Coding Interview》——第1章:数组和字符串——题目3的更多相关文章
- 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型,数组里的数据成为元素. ...
随机推荐
- 框架: namespace和use的区别以及使用注意项
我们在使用框架的时候,总会使用到namespace和Use这两个东西,我们先来看它们存在的意义 namespace:是指我们当前类中,所在的位置.使用namespace关键字的话,我们就可以达到效果: ...
- C++ POD类型
POD( Plain Old Data)概念: Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to m ...
- ArcGIS License Server Administrator 10.2 无法启动许可的解决办法
刚刚重装了电脑,安装ArcGIS的时候,安装完desktop之后又安装了License Manager,结果把破解文件替换完之后,发现ArcGIS License Server Administrat ...
- gearmand安装过程
51 cd boost_1_53_0 52 tail -f build_log 53 dir 54 cd gearmand-1.1.8 55 ./configure 56 could not find ...
- P1151 子数整数
题目描述 对于一个五位数a_1a_2a_3a_4a_5a1a2a3a4a5,可将其拆分为三个子数: sub_1=a_1a_2a_3sub1=a1a2a3 sub_2=a_2a_3a_ ...
- Network in Network 笔记
传统CNN里的卷积核是一个generalized linear model(GLM)之后经过一个sigmoid(现在通常是ReLu)的非线性激励函数,假设卷积有K个filter,那么这K个filter ...
- 第9章 初识HAL固件库
本章参考资料:<STM32F76xxx参考手册>.<STM32F7xx规格书>.<Cortex-M3权威指南>, STM32 HAL库帮助文档:<STM32F ...
- 前端JavaScript之ECMA
1.JavaScript基础 2.语法规则 3 常用内置对象 4 函数 5 伪数组 6.异常处理 1.1 web前端分为三层 HTML:从语义的角度,描述页面结构 CSS:从审美的角度,描述样式(美化 ...
- using namespace std 是什么意思?
摘录CSDN上面大牛的回答简要意思就是使用标准库,想知道更清楚的继续读下面的. using namespace std 意思: using 和namespace都是C++的关键词. ...
- lintcode_115_不同的路径 II
不同的路径 II 描述 笔记 数据 评测 "不同的路径" 的跟进问题: 现在考虑网格中有障碍物,那样将会有多少条不同的路径? 网格中的障碍和空位置分别用 1 和 0 来表示. ...