[Cracking the Coding Interview] Chapter 1 - Arrays and Strings

1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?

/* solution for question 1.1 */ bool solution_1(string str){ // initial bool table[256]; memset(table, false, sizeof(table)); // check for(int i = 0; i < str.length(); ++i){ if(table[str[i]]){ return false; } else{ table[str[i]] = true; } } return true; }


1.2 Write code to reverse a C-Style String. (C-String means that "abcd" is represented as five characters, including the null character.)

/* solution for question 1.2 */ void solution_2(char *str){ // null string if(NULL == str){ return; } // reverse int len = strlen(str); for(int i = 0; i < len/2; ++i){ char tmp = str[i]; str[i] = str[len-1-i]; str[len-1-i] = tmp; } }


1.3 Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is note.

/* solution for question 1.3 */ void solution_3(char *str){ // null string or a string with only letter if(NULL == str || strlen(str) <= 1){ return; } // initial bool table[256]; memset(table, false, sizeof(table)); // remove dunplicate int write_pos = 0, read_pos = 0; while(read_pos < strlen(str)){ if(!table[str[read_pos]]){ table[str[read_pos]] = true; str[write_pos++] = str[read_pos]; } read_pos++; } str[write_pos] = '\0'; }


1.4 Write a method to decide if two strings are anagrams or not.

/* solution for question 1.4 */ /* ignore the upper case and lower case */ bool solution_4(string str1, string str2){ // not anagrams if(str1.length() != str2.length()){ return false; } // check int prime[26] = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 87, 89, 97, 101}; int sum1 = 1, sum2 = 1; for(int i = 0; i < str1.length(); ++i){ // update sum1 if(str1[i] >= 'a' && str1[i] <= 'z'){ sum1 *= prime[str1[i] - 'a']; } else if(str1[i] >= 'A' && str1[i] <= 'Z'){ sum1 *= prime[str1[i] - 'A']; } // update sum2 if(str2[i] >= 'a' && str2[i] <= 'z'){ sum2 *= prime[str2[i] - 'a']; } else if(str2[i] >= 'A' && str2[i] <= 'Z'){ sum2 *= prime[str2[i] - 'A']; } } return sum1 == sum2; } /* solution for question 1.4 */ /* another solution for this question */ /* care about the upper case and lower case */ bool solution_4_1(string str1, string str2){ // not anagrams if(str1.length() != str2.length()){ return false; } // initial int table[256]; memset(table, 0, sizeof(table)); // check for(int i = 0; i < str1.length(); ++i){ table[str1[i]]++; } for(int i = 0; i < str2.length(); ++i){ table[str2[i]]--; if(table[str2[i]] < 0)return false; } return true; }


1.5 Write a method to replace all spaces in a string with '%20'.

/* solution for question 1.5 */ bool solution_5(char str[], int str_len, int max_len){ // null string if(NULL == str){ return false; } // count spaces int cnt = 0; for(int i = 0; i < str_len; ++i){ if(str[i] == ' '){ cnt++; } } // check memory if(cnt*2 + str_len + 1 > max_len){ return false; } // replace spaces int read_pos = str_len - 1; int write_pos = str_len + cnt*2; str[write_pos--] = '\0'; while(write_pos >= 0){ if(str[read_pos] != ' '){ str[write_pos--] = str[read_pos--]; } else{ str[write_pos--] = '0'; str[write_pos--] = '2'; str[write_pos--] = '%'; read_pos--; } } return true; }


1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?

/* solution for question 1.6 */ void solution_6(int n, vector<vector<int> > &img){ for(int i = 0; i < n/2; ++i){ for(int j = i; j < n-1-i; ++j){ int tmp = img[i][j]; img[i][j] = img[j][n-1-i]; img[j][n-1-i] = img[n-1-i][n-1-j]; img[n-1-i][n-1-j] = img[n-1-j][i]; img[n-1-j][i] = tmp; } } }


1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.

/* solution for question 1.7 */ void solution_7(int m, int n, vector<vector<int> > &matrix){ // check the first row and the first column bool firstRow = false, firstColumn = false; for(int i = 0; i < n; ++i){ if(matrix[0][i] == 0){ firstRow = true; break; } } for(int i = 0; i < m; ++i){ if(matrix[i][0] == 0){ firstColumn = true; break; } } // check other rows and other columns for(int i = 1; i < m; ++i){ for(int j = 1; j < n; ++j){ if(matrix[i][j] == 0){ matrix[0][j] = 0; matrix[i][0] = 0; } } } // update for(int i = 1; i < n; ++i){ if(matrix[0][i] == 0){ for(int j = 1; j < m; ++j){ matrix[j][i] = 0; } } } for(int i = 1; i < m; ++i){ if(matrix[i][0] == 0){ for(int j = 1; j < n; ++j){ matrix[i][j] = 0; } } } // update the first row and the first column if(firstRow){ for(int i = 0; i < n; ++i){ matrix[0][i] = 0; } } if(firstColumn){ for(int i = 0; i < m; ++i){ matrix[i][0] = 0; } } }


1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (i.i., "waterbottle" is a rotation of "erbottlewat").
/* solution for question 1.8 */
bool isSubstring(string str1, string str2){
	if(str1.find(str2) == string::npos){
		return false;
	}
	else{
		return true;
	}
}
bool solution_8(string str1, string str2){
	if(str1.length() != str2.length()){
		return false;
	}
	else{
		return isSubstring(str1+str1, str2);
	}
}
说明:版权所有,转载请注明出处。 Coder007的博客
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值