[每天一题]判断字符串里否是都是独一无二的字符(不要用额外空间)
2011-09-18 04:30
295 查看
Implement an algorithm to determin if a string has all unique characters. What if you can not use additional data structures?
首先,先实现用额外空间的方法。
算法:把字符串数组都变成ASCII,并将这个数组全部设置为false。从头到尾循环一编这个string,把他们变成true,如果入到true就说明之前有一样的character,就变成false,返回
#include<iostream>
using namespace std;
#include<string>
bool isunique(string str);
void main()
{
cout<<"please input a string"<<endl;
string str;
cin>>str;
if(isunique(str)==true)
cout<<"it contains all the unique charaters";
else
cout<<"it contains some same charaters";
}
bool isunique(string str)
{
bool set[]={false};// set all character int the array are false
for(int i=0;i<str.length();i++)
{
int val=str.at(i);
if(set[val]==true) return false;
set[val]=true;
}
return true;//don't forget reture true here!
}
以上的算法:时间复杂度n,空间复杂度n.
现在不用额外空间来实现,我们可以用一个bit vector来判断,假设下面的输入都是小写字符,这样int型的bit vector就可以使用了!
#include<iostream>
using namespace std;
#include<string>
bool isunique(string str);
void main()
{
cout<<"please input a string"<<endl;
string str;
cin>>str;
if(isunique(str)==true)
cout<<"it contains all the unique charaters";
else
cout<<"it contains some same charaters";
}
bool isunique(string str)
{
int checker=0;
for(int i=0;i<str.length();i++)
{
int val=str.at(i)-'a';
if((checker&(1<<val))>0) return false;
checker |=(1<<val);
}
return true;
}
还有一种想法,就是先排序用去(nlogn)的时间(比如归并排序,堆排),然后线性的查找当前字符的相邻字符是否相同。值得注意的时候是一般排序也会用到空间!
首先,先实现用额外空间的方法。
算法:把字符串数组都变成ASCII,并将这个数组全部设置为false。从头到尾循环一编这个string,把他们变成true,如果入到true就说明之前有一样的character,就变成false,返回
#include<iostream>
using namespace std;
#include<string>
bool isunique(string str);
void main()
{
cout<<"please input a string"<<endl;
string str;
cin>>str;
if(isunique(str)==true)
cout<<"it contains all the unique charaters";
else
cout<<"it contains some same charaters";
}
bool isunique(string str)
{
bool set[]={false};// set all character int the array are false
for(int i=0;i<str.length();i++)
{
int val=str.at(i);
if(set[val]==true) return false;
set[val]=true;
}
return true;//don't forget reture true here!
}
以上的算法:时间复杂度n,空间复杂度n.
现在不用额外空间来实现,我们可以用一个bit vector来判断,假设下面的输入都是小写字符,这样int型的bit vector就可以使用了!
#include<iostream>
using namespace std;
#include<string>
bool isunique(string str);
void main()
{
cout<<"please input a string"<<endl;
string str;
cin>>str;
if(isunique(str)==true)
cout<<"it contains all the unique charaters";
else
cout<<"it contains some same charaters";
}
bool isunique(string str)
{
int checker=0;
for(int i=0;i<str.length();i++)
{
int val=str.at(i)-'a';
if((checker&(1<<val))>0) return false;
checker |=(1<<val);
}
return true;
}
还有一种想法,就是先排序用去(nlogn)的时间(比如归并排序,堆排),然后线性的查找当前字符的相邻字符是否相同。值得注意的时候是一般排序也会用到空间!
相关文章推荐
- [每天一题]删除字符串中的重复字符(不要使用额外空间)
- 判断字符串中有无重复字符,不使用额外的数据结构
- 实验9-2 //编程判断输入的一串字符是否为“回文”。所谓“回文”,是指顺读和倒读都一样的字符串。如“XYZYX”和“xyzzyx”都是“回文”。要求使用指针变量判断字符串是否是回文。
- 1.确定一个字符串每个字符都是独一无二的
- java 判断两个字符串是否由相同的字符组成 排序算法 空间换时间的方法
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间。
- 1.1 设计算法判断一个字符串中字符都是否唯一的。如果不能使用额外的数据结构呢?
- 小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。 你能帮帮小Q吗?
- 腾讯2017年实习生编程题目(第二题)----2.算法基础-字符移位 小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间。
- 判断一个字符串的所有字符是否都是唯一的(使用hash表方法)
- 把一个字符串的大写字母放到字符串的后面 ,各个字符的相对位置不变,不能申请额外的空间
- 设计算法并写出代码移除字符串中重复的字符,不能使用额外的缓存空间。注意: 可以使用额外的一个或两个变量,但不允许额外再开一个数组拷贝。
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间
- 字符移位 小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。 你能帮帮小Q吗? 输入描述: 输入数据有多组
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间
- [面试算法]把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间。