《Cracking the Coding Interview程序员面试金典》----确定字符互异
来源:互联网 发布:网络剧哪里备案 编辑:程序博客网 时间:2024/06/06 19:38
时间限制:3秒 空间限制:32768K 热度指数:8610
算法知识视频讲解题目描述
请实现一个算法,确定一个字符串的所有字符是否全都不同。这里我们要求不允许使用额外的存储结构。
给定一个string iniString,请返回一个bool值,True代表所有字符全都不同,False代表存在相同的字符。保证字符串中的字符为ASCII字符。字符串的长度小于等于3000。
测试样例:
"aeiou"
返回:True
"BarackObama"
返回:False
解题思路:
ASCII中字符的个数也是有限的,有256个。因此这题可以用一个大小为256的数组,遍历字符串时把对应的个数记录在数组里,数组某一项数字超过1那么就表示有重复字符了。
#include<iostream>#include<iomanip>#include<cmath>#include<stdio.h>#include<string.h>using namespace std;bool checkDifferent(string iniString) {//ascii字符集只有256个字符if (iniString.size() > 256)return false;bool result[256] = { false };for (size_t i = 0; i < iniString.size(); ++i){int tmp = iniString[i];if (result[tmp])return false;result[tmp] = true;}return true;}int main(){double s;int i;string n, m;while (cin >> n){if (checkDifferent(n) == 1)cout << "Ture" << endl;if (checkDifferent(n) == 0)cout << "False" << endl;}return 0;}
如果范围缩写到a-z,26个字符,那么也可以用位操作来解决这个问题。对于26个字符,每个字符在它对应的ASCII码值对应的位置上设置一个标志,例如设置标志位为1,当字符不重复时1的位置都是错开的,所以位与的的结果必定为零,不为零说明之前出现过一个这样的字符。26个字符需要的数据类型至少有26位,而int型有32位,够用了。#include<iostream>#include<iomanip>#include<cmath>#include<stdio.h>#include<string.h>using namespace std;bool checkDifferent(string iniString) {if (iniString.size() > 256)return false;int result = 0;for (size_t i = 0; i < iniString.size(); ++i){int c = iniString[i] - 'a';int check = 1 << c;if ((result & check) != 0) //注意'&'的优先级比‘<','!='低,括号不能丢return false;result |= check;}return true;}int main(){double s;int i;string n, m;while (cin >> n){if (checkDifferent(n) == 1)cout << "Ture" << endl;if (checkDifferent(n) == 0)cout << "False" << endl;}return 0;}不懂的可以加我的QQ群:261035036(IT程序员面试宝典
群) 欢迎你的到来哦,看了博文给点脚印呗,谢谢啦~~
0 0
- 《Cracking the Coding Interview程序员面试金典》----确定字符互异
- 《Cracking the Coding Interview程序员面试金典》----确定两串乱序同构
- cracking the coding interview 中文版 (程序员面试金典)
- cracking the coding interview 中文版 (程序员面试金典)
- 《Cracking the Coding Interview程序员面试金典》----空格替换
- 《Cracking the Coding Interview程序员面试金典》----像素翻转
- 《Cracking the Coding Interview程序员面试金典》----清除行列
- 《Cracking the Coding Interview程序员面试金典》----实时中位数
- 《Cracking the Coding Interview程序员面试金典》----词频统计
- 《Cracking the Coding Interview程序员面试金典》----数字发音
- 《Cracking the Coding Interview程序员面试金典》-----
- cracking the coding interview 中文版 (程序员面试金典)
- 《Cracking the Coding Interview程序员面试金典》----原串翻转
- 《Cracking the Coding Interview程序员面试金典》----基本字符串压缩
- 《Cracking the Coding Interview程序员面试金典》----C++重载>>和<<(输入输出运算符)
- 《Cracking the Coding Interview程序员面试金典》----翻转子串
- 《Cracking the Coding Interview程序员面试金典》----链表中倒数第k个结点
- 《Cracking the Coding Interview程序员面试金典》----链表中倒数第k个结点
- 备份数据库
- Spring--IoC--Bean的装配--工厂方法设计模式
- 让JAVA的JDBC支持命名参数的SQL语句
- Java与Oracle中关于毫秒的处理
- 基于Deep Learning的跟踪算法总结(三)
- 《Cracking the Coding Interview程序员面试金典》----确定字符互异
- 设计模式之观察者模式
- oj203. Remove Linked List Elements
- 查找---分块查找
- Servlet验证码的创建应用
- imgeresizer
- 自定义对象的写入和读出函数
- Android注解简析
- cocos code ide选择Engie Mode时报错:Lua:不能找到D:\cocos2d-x-3.14.1\templates\lua-template-runtime\runtime\..