2014-03-18 01:25

题目:给定一个字符串,判断其中是否有重复字母。

解法:对于可能有n种字符的字符集,用一个长度为n的数组统计每个字符的出现次数,大于1则表示有重复。

代码:

 // 1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure?
#include <cstdio>
#include <cstring>
using namespace std; class Solution {
public:
bool isAllUnique(const char *s) {
if (nullptr == s) {
return false;
}
int i; int a[];
memset(a, , * sizeof(int));
for (i = ; s[i]; ++i) {
if (a[s[i]]) {
return false;
} else {
a[s[i]] = ;
}
} return true;
};
}; int main()
{
Solution sol;
char s[]; while (scanf("%s", s) == ) {
if (sol.isAllUnique(s)) {
printf("Unique.\n");
} else {
printf("Not unique.\n");
}
} return ;
}

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

  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. 119. Pascal's Triangle II (Amazon) from leetcode

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  2. 如何在win10中安装ArcGIS10.2

    在win10中安装ArcGIS10.2,完美兼容,下面将自己在win10界面下的安装方法给大家分享一下. 工具/原料   win10环境 ArcGIS10.2安装包, 安装包地址链接: 链接: htt ...

  3. UESTC 757 棋盘

    虽然是水题,但是还是很interesting的.(大概就是我最晚出这个题了... 博弈感觉就是靠yy能力啊.这题是对称性. 最后的必败态是白色格子对称的,一旦对称形成,对手怎么选,跟随就好,对手无法摆 ...

  4. 【HDU4676】Sum Of Gcd(莫队+欧拉函数)

    点此看题面 大致题意: 多组询问,求\(\sum_{i=L}^R\sum_{j=i+1}^Rgcd(i,j)\). 推式子 这道题我们可以考虑,每个因数\(d\)被统计答案的次数,肯定与其出现次数有关 ...

  5. caffe RandomHue和RandomSaturation

    https://www.cnblogs.com/wangyblzu/p/5710715.html HSV和RGB一样是一种图像的颜色模型,h表示色调,s表示饱和度 1.RandomHue void R ...

  6. 精心收集的48个JavaScript代码片段,仅需30秒就可理解

    源文链接 :https://github.com/Chalarangelo/30-seconds-of-code#anagrams-of-string-with-duplicates 该项目来自于 G ...

  7. 使用 W3C Performance 对象通过 R 和 JavaScript 将浏览器内的性能数据可视化[转]

    当考虑 Web 性能指标时,需要关注的目标数字应该是从您自己的用户那里获得的实际用户指标.最常见的方法是利用 Splunk 之类的工具来分析您的机器数据,该工具支持您分析和可视化您的访问权限和错误日志 ...

  8. DB总结1

    DBA  重构 data  new york   committee   cobol codasyl  journal DDL  DML    关系演算  域关系演算语言(QBE)  元祖关系演算语言 ...

  9. 1801: [Ahoi2009]chess 中国象棋

    Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2520  Solved: 1524[Submit][Status][Discuss] Descripti ...

  10. BZOJ1061: [Noi2008]志愿者招募(线性规划)

    Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 5725  Solved: 3437[Submit][Status][Discuss] Descript ...