题目:
1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? _________________________________________________________________
// 不允许使用额外数据结构判断一个字符串中是否所有的字符都不相同 //char * const cp : 定义一个指向字符的指针常数,即const指针 //const char* p : 定义一个指向字符常数的指针 //char const* p : 等同于const char* p #include <cstdio> #include <cstring> #include <bitset> bool check1(char const * str) { bool c[256] = {false}; int len = strlen(str); for (int i = 0; i < len; ++i) { if (!c[str[i]]) c[str[i]] = true; else return false; } return true; } bool check2(char const * str) { int bit = 0; int len = strlen(str); for (int i = 0; i < len; ++i) { // printf("%d ", str[i] - 'a'); int v = str[i] - 'a'; if (bit & (1 << v)) return false; else bit |= 1 << v; } return true; } bool check3(char const * str) { std::bitset<256> bit; // for (int i = 0;i <256;++i) printf("%d ", (int) bit[i]); int len = strlen(str); for (int i = 0; i < len; ++i) { if (bit[str[i]]) return false; else bit.set(str[i]); } return true; } bool check4(char const * str) { int bit[8] = {0}; int len = strlen(str); for (int i = 0; i < len; ++i) { int v = (int) str[i]; int idx = v / 32, shift = v % 32; if (bit[idx] & (1 << shift)) return false; else bit[idx] |= 1 << shift; } return true; } int main() { char *t1 = "asdsdfff"; char *t2 = "asdksdas"; char *t3 = "wkjpul"; char *t4 = "anmzxsfghij"; char *t5 = "%+-anmzxsfghij"; printf("test %s %d ", t1, check1(t1)); printf("test %s %d ", t2, check1(t2)); printf("test %s %d ", t3, check1(t3)); printf("test %s %d ", t4, check1(t4)); printf("test %s %d ", t1, check2(t1)); printf("test %s %d ", t2, check2(t2)); printf("test %s %d ", t3, check2(t3)); printf("test %s %d ", t4, check2(t4)); printf("test %s %d ", t5, check3(t5)); printf("test %s %d ", t5, check4(t5)); return 0; }
pg95 1.2 Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.) _________________________________________________________________
http://www.cnblogs.com/chenchenluo/p/3522195.html
这个教训终身难忘。
#include <cstdio> #include <cstring> char* reverse(char * str) { char *ret = str, *end = str; if (str) { while (*end) { ++end; } --end; while (str < end) { char tmp = *str; *str++ = *end; *end-- = tmp; } } return ret; } char* reverse2(char *str) { char *ret = str; int s = 0, e = strlen(str) - 1; while (s < e) { char tmp = str[s]; str[s++] = str[e]; str[e--] = tmp; } return ret; } int main() { char t1[] = "23123/0)*(&^&*$%^$^%asdsdfff"; char t2[] = "a3234][\sdksdaacas"; char t3[] = "/.,';]][[])(*wkjpul"; char t4[] = "21['./a2na34324mzxsfghij"; char t5[] = "%+-anmzxsfghij"; reverse(t1); printf("test %s ", t1); printf("test %s %s ", t2, reverse(t2)); printf("test %s %s ", t3, reverse2(t3)); printf("test %s %s ", t4, reverse(t4)); printf("test %s %s ", t5, reverse2(t5)); return 0; }
pg96 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 not. FOLLOW UP Write the test cases for this method. _________________________________________________________________
pg97 1.4 Write a method to decide if two strings are anagrams or not.
_________________________________________________________________
pg 99 1.5 Write a method to replace all spaces in a string with ‘%20’.
________________________________________________________________
pg 100 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?
________________________________________________________________
pg 101 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. ________________________________________________________________
pg 102 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.e., “waterbottle” is a rotation of “erbottlewat”).