zoukankan      html  css  js  c++  java
  • 001 unique string

    以后坚持每一个星期都写记到算法题,不论简单还是难,纯熟娱乐!

    描写叙述:

    实现一个算法来推断一个字符串中的字符是否唯一(即没有反复).不能使用额外的数据结构。

    (即仅仅使用主要的数据结构)

    代码:

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <assert.h>
    
    bool isUnique(const std::string& vStr)
    {
    	if (vStr.size() == 0) return true;
    	bool Hash[128];
    	std::fill(Hash, Hash+128, false);
    
    	for (unsigned int i=0; i<vStr.size(); ++i)
    	{
    		int Temp = vStr[i];
    		if (Hash[Temp]) return false;
    		Hash[Temp] = true;
    	}
    
    	return true;
    }
    
    bool isUnique2(const std::string& vStr)
    {
    	if (vStr.size() == 0) return true;
    	int Bucket[4] = {0, 0, 0, 0};
    
    	for (unsigned int i=0; i<vStr.size(); ++i)
    	{
    		int Temp = vStr[i];
    		int Num  = Temp/32;
    		int Mod  = Temp%32;
    
    		if (Bucket[Num] & (1<<Mod)) return false;
    		Bucket[Num] |= (1<<Mod);
    	}
    
    	return true;
    }
    
    
    int main()
    {
    	std::string Test = "128&";
    	std::cout << isUnique2(Test) << std::endl;
    	_ASSERT(isUnique(Test) && isUnique2(Test));
    
    	system("pause");
    	return 0;
    }

    參考:http://hawstein.com/posts/1.1.html


  • 相关阅读:
    Python内建GUI模块Tkinter(二)
    Python内建GUI模块Tkinter(一)
    验证码处理
    Python小练习
    Python中csv模块解析
    Python中xlutils解析
    Python中xlwt解析
    Python中pandas模块解析
    Python中matplotlib模块解析
    一款软件同时管理MySQL,MongoDB数据库
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7323397.html
Copyright © 2011-2022 走看看