翻譯和代碼有不正之處,歡迎批評指正。有好的想法,歡迎交流。
1、Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
問題描述:
設計一個演算法,判斷一個字串是否含有重複的字元,不能使用額外的資料結構。
思路:
排序,判斷是否重複。
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<string.h>void QuickSort(char *p, int n);int StrightWay(char *);int main(){int n;char *p;int i = 0;int temp;char *ptemp;char c;printf("Input n\n");scanf("%d", &n);if(n <= 0){printf("Input Error\n");return 1;}p = (char*)malloc(sizeof(char) * (n+1));if(p == NULL){printf("Out of space\n");return 1;}getchar();ptemp = p;temp = n;printf("Input the string\n");while(((c = getchar()) != EOF) && temp){*ptemp++ = c;temp--;}*ptemp = '\0';//printf("%s", p);QuickSort(p, n);if(!StrightWay(p))printf("It's unique\n");elseprintf("It's not unique\n");free(p);return 0;}void QuickSort(char *p, int n){void Swap(char *, char *);void Find(char *, char *, char *);char temp;int i, j;if(n < 2)return;j = n>>1;i = -1;Find(p, p+j, p+n-1);Swap(p+j, p+n-1);for(j = 0; j < n-1; j++){if(p[j] < p[n-1]){++i; if(i != j) { Swap(p+i, p+j); }}}++i;Swap(p+i, p+n-1);QuickSort(p, i);QuickSort(p+i+1, n-i-1);}void Swap(char *p, char *q){char c;c = *p;*p= *q;*q = c;}int StrightWay(char *p){int i, j;if(p == NULL)return 1;for(i = 0, j = 1; (p[i] != p[j]) && (p[j] != '\0'); i++, j++);if(p[j] == '\0' && p[i+1] == '\0')return 0;elsereturn 1;}void Find(char *a, char *b, char *c){if(*a > *b)Swap(a, b);if(*a > *c)Swap(a,c);if(*b > *c)Swap(b, c);}
2、Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)
問題描述:
寫代碼倒置一個c風格的字串。
#include<stdio.h>#include<string.h>#include<malloc.h>void Reverse(char *p);void Swap(char *, char *);int main(){char p[100] = "abcdefg";Reverse(p);printf("%s\n", p);return 0;}void Reverse(char *p){int i, j;if(p == NULL)return;i = 0; j = strlen(p) - 1;while(i < j){Swap(p+i, p+ j);i++;j--;}}void Swap(char *p, char *a){char c;c = *p;*p = *a;*a = c;}
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
問題描述:
設計一個演算法,移除一個字串中重複的字元,不要使用額外的緩衝,一個或者兩個變數時可以的。
思路:
排序,設定兩個指標。
#include<stdio.h>#include<string.h>char *Remove(char *);void Qsort(char *, int n);int main(){char p[100] = "333333222222";int n = strlen(p);if(n < 2)return 0;Qsort(p, n);//printf("%s\n", p);printf("%s", Remove(p));}char *Remove(char *p){void Move(char *, char *);char *first, *second;if(p == NULL)return NULL;first = p;second = first + 1;while(*second != '\0'){if(*first != *second){first++;second++;}else{while(*second == *first)second++;Move(second, first+1);second = first+1;}}return p;}void Move(char *second, char *first){if(!(first && second))return;while(*second)*first++ = *second++;*first = '\0';}void Qsort(char *p, int n){void Swap(char *, char *);int i, j;j = 1;i = 0;if(n < 2)return;for(; j < n; j++){if(p[j] < p[0]){i++;if(i != j)Swap(p+i, p+j);}}Swap(p, p+i);Qsort(p, i);Qsort(p+i+1, n-i-1);}void Swap(char *p, char *q){char c;c = *p;*p = *q;*q = c;}
4、Write a method to decide if two strings are anagrams or not
問題描述:
判斷兩個字串是否是迴文的。
思路:
用一個256個元素的數組統計第一個字串中每個出現的次數
#include<stdio.h>int anagrams(char*, char *);int main(){char a[100] = "abcd";char b[100] = "adbce";if(anagrams(a, b)){printf("YES\n");}elseprintf("NO\n");}int anagrams(char *a, char *b){int Arr[256];int i = 0;for(i = 0; i < 256; ++i)Arr[i] = 0;while(*a != '\0'){Arr[*a]++;a++;}while(*b != 0){Arr[*b]--;if(Arr[*b] < 0)return 0;b++;}for(i = 0; i < 256; ++i)if(Arr[i] != 0)return 0;if(i == 256)return 1;}
5、Write a method to replace all spaces in a string with ‘%20’.
問題描述:
把一個字串中的空格用 %20替換。
思路:
先遍曆一字串,統計有多少個空格,然後從後往前複製替換。
#include<stdio.h>char * Replace(char *);int main(){char p[100] = " ";printf("%s", Replace(p));}char* Replace(char *p){int count;char *q, *temp;if(p == NULL)return NULL;count = 0;q = p;while(*q != '\0'){if(*q == ' ')count++;q++;}temp = q+ 2* count;while(q >= p){if(*q == ' '){*temp-- = '0';*temp-- = '2';*temp = '%';}else*temp = *q;temp--;q--;}return p;}
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?
問題描述:
把一個數字順時針旋轉90度。
思路:
方法:按層,依次把每個元素放到合適的位置。
代碼如下:
#include<stdio.h>#include<malloc.h>void Rotate(int n,int**p);int main(){int n;int **p;int i, j;printf("Input n \n");scanf("%d", &n);p = (int **)malloc(sizeof(int*));if(p == NULL)return 0;for(i = 0; i < n; ++i){p[i] = (int *)malloc(sizeof(int) * n);if(p[i] == NULL)return 0;}printf("Input value n*n\n");for(i = 0; i < n; i++)for(j = 0; j < n; j++)scanf("%d", *(p+i) + j);Rotate(n, p);printf("out value\n");for(i = 0; i < n; i++){for(j = 0; j < n; j++)printf("%d ", p[i][j]);printf("\n");}return 0;}void Rotate(int n, int **a){int layer;int last;int first;int offset;int i;int temp;for(layer = 0; layer < n/2; layer++){first = layer;last = n-1 - layer;for(i = first; i < last; ++i){offset = i - first;temp = a[first][i];a[first][i] = a[last-offset][first];a[last-offset][first] = a[last][last-offset];a[last][last-offset] = a[i][last];a[i][last] = temp;}}}
7、Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
問題描述:
如果一個數組中有元素的值為零,則把所在行和列都設為0.
思路:
遍曆並儲存值為0的元素的行號和列號。然後置為0;
代碼如下:
#include<stdio.h>#include<malloc.h>#include<assert.h>void Zero(int n, int **p);int main(){int n;int **p;int i, j;printf("Input n \n");scanf("%d", &n);assert(n > 0);p = (int **)malloc(sizeof(int*));if(p == NULL)return 0;for(i = 0; i < n; ++i){p[i] = (int *)malloc(sizeof(int) * n);if(p[i] == NULL)return 0;}printf("Input value n*n\n");for(i = 0; i < n; i++)for(j = 0; j < n; j++)scanf("%d", *(p+i) + j);Zero(n, p);printf("out value\n");for(i = 0; i < n; i++){for(j = 0; j < n; j++)printf("%d ", p[i][j]);printf("\n");}for(i = 0; i < n; ++i) free(p[i]);p == NULL;return 0;}void Zero(int n, int **p){int *column;//lieint *line;//hang;int i, j;column = (int *)malloc(sizeof(int) * n);for(i = 0; i < n; ++i)column[i] = 0;line = (int *)malloc(sizeof(int) * n);for(i = 0; i < n; ++i)line[i] = 0;for(i = 0; i < n; i++)for(j = 0; j < n; j++){if(p[i][j] == 0){column[j] = 1;line[i] = 1;}}for(i = 0; i < n; i++){if(column[i] == 1){for(j = 0; j < n; j++)if(p[j][i] != 0)p[j][i] = 0;}}for(i = 0; i < n; i++)if(line[i] == 1){for(j = 0; j < n; j++)if(p[i][j]!= 0)p[i][j] = 0;}free(line);free(column);line = NULL;column = NULL;}
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.e., “waterbottle” is a rotation of “erbottlewat”).
問題描述:
判斷一個字串是不是另外一個的旋轉字串。
思路:
構造'waterbottlewaterbottle'判斷erbottlewat是不是構造的字串的字串。
代碼如下:
#include<stdio.h>#include<string.h>bool Isrotation(char *, char *);int main(){char p[100] = "waterbottle";char q[50] = "erbottlewat";if(strlen(p) != strlen(q))return 0;if(Isrotation(p, q))printf("YES\n");elseprintf("NO\n");return 0;}bool Isrotation(char *p, char *q){int str(char *p, char *q);void copy(char *);int i;if(!(p&&q)){printf("Error\n");return false;}copy(p);printf("%s\n", p);p[strlen(p)] = '\0';i = str(p, q);if(i == 1)return true;elsereturn false;}int str(char *p, char *q){int i, j, k;int lenp, lenq;if(!(p&&q)){printf("Error\n");return 0;}lenp = strlen(p);lenq = strlen(q);for(i = 0; i < lenp-lenq+1; ++i){for(j = 0,k = i; q[j]!= '\0' && (p[k] == q[j]); k++, j++);if(q[j] == '\0')break;}if(q[j] == '\0' && i < lenp-lenq+1)return 1;elsereturn 0;}void copy(char *p){char *temp;char *ptr;ptr = temp = p;while(*temp)temp++; p = temp;while(ptr < p){*temp++ = *ptr++;}*temp = '\0';}