CareerCup chapter 1 Arrays and Strings
2015-10-14 19:28
309 查看
1.Implement an algorithm to determine if a string has all unique characters What if you can not use additional data structures?
The length of ACSII code of a character is 8, so we can build a array, the length is 260, to represent the hash table of all characters.
bool allCharUnique(string s){
int a[260];
memset(a,0,sizeof a);
for(int i=0;i<s.length();i++){
if(a[s[i]]==0)a[s[i]]++;
else return false;
}
return true;
}
2.Write code to reverse a C-Style String (C-String means that “abcd” is represented as five characters, including the null character )
consider the null character. we traverse the string to find the last index of valid char.
void reverseCStyleString(char* s){
int i=0,j=0;
char *cur = s;
while(cur){cur++;j++;}--j;
while(i<j){
swap(s[i],s[j]);
++i;--j;
}
}
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 not
FOLLOW UP
Write the test cases for this method
Here is two idea. One is O(n^2), by loop all characters of the string, and each char, loop all characters whose index is bigger than current char. if
these two char is equal, then erase the char whose index is bigger.
Other idea is build a map array, map[8], represents the hash table of all characters. Following is this code:
void removeDuplicateChar(string s){
if(s==NULL)return;
int map[]={0,0,0,0,0,0,0,0};
int ascii,index,pos;
int cur=0;
for(int i=0;i<s.length();i++){
ascii = (int)s[i];
index = ascii/32;
pos = ascii%32;
int res = map[index]&(1<<pos);
if(!res){
map[index]|=1<<pos;
s[cur++]=s[i];
}
}
s[cur]='\0';
return;
}
Test case: NULL , empty string, all duplicates and others.
4.Write a method to decide if two strings are anagrams or not.
One idea: sort these two strings, then look whether of these two strings are equal. O(nlogn)
Other idea: map the first string by unordered_map<char,int>. then decide the map 0f the second string is equal to the first. O(n). Following is this code:
bool isAnagrams(string a,string b){
if(a==NULL||b==NULL)return false;
if(a.length()==0&&b.length()==0)return true;
if(a.length()!=b.length())return false;
unordered_map<char,int> map;
for(int i=0;i<a.length();i++){
if(map.count(a[i])==0)map.insert(make_pair(a[i],1));
else map[a[i]]++;
}
for(int i=0;i<b.length();i++){
if(map.count(b[i])==0)return false;
else{
map[b[i]]--;
if(map[b[i]]<0)return false;
}
}
return true;
}
5.Write a method to replace all spaces in a string with "%20".
"%20" is a string with length of 3. each space is 1 length. So the string's length is extend to s.length()+spaceCount*2. First enlarge the string,whose length
is s.length()+spaceCount*2, then we copy the original string to this new string. When we come across a space, the new string is insert '0' '2' '%'.
void replaceAllSpaces(string s){
if(s==NULL)return;
int spaceCount=0,preLenth=s.length();
for(int i=0;i<s.length();i++){
if(s[i]==' ')spaceCount++;
}
for(int i=0;i<2*spaceCount;i++)s+=' ';
int cur=s.length()-1,j=preLenth-1;
while(j>=0){
if(s[j]==' '){
s[cur--]='0';
s[cur--]='2';
s[cur]='%'
}else{
s[cur]=s[j];
}
cur--;j--;
}
return;
}
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?
ClockWise rotate: first we turn over all pixels diagonal. then reverse each line.
other idea: rotate each layer such as loop diagram. left layer ->top layer,bottom layer -> left layer, right layer->bottom layer, top layer->right layer.
void clockwiseRotate(vector<vector<int> > matrix){
if(matrix.size()==0||matrix[0].size()==0)return;
int n=matrix.size();
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
swap(matrix[i][j],matrix[j][i]);
}
}
for(int i=0;i<n;i++){
reverse(matrix[i].begin(),matrix[i].end());
}
return;
}
7.Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
we build two record array(row,column) to track in which row/column should be set to 0.
void setMatrix(vector<vector<int> > matrix){
if(matrix.size()==0||matrix[0].size()==0)return;
int *row = new int[matrix.size()];
int *column = new int[matrix[0].size()];
memset(row,0,sizeof row);memset(column,0,sizeof column);
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[i].size();j++){
if(matrix[i][j]==0){
row[i]=1;column[j]=1;
}
}
}
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[i].size();j++){
if(row[i]||column[j])matrix[i][j]=0;
}
}
return;
}
8.Assume you have a method isSubstring which checks if one word is a substring of another Given two strings,s1and s2,write code to check if s2 is a rotation of s1using only one call to isSubstring
(i e , “waterbottle” is a rotation of “erbottlewat”).
first, make sure s1.length() equals to s2.length(). if(s1.length()!=s2.length())return false;
Then, concatenate s1 with itself and look s2 is or not the substring of s1s1.
bool isRotation(string s1,string s2){
if(s1==NULL||s2==NULL)return false;
int len = s1.length();
if(len==s2.length()&&len>0){
string s1s1 = s1+s1;
return isSubstring(s1s1,s2);
}
return false;
}
The length of ACSII code of a character is 8, so we can build a array, the length is 260, to represent the hash table of all characters.
bool allCharUnique(string s){
int a[260];
memset(a,0,sizeof a);
for(int i=0;i<s.length();i++){
if(a[s[i]]==0)a[s[i]]++;
else return false;
}
return true;
}
2.Write code to reverse a C-Style String (C-String means that “abcd” is represented as five characters, including the null character )
consider the null character. we traverse the string to find the last index of valid char.
void reverseCStyleString(char* s){
int i=0,j=0;
char *cur = s;
while(cur){cur++;j++;}--j;
while(i<j){
swap(s[i],s[j]);
++i;--j;
}
}
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 not
FOLLOW UP
Write the test cases for this method
Here is two idea. One is O(n^2), by loop all characters of the string, and each char, loop all characters whose index is bigger than current char. if
these two char is equal, then erase the char whose index is bigger.
Other idea is build a map array, map[8], represents the hash table of all characters. Following is this code:
void removeDuplicateChar(string s){
if(s==NULL)return;
int map[]={0,0,0,0,0,0,0,0};
int ascii,index,pos;
int cur=0;
for(int i=0;i<s.length();i++){
ascii = (int)s[i];
index = ascii/32;
pos = ascii%32;
int res = map[index]&(1<<pos);
if(!res){
map[index]|=1<<pos;
s[cur++]=s[i];
}
}
s[cur]='\0';
return;
}
Test case: NULL , empty string, all duplicates and others.
4.Write a method to decide if two strings are anagrams or not.
One idea: sort these two strings, then look whether of these two strings are equal. O(nlogn)
Other idea: map the first string by unordered_map<char,int>. then decide the map 0f the second string is equal to the first. O(n). Following is this code:
bool isAnagrams(string a,string b){
if(a==NULL||b==NULL)return false;
if(a.length()==0&&b.length()==0)return true;
if(a.length()!=b.length())return false;
unordered_map<char,int> map;
for(int i=0;i<a.length();i++){
if(map.count(a[i])==0)map.insert(make_pair(a[i],1));
else map[a[i]]++;
}
for(int i=0;i<b.length();i++){
if(map.count(b[i])==0)return false;
else{
map[b[i]]--;
if(map[b[i]]<0)return false;
}
}
return true;
}
5.Write a method to replace all spaces in a string with "%20".
"%20" is a string with length of 3. each space is 1 length. So the string's length is extend to s.length()+spaceCount*2. First enlarge the string,whose length
is s.length()+spaceCount*2, then we copy the original string to this new string. When we come across a space, the new string is insert '0' '2' '%'.
void replaceAllSpaces(string s){
if(s==NULL)return;
int spaceCount=0,preLenth=s.length();
for(int i=0;i<s.length();i++){
if(s[i]==' ')spaceCount++;
}
for(int i=0;i<2*spaceCount;i++)s+=' ';
int cur=s.length()-1,j=preLenth-1;
while(j>=0){
if(s[j]==' '){
s[cur--]='0';
s[cur--]='2';
s[cur]='%'
}else{
s[cur]=s[j];
}
cur--;j--;
}
return;
}
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?
ClockWise rotate: first we turn over all pixels diagonal. then reverse each line.
other idea: rotate each layer such as loop diagram. left layer ->top layer,bottom layer -> left layer, right layer->bottom layer, top layer->right layer.
void clockwiseRotate(vector<vector<int> > matrix){
if(matrix.size()==0||matrix[0].size()==0)return;
int n=matrix.size();
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
swap(matrix[i][j],matrix[j][i]);
}
}
for(int i=0;i<n;i++){
reverse(matrix[i].begin(),matrix[i].end());
}
return;
}
7.Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
we build two record array(row,column) to track in which row/column should be set to 0.
void setMatrix(vector<vector<int> > matrix){
if(matrix.size()==0||matrix[0].size()==0)return;
int *row = new int[matrix.size()];
int *column = new int[matrix[0].size()];
memset(row,0,sizeof row);memset(column,0,sizeof column);
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[i].size();j++){
if(matrix[i][j]==0){
row[i]=1;column[j]=1;
}
}
}
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[i].size();j++){
if(row[i]||column[j])matrix[i][j]=0;
}
}
return;
}
8.Assume you have a method isSubstring which checks if one word is a substring of another Given two strings,s1and s2,write code to check if s2 is a rotation of s1using only one call to isSubstring
(i e , “waterbottle” is a rotation of “erbottlewat”).
first, make sure s1.length() equals to s2.length(). if(s1.length()!=s2.length())return false;
Then, concatenate s1 with itself and look s2 is or not the substring of s1s1.
bool isRotation(string s1,string s2){
if(s1==NULL||s2==NULL)return false;
int len = s1.length();
if(len==s2.length()&&len>0){
string s1s1 = s1+s1;
return isSubstring(s1s1,s2);
}
return false;
}
相关文章推荐
- 百度实习回来了
- js求字符长度
- 安卓开发环境安装--菜鸟之路(二)
- 信息系统项目管理师论文:论项目的风险管理
- 如何在Mac上安装Ruby
- Android CardView简单使用方法
- 1008. Elevator
- 如何理解IOC 依赖注入的思想(目前见过最好的对DI的描述)
- iOS开发工具篇-AppStore统计工具
- 实现姓名和学号每隔一秒钟互换的效果
- 黑马程序员——IO流
- mysql主从复制,读写分离,半同步复制实现
- 1007. Maximum Subsequence Sum
- 60 Permutation Sequence
- 14 Ways to Take Charge of LiDAR Data
- iOS开发之加载、滑动翻阅大量图片优化解决方案
- SQL分组查询,子查询
- CAS Server部署指南
- iOS 适配 ,关于prefix Header 文件的配置
- 实现列表框元素的左右移动