Careercup | Chapter 1
1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
字符串问题,需要先确定是不是只有ASCII码。
如果是,可以用char[256],也可以用位向量。位向量的实现参照《编程珠玑》。i&MASK就是取余。i>>SHIFT就是取商。
1 class BitVector { 2 public: 3 BitVector(int n) { 4 arr = new int[1 + n / INT_LEN]; 5 memset(arr, 0, (1 + n / INT_LEN) * sizeof(int)); 6 } 7 8 ~BitVector() {delete[] arr;} 9 10 void set(int i) { 11 arr[i >> SHIFT] |= (1 << (i & MASK)); 12 } 13 14 void clear(int i) { 15 arr[i >> SHIFT] &= ~(1 << (i & MASK)); 16 } 17 18 bool isSet(int i) { 19 return (arr[i >> SHIFT] & (1 << (i & MASK))); 20 } 21 22 private: 23 int* arr; 24 enum {INT_LEN = 32, SHIFT = 5, MASK = 0x1f}; 25 }; 26 27 bool unique(char *p) { 28 bool ch[256]; 29 memset(ch, false, sizeof(bool) * 256); 30 while (*p) { 31 if (ch[*p]) return false; 32 ch[*p] = true; 33 p++; 34 } 35 return true; 36 } 37 38 bool unique2(char *p) { 39 BitVector bv(256); 40 41 while (*p) { 42 if (bv.isSet(*p)) return false; 43 bv.set(*p); 44 p++; 45 } 46 return true; 47 }
1.2 Implement a function void reversefchar* str) in C or C++ which reverses a null-terminated string.
1 void reverse(char* str) { 2 if (str == NULL) return; 3 int n = strlen(str); 4 int i = 0, j = n - 1; 5 while (i < j) { 6 swap(str[i], str[j]); 7 i++; 8 j--; 9 } 10 }
1.3 Given two strings, write a method to decide if one is a permutation of the other。
忘了一点,就是要先确认是不是case sensitive和whitespace significant。
1 bool isPerm(char* s1, char* s2) { 2 int n1 = strlen(s1), n2 = strlen(s2); 3 if (n1 != n2) return false; 4 sort(s1, s1 + n1); 5 sort(s2, s2 + n2); 6 return strcmp(s1, s2) == 0; 7 } 8 9 bool isPerm2(char* s1, char* s2) { 10 int ch[256]; 11 memset(ch, 0, sizeof(int) * 256); 12 13 while (*s1) ch[*s1++]++; 14 while (*s2) { 15 if (--ch[*s2++] < 0) return false; 16 } 17 return true; 18 }
1.4 Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string.
1 char* replaceSpace(char* str) { 2 int b = 0; 3 char *p = str; 4 while (*p != '\0') if (*p++ == ' ') b++; 5 6 char *np = p + (b << 1); 7 while (p >= str) { 8 if (*p == ' ') { 9 *np-- = '0'; 10 *np-- = '2'; 11 *np-- = '%'; 12 } else { 13 *np-- = *p; 14 } 15 p--; 16 } 17 return str; 18 }
如果最左和最右的空格不需要替代,那么情况就会复杂一些,不能in place地实现了。
写的有点乱。
1 char* replaceSpace2(char* str) { 2 char *p = str; 3 while (*p && *p == ' ') p++; 4 if (!*p) return str; 5 str = p; 6 int b = 0, n = 0; 7 char *t; 8 while (*p) { 9 if (*p != ' ') { 10 n++; 11 t = p; 12 } else b++; 13 p++; 14 } 15 b -= (p - t - 1); 16 n += b; 17 char * newstr = new char[n + b * 2]; 18 p = newstr; 19 for (int i = 0; i < n; ++i) { 20 if (str[i] == ' ') { 21 *p++ = '%'; 22 *p++ = '2'; 23 *p++ = '0'; 24 } else { 25 *p++ = str[i]; 26 } 27 } 28 *p = '\0'; 29 return newstr; 30 }
1.5 Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string.
careercup上的code是O(n)时间复杂度和O(n)空间复杂度。另外就是不用stringbuffer的字符串拼接的开销是O(n^2)。
1 int digits(int n) { 2 int i = 0; 3 while (n > 0) { 4 n /= 10; 5 i++; 6 } 7 return i; 8 } 9 10 int int2str(int n, char *s) { 11 int i = 0; 12 while (n > 0) { 13 s[i++] = n % 10 + '0'; 14 n /= 10; 15 } 16 17 int k = 0, m = i - 1; 18 while (k < m) { 19 swap(s[k], s[m]); 20 k++; 21 m--; 22 } 23 return i; 24 } 25 26 char* compress(char* str) { 27 if (str == NULL) return str; 28 char *p = str; 29 int n1 = 0, n2 = 0, n; 30 while (*p) { 31 char *t = p; 32 while (*p && *p == *t) p++; 33 n = p - t; 34 n1 += n; 35 n2 += 1 + digits(n); 36 } 37 if (n2 >= n1) return str; 38 cout << n2 << endl; 39 char *s = new char[n2]; 40 int i = 0; 41 p = str; 42 while (*p) { 43 char *t = p; 44 while (*p && *p == *t) p++; 45 n = p - t; 46 s[i++] = *t; 47 i += int2str(n, s + i); 48 } 49 s[i] = '\0'; 50 return s; 51 }
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?
Leetcode有,点此。
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
Leetcode有,点此。
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 (e.g., "waterbottLe" is a rotation of "erbottLewat").
如果s2是s1旋转而来的,那么s1和s2的长度一定相同,s2一定是s1+s1的子串。反过来亦然。
原命题很显然。逆命题证明如下:
假设s1和s2长度相同,s2是s1+s1的子串。假设s1+s1=x+s2+y,令s2=m+n,使得len(x)+len(m)=len(s1),那么s1+s1=x+m+n+y;
于是有x+m=n+y=s1;且len(y)=len(s1)-len(n)=len(s2)-len(n)=len(m)。同理可证len(x)=len(n)。
因为len(y)=len(m),len(x)=len(n),x+m=n+y=s1,所以有x=n,y=m。于是s1=x+m=x+y,s2=m+n=y+x。所以s2是s1旋转而来的。
1 bool isRotate(string s1, string s2) { 2 if (s1.length() != s2.length()) return false; 3 string s1s1 = s1 + s1; 4 return (s1s1.find(s2) != string::npos); //isSubstring 5 }