第三种方法为位运算的方法。

位运算符: << 左移  & 与 | 或

#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
#include <unordered_map>
using namespace std; //时间 O(n) 空间 O(1)
bool hasSame(string str)
{
if(str.size() == )
return false;
bool ans = false;
vector<bool> flag(,false);
for(int i = ; i < str.size(); i++)
{
if(flag[str[i]] == true)
return true;
flag[str[i]] = true;
}
return false;
}
//时间 O(n*n) 空间 O(1)
bool hasSame2(string str)
{
if(str.size() == )
return false;
for(int i = ; i < str.size(); i++)
for(int j = i+; j < str.size(); j++)
{
if(str[i] == str[j])
return true;
}
return false;
}
//时间 O(n) 空间 O(1)
bool hasSame3(string str)
{
if(str.size() == )
return false;
if(str.size() > )
return true; int flag = ;
for(int i = ; i < str.size(); i++)
{
int num = <<(str[i] - 'a');
if(flag & num)
return true;
flag = flag | num;
}
return false;
}
int main()
{
cout<< hasSame3("abc");
cout<< hasSame3("aa");
cout<< hasSame3("abac");
cout<< hasSame3("bcb");
cout<< hasSame3("");
cout<< hasSame3(" ");
cout<< hasSame3(" ");
}

Cracking-- 1.1 判断字符串中是否有重复字符的更多相关文章

  1. python 判断字符串中是否只有中文字符

    python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...

  2. 使用HashMap或Hashset优化使用循环判断字符串中是否含有重复元素

    原本遇到判断字符串中是否含有重复元素的问题总是使用for循环遍历进行判断,这一方法则需要O(n3)的时间复杂度,如果本身方法处于几个循环中,就会指数倍增加时间复杂度.类似于如下代码: String[] ...

  3. JS判断字符串中是否存在某个字符

    用String类中的indexOf函数,例如:String str="we find out sth";if(str.indexOf("o")==-1){ // ...

  4. JavaScript知识之判断字符串中出现最多的字符及次数

    var str = 'asdddasdfdseeeeeweeeeeeeeeeeee'; var json = {}; // 定义json一个对象 for(var i = 0; i < str.l ...

  5. jquery判断字符串中是否包含特定字符的方法总结

    方法一:使用indexOf() 和lastIndexOf()方法 案例: var Cts = "bblText"; if(Cts.indexOf("Text") ...

  6. java中判断字符串中是否有中文字符

    package com.meritit.test; public class TestChart { public static void main(String[] args) throws Exc ...

  7. PHP判断字符串中是否包含指定字符串,支持中文哦

    RT,随手写的 /** * 判断字符串中是否包含指定字符串 * @var source 源字符串 * @var target 要判断的是否包含的字符串 * @return bool */ functi ...

  8. 判断字符串中是否有SQL攻击代码

    判断一个输入框中是否有SQL攻击代码 public const string SQLSTR2 = @"exec|cast|convert|set|insert|select|delete|u ...

  9. [C#]判断字符串中是否包含中文

    关键代码: /// <summary> /// 判断字符串中是否包含中文 /// </summary> /// <param name="str"&g ...

随机推荐

  1. NSMutableAttributedString 的使用

    NSMutableAttributedString *msAttriStr = [[NSMutableAttributedString alloc] initWithString:[NSString ...

  2. 记一次项目中的css样式复用

    本文同步至微信公众号:http://mp.weixin.qq.com/s?__biz=MzAxMzgwNDU3Mg==&mid=401616238&idx=1&sn=3c6e9 ...

  3. eclipse:不能在tomcat里添加一个项目的解决方法

    Cannot add a project to a tomcat server in eclipse You didn't create your project as "Dynamic W ...

  4. json文本装换为JSONArray

    package com.beijxing.TestMain; import java.io.File; import java.io.IOException; import org.apache.co ...

  5. mac上执行sed的编辑 -i命令报错sed: 1: "test.txt": undefined label ‘est.txt’或sed: 1: "2a\test\": extra characters after \ at the end of a command

    问题一 sed编辑命令:[sed -i 's/a/b/g' test.txt]   报错:sed: 1: "test.txt": undefined label 'est.txt' ...

  6. 编码UTF-8

    ☯,首先,这并不是图片,这是一个unicode字符,Yin Yang,即阴阳符,码点为U+262F.如果你的浏览器无法显示,可以查看这个链接http://www.fileformat.info/inf ...

  7. (转)modelsim10.0C编译ISE14.7的xilinx库(xilinx ip核)

    原地址modelsim10.0C编译ISE14.7的xilinx库(xilinx ip核)   1.打开D:\Xilinx\14.7\ISE_DS\ISE\bin\nt64\compxlibgui.e ...

  8. ios第三方库和工具类

    下面的是使用苹果电脑后,自己的一下积累吧.有好用的第三方库和工具,肯定会第一时间和大家分享的. 自己平时写的一些分类和工具库 SSTools已经在github上面开始更新了,欢迎大家来指正和补充 一. ...

  9. Android程序crash处理

    Android程序crash处理 时间 2014-11-24 13:45:37  CSDN博客 原文  http://blog.csdn.net/allen315410/article/details ...

  10. maven建立本地仓库

    maven 建立本地仓库 博客分类: java,maven,nexus   前面我讲到为什么要使用 Maven, Maven 的安装,以及 如何与 IDE 集成等,前面的介绍可以认为是一个 Hello ...