• 南京三星面试准备1--字符串


    字符串

    1、把一串字符串里的数字找出来加起来

    long long int AddNumInChar(const char* str)
    {
    	assert(str!=NULL);
    	int length=strlen(str);
    	long long int result=0;
    	for(int i=0;i<length;i++)
    	{
    		if(str[i]>='0'&&str[i]<='9')
    		{
    			result+=str[i]-'0';
    		}
    	}
    	return result;
    }

    2、把字符串转换成整数

    void Char2Int(const char*str)
    {
    	assert(str!=NULL);
    	int length=strlen(str);
    	long long int result=0;
    	bool flag=false;
    	bool fuhao=true;
    
    	for(int i=0;i<length;i++)
    	{
    		if(i==0&&(str[0]=='+'||str[0]=='-'))
    		{
    			fuhao=str[0]=='-'?false:true;	
    		}
    		else if(str[i]>='0'&&str[i]<='9')
    		{
    			flag=true;
    			result=result*10+str[i]-'0';
    		}
    		else
    		{
    			printf("My God
    "); 
    			return;
    		}
    
    	}
    	if(!fuhao)
    		result=result*(-1);
    	printf("%lld
    ",result);
    
    }


    3、把字符串顺序倒过来


    char* reverseString(char* str)
    {
    	assert(str!=NULL);
    	int length=strlen(str);
    	for(int i=0;i<length/2;i++)
    	{
    		char temp=str[i];
    		str[i]=str[length-i-1];
    		str[length-i-1]=temp;
    	}
    	return str;
    }


    4、找字符串里的回数

    char *FindString(const char *str)
    {
    	assert(str!=NULL);
    	int length=strlen(str);
    	int *numbers=new int[length];
    	float *index=new float[length];
    	numbers[0]=numbers[length-1]=1;
    	index[0]=0;
    	index[length-1]=length-1;
    	int left,right;
    	for(int i=1;i<length-1;i++)
    	{
    		left=i-1;
    		right=i+1;
    		if(str[left]!=str[right]&&str[left]!=str[i]&&str[i]!=str[right])
    		{
    			numbers[i]=1;
    			index[i]=i;
    		}
    		else 
    		{
    			if(str[left]==str[right])
    			{
    				numbers[i]=3;
    				index[i]=i;
    			}
    			else if(str[left]==str[i])
    			{
    				numbers[i]=2;
    				index[i]=(left+i)/2.0;
    				right=i;
    			}
    			else if(str[i]==str[right])
    			{
    				numbers[i]=2;
    				index[i]=(right+i)/2.0;
    				left=i;
    			}
    			while(--left>=0&&++right<length)
    			{
    				if(str[left]==str[right])
    					numbers[i]+=2;
    				else
    					break;
    			}
    		}
    	}
    	int max=numbers[0];
    	int tempI=0;
    	for(int i=0;i<length;i++)
    	{
    		if(numbers[i]>max)
    		{
    			tempI=i;
    			max=numbers[i];
    		}
    	}
    	char *result=new char[max];
    	for(int i=0;i<max;i++)
    	{
    		int temp=floor(i+index[tempI]-max/2+0.5);
    		result[i]=str[temp];
    	}
    	delete []numbers;
    	return result;
    }

    5、替换空格为%20:

    char* ChangeString(char* myChar,int length)
    {
        char* result=new char[length*3];
     
        int count=0;
        for(int i=0;i<length;i++)
        {
            if(myChar[i]==' ')
            {
                result[count++]='%';
                result[count++]='2';
                result[count++]='0';
            }
            else
            {
                result[count++]=myChar[i];
            }
        }
        result[count]='';
        return result;
    }

    6、翻转单词顺序

    void Reverse(char* const numbers,int length)
    {
    	char temp;
    	for(int i=0;i<length/2;i++)
    	{
    		temp=numbers[i];
    		numbers[i]=numbers[length-i-1];
    		numbers[length-i-1]=temp;
    	}
    }
    
    void PrintReverse(char* const numbers,int length)
    {
    	int i=0;
    	Reverse( numbers,length);
    	int letterLen=0,begin=0;i=0;
    	bool flag=true;
    	while(numbers[i]!='')
    	{
    		if(numbers[i]!=' ')
    		{
    			flag=true;
    			letterLen++;
    		}
    		else if(flag)
    		{
    			if(letterLen>1)
    				Reverse(numbers+begin,letterLen);
    			letterLen=0;
    			begin=i+1;
    			flag=false;
    		}
    		else
    			begin++;
    		i++;
    	}
    	Reverse(numbers+begin,letterLen);
    	printf("%s
    ",numbers);
    }
    

    左旋转字符串
    void Reverse(char* const numbers,int length)
    {
    char temp;
    for(int i=0;i<length/2;i++)
    {
    temp=numbers[i];
    numbers[i]=numbers[length-i-1];
    numbers[length-i-1]=temp;
    }
    }
    void PrintMov(char* const numbers,int move)
    {
    int length=strlen(numbers);
    move=move%length;


    Reverse( numbers,length);


    Reverse(numbers,length-move);
    Reverse(numbers+length-move,move);
    printf("%s ",numbers);
    }

    7、第一次只出现一次的字符

    int FirstOneCharacter(const char* characters)
    {
    	int appearances[LetterNum]={0};
    	int result=-1;
    	if(characters==NULL)
    		return result;
    	int length=strlen(characters);
    	
    	for(int i=0;i<length;i++)
    	{
    		appearances[characters[i]-'A']++;
    	}
    	for(int i=0;i<length;i++)
    	{
    		if(appearances[characters[i]-'A']==1)
    		{
    			result=i;
    			break;
    		}
    	}
    	return result;
    }


    8、字符串的全排列:

    void Permutation(char* source,char *begin);
    void Permutation(char* source)
    {
    	int length=strlen(source);
    	if(source==NULL||length<=0)
    		return;
    	Permutation(source,source);
    
    }
    
    int *Sort(char *arrays)
    {
    	int length=strlen(arrays);
    	char temp;
    	int *index=new int[length];
    	int tempIndex;
    	for(int i=0;i<length;i++)
    	{
    		index[i]=i;
    	}
    	for(int i=0;i<length;i++)
    	{
    		for(int j=i;j<length;j++)
    		{
    			if(arrays[j]<arrays[i])
    			{
    				temp=arrays[j];
    				arrays[j]=arrays[i];
    				arrays[i]=temp;
    				tempIndex=index[j];
    				index[j]=index[i];
    				index[i]=tempIndex;
    			}
    		}
    	}
    	return index;
    }
    
    void Permutation(char* source,char *begin)
    {
    	if(begin[0]=='')
    		printf("%s
    ",source);
    	else
    	{
    		int length=strlen(begin);
    		char *temp=new char[length+1];
    		int i=0;
    		char word;
    		int *index;
    		temp=strcpy(temp,begin);
    		index=Sort(temp);
    		
    		for(;i<length;i++)
    		{
    			if(i>0&&temp[i]==temp[i-1])
    				continue;
    			swap(begin[0],begin[index[i]]);
    			Permutation(source,begin+1);
    			swap(begin[0],begin[index[i]]);
    		}
    		delete []temp;
    		delete []index;
    
    	}
    }
    


  • 相关阅读:
    [LeetCode] Remove Linked List Elements
    [LeetCode] Delete Node in a Linked List
    [LeetCode] Valid Anagram
    [LeetCode] Ugly Number II
    自制工具:迅速打开一个Node 环境的Playground
    [LeetCode] Ugly Number
    [LeetCode] Happy Number
    [LeetCode] Isomorphic Strings
    [LeetCode] Word Pattern
    自制工具:上传修改过的文件到指定服务器
  • 原文地址:https://www.cnblogs.com/james1207/p/3296988.html
Copyright © 2020-2023  润新知