zoukankan      html  css  js  c++  java
  • 剑指offer--13题

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    void FirstNoRepeatCh(const char str[], char& ch);
    
    int main(int argc, char* argv[])
    {
    	const char* str = "cdceasafggsfrrtkkxx";
    	char ch = '@';
    	FirstNoRepeatCh(str,ch);
    	if(ch == '@')
    		cout<<"No Find!"<<endl;
    	else
    		cout<<ch<<endl;
    	return 0;
    }
    //自己编写,即第一种思路
    /*void FirstNoRepeatCh(const char str[], char& ch)
    {
    	int HashTable[256] = {0};
    	while(*str != '')
    	{
    		if(HashTable[*str] == 0)  //用Hash表(哈希表)来判断字符是否重复出现
    		{
    			char* pch = (char*)(str+1);
    			bool chFind = false;
    
    			while(*pch != '' && !chFind)
    			{
    				if(*str == *pch)
    					chFind = true;
    				pch++;
    			}
    			if(chFind)
    				HashTable[*str] = 1;
    			else
    			{
    				ch = *str;
    				break;
    			}
    		}
    		str++;
    	}
    }*/
    //此为第二种思路+自己编写
    void FirstNoRepeatCh(const char str[], char& ch)
    {
    	if(!str)
    		return;
    	const int TableSize = 256;
    	int HashTable[TableSize] = {0};
    	char* pch = (char*)str;
    	while(*str != '')
    	{
    		HashTable[*str]++;
    		str++;
    	}
    	bool chFind = false;
    	while(*pch != '' && !chFind)
    	{
    		if(HashTable[*pch] == 1)
    		{
    			ch = *pch;
    			chFind = true;
    		}
    		pch++;
    	}
    
    }
    
    /*//标准答案
    ///////////////////////////////////////////////////////////////////////
    // Find the first char which appears only once in a string
    // Input: pString - the string
    // Output: the first not repeating char if the string has, otherwise 0
    ///////////////////////////////////////////////////////////////////////
    char FirstNotRepeatingChar(char* pString)
    {
          // invalid input
          if(!pString)
                return 0;   //因为涉及指针,所以判空
    
          // get a hash table, and initialize it 
          const int tableSize = 256;  //最好用const int
          unsigned int hashTable[tableSize];
          for(unsigned int i = 0; i < tableSize; ++ i)
                hashTable[i] = 0;
    
          // get the how many times each char appears in the string
          char* pHashKey = pString;
          while(*(pHashKey) != '')
                hashTable[*(pHashKey++)] ++;
    
          // find the first char which appears only once in a string
          pHashKey = pString;
          while(*pHashKey != '')
          {
                if(hashTable[*pHashKey] == 1)
                      return *pHashKey;
    
                pHashKey++;
          }
    
          // if the string is empty 
          // or every char in the string appears at least twice
          return 0;
    } */
    
    //注意:根据评论可知,若第二次遍历Hash表,则会得到仅出现一次的最小字符
    

      

    清醒时做事,糊涂时读书,大怒时睡觉,独处时思考; 做一个幸福的人,读书,旅行,努力工作,关心身体和心情,成为最好的自己 -- 共勉
  • 相关阅读:
    微信小程序实战练习(仿五洲到家微信版)
    vue2.0项目 calendar.js(日历组件封装)
    基于thinkphp的后台管理系统模板快速搭建
    你不知道的javascript(上卷)读后感(二)
    你不知道的javascript(上卷)读后感(一)
    教你10分钟搭建酷炫的个人博客
    Webpack学习-工作原理(下)
    Webpack学习-工作原理(上)
    Css Secret 案例Demo全套
    基于excel导入数据到ms sql server
  • 原文地址:https://www.cnblogs.com/hello-yz/p/3189131.html
Copyright © 2011-2022 走看看