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++;
    		}
    	}
    }
    
  • 相关阅读:
    Activity之间的数据传递
    解析JSON
    使用HTTP协议访问网路
    WebView的初体验
    Alarm机制用于定时服务
    IntentService和Service执行子线程对比
    前台服务
    Android四大组件之服务
    异步消息处理机制,UI更新
    Git学习
  • 原文地址:https://www.cnblogs.com/liuweilinlin/p/3281739.html
Copyright © 2011-2022 走看看