crack the code interview chapter 1

#crack the code interview handwrite code
#chapter 1 arrays and strings

#1.1 implement an algorithm to determine if a string has all unique characters,
#requirement:no additional data structures

// C++,function
bool is_unique_characters(string str){
		bool character[256] = {false};
		for(int i=0;i<str.length();i++){
				if (character[str[i]] == true)
						return false;
				else
						character[str[i]] = true;
		}
		return true;
}
//python function

#1.2 write code to reverse a C-style String
#tips: C-style string ends with null character

// C/C++
char* reverse_string(char* str){
		int length = 0;
		while(*str != NULL)
			length++;
		int start = 0;
		while(start < length){
			swap(str[start],str[length]);
			start++;
			length--;
		}
		return str;
}

#1.3 To remove the duplicate characters in a string
#requirement: no additional buffer allowed,but one or two additional variables are fine

// C/C++
string remove_duplicate_characters(string str){
		bool exist[256];
		int duplicate_nums = 0;
		int len = str.length();
		for(int i = 0;i<len;i++){
				if(exist[str[i]] == false)
						exist[str[i]] = true;
				else{
						duplicate_nums++;
						for(int j= i;j<len-duplicate_num-2;j++)
								str[j] = str[j+1];
				}
		}
}

#1.4 write an algorithm to decide if two strings are anagrams or not
#tips:the anagrams means having the same nums of the same character

//C/C++
bool is_anagrams(string str1,string str2){
		int character_str1[256],character_str2[256];
		memset(character_str1,0,sizeof(character_str1));
		memset(character_str2,0,sizeof(character_str2));
		int length1 = str1.length(),length2 = str2.length();
		for(int i=0;i<length1;i++)
				character[str1[i]]++;
		for(int i=0;i<length2;i++)
				character[str2[i]]++;
		for(int i=0;i<256;i++)
				if(character_str1[i] != character_str2)
						return false;		
		return true;
}

#1.5 write a method to replace all spaces in string with '%20'
#tips: i think the '%20' should be "%20", first count the number of the space, then get a new array
# donot return the local pointer, it will cause illegal access

//C/C++
char * newstr;
void replace_space(string str){
	int count = 0;
	for(int i=0;i<str.length();i++)
			if(str[i] == ' ')
				count++;
	new_length = str.length() + 2*count + 1;
	newstr = (char *)malloc(new_length);
	for(int i=0;i<new_length;i++){
		if(str[i] == ' '){
			newstr[i] = '%';
			newstr[++i] = '2';
			newstr[++i] = '0';
		}
		else{
			newstr[i] = str[i];
		}
	}	
}


#1.6 write a method to rotate a N*N matrix by 90 degrees
#tips: (i,j)---(j,m-i)













#1.7 write an algorithm  if an element in an M*N matrix is 0,its entire row and column is set to 0
#tips:first scan the matrix to record the 0's position


// C/C++
void set_zero(char ** matrix,int m,int n){
			bool is_zero[m][n] = {false};
			for(int i=0;i<m;i++)
				for(int j=0;j<n;j++)
					if(matrix[i][j] == 0)
							is_zero[i][j] = 0;
							
			for(int i=0;i<m;i++)
				for(int j=0;j<n;j++)
					if(is_zero){
						for(int p=0;p<m;p++)
								matrix[p][j] = 0;
						for(int q = 0;q<n;q++)
								matrix[i][q] = 0;
					}
}

#1.8 write code to test if a string is a substring of the rotation of another string
#tips: rotation: "waterbottle" is a rotation of "erbottlewat"


//whether str2 is a substring of str1, the rotation in this question means counterclockwise rotation
//complexity O(2*length(str1) * length(str2))
//by using kmp algorithm, the complexity is O(2*length(str1))

bool is_substring(string str1, string str2){
			int length = 2* str1.length();
			char str[length];
			for(int i=0;i<length/2;i++){
					char[i] = str1[length/2-i-1];
					char[length -i -1] = str1[length/2-i-1];
			}
			for(int i=0;i<length;i++)
				for(int j=i;j<str2.length();j++){
						if(char[i] != str2[j])
								continue;
						if(j == str2.length() - 1)
							return true;			
				}	
			return false;	
}















1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值