zoukankan      html  css  js  c++  java
  • 字符串之判断重复字符串

    1.去掉重复字符串时间复杂度为O(n)

    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char str[] = "bdsjiseftftftfyrzsesese";
    	int length = strlen(str); 
    	char *p = str;
    	char hashTable[256] = {0};
    	
    	for (int i  = 0;i <length;++i)
    	{
    		if (hashTable[str[i]]==0)
    		{
    			*p = str[i];
    			hashTable[*p] =1;
    			p++;
    		}
    
    	}
    	*p ='';
    	cout << str <<endl;
    	return 0;
    }
    

     2.第一次只出现一次的字符

    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char *str = "edsjiseftftftfyrzsesese";
    	
    	char *p = str;
    	char hashTable[256] = {0};
    	
    	while(*p)
    	{
    		hashTable[*p]++;
    		p++;
    	}
    	p = str;
    	while(*p)
    	{
    		if(hashTable[*p] == 1)
    		{
    			break;
    		}
    		p++;
    	}
    	cout << *p <<endl;
    	return 0;
    }
    

    3.删除出现次数最少的字符

    有三个关键字 次数 最少 删除

    需要遍历三次:

    void reverse(char *str)
    {
    	char hashtable[26] = {0};
    	char *p = str;
    	while (*p != '')
    	{
    		hashtable[*p -'a']++;
    		p++;
    	}
    	p = str;
    	int min = hashtable[*p -'a'];
    	while(*p != '')
    	{
    		if (hashtable[*p - 'a'] != 0)
    		{
    			if(hashtable[*p - 'a'] < min)
    			{
    				min = hashtable[*p -'a'];
    			}
    		}
    		p++;
    	}
    	p = str;
    	for (int i  =  0 ;i < strlen(str);i++)
    	{
    		if (hashtable[str[i] - 'a'] != min)
    		{
    			*p = str[i];
    			p++;
    		}
    	}
    }
    
  • 相关阅读:
    java面试题
    [gcc]: unknown (64bit) [FAIL]
    一种基于Spring的java程序常量管理思路
    if (! +"\v1")的解释
    jQuery validation plugin
    oscache缓存技术
    基于jQuery开发的javascript模板引擎jTemplates
    编写好的CSS代码的13个忠告
    对 HTTP 304 的理解
    Hibernate N+1 问题
  • 原文地址:https://www.cnblogs.com/liuweilinlin/p/3281739.html
Copyright © 2011-2022 走看看