• 004串重量 (keep it up)


    设计算法并写出代码移除字符串中反复的字符,不能使用额外的缓存空间。

    注意: 能够使用额外的一个或两个变量,但不同意额外再开一个数组拷贝。

    简单题直接上代码:

    #include <stdio.h>
    #include <string.h>
    
    void remove_duplicate(char vStr[])
    {
    	int Len = strlen(vStr);
    	if (!Len) 
    	{
    		printf("the string is NULL
    ");
    		return ;
    	}
    	
        int Count = 0;
    	for (int i=0; i<Len; ++i)
    	{
    		if (vStr[i] != '')
    		{
    			vStr[Count++] = vStr[i];
    			for (int k=i+1; k<Len; ++k)
    			{
    				if (vStr[i] == vStr[k])
    				{
    					vStr[k] = '';
    				}
    			}
    		}
    	}
    	vStr[Count] = '';
    }
    
    void remove_duplicate2(char vStr[])
    {
    	int Len = strlen(vStr);
    	if (!Len) 
    	{
    		printf("the string is NULL
    ");
    		return ;
    	}
    
    	bool Visited[256];
    	memset(Visited, 0, sizeof(Visited));
    
    	int Count = 0;
    	for (int i=0; i<Len; ++i)
    	{
    		if (!Visited[vStr[i]])
    		{
    			vStr[Count++] = vStr[i];
    			Visited[vStr[i]] = true;
    		}
    	}
    	vStr[Count] = '';
    }
    
    void test()
    {
    	char Str[] = "13434343435568889hhhhhhhfdcvbb";
    	remove_duplicate(Str);
    	printf("%s
    ", Str);
    }

    keep it up  那些每周写几个算法,entertainment!

    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    Tutorial中代码的区别及不同效果
    Session
    代码解析&Filter用户授权例子
    web后台运作过程
    工厂纸杯生产流水线管理系统
    Webservice和EJB
    Week8——hashcode()和equals()方法
    Week7——JSON
    Week6——Lifecycle of JSF and Facelets
    Week5——Ajax
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4713152.html
Copyright © 2020-2023  润新知