代码改变世界

crack the coding interview

2017-04-25 11:05  forest~wow  阅读(254)  评论(0)    收藏  举报

crack the coding interview

1.1

  1. #ifndef __Question_1_1_h__ 
  2. #define __Question_1_1_h__ 
  3.  
  4. #include <string> 
  5.  
  6. using std::string; 
  7.  
  8. class Question1_1  
  9. { 
  10. public: 
  11. int run(); 
  12. bool isUniqueChars(const string& str); 
  13. bool isUniqueChars2(const string& str); 
  14. string result(bool value); 
  15. };  
  16.  
  17. #endif // __Question_1_1_h__ 
  18.  
  19. #include<iostream> 
  20. #include<string> 
  21. #include "Question1_1.h" 
  22. using namespace std; 
  23.  
  24. bool Question1_1::isUniqueChars(const string& str) 
  25. { 
  26. if (str.length() > 256)  
  27. { 
  28. return false; 
  29. } 
  30.  
  31. unsigned int checker = 0; 
  32.  
  33. for (int i = 0; i < str.length(); ++i)  
  34. { 
  35. int value = str[i] - 'a'; 
  36.  
  37. if ((checker & (1 << value)) != 0) 
  38. { 
  39. return false; 
  40. } 
  41.  
  42. checker |= (1 << value); 
  43. } 
  44.  
  45. return true; 
  46. } 
  47.  
  48. bool Question1_1::isUniqueChars2(const string& str)  
  49. { 
  50. if (str.length() > 256)  
  51. { 
  52. return false; 
  53. } 
  54.  
  55. bool ascii_set[256] = { false }; 
  56.  
  57. for (int i = 0; i < str.length(); ++i)  
  58. { 
  59. int value = str[i]; 
  60.  
  61. if (ascii_set[value])  
  62. { 
  63. return false; 
  64. } 
  65.  
  66. ascii_set[value] = true;  
  67. } 
  68.  
  69. return true; 
  70. } 
  71.  
  72. /* Function to print true and false */ 
  73. string Question1_1::result(bool value) 
  74. { 
  75. if (value)  
  76. { 
  77. return "True"; 
  78. } 
  79.  
  80. return "False"; 
  81. } 
  82.  
  83. int Question1_1::run()  
  84. { 
  85. string input[] ={"abcde", "aba"}; 
  86.  
  87. for (int i = 0; i < 2; i++)  
  88. { 
  89. cout << input[i] << " has unique characters: " << result(isUniqueChars(input[i])) << endl; 
  90. cout << input[i] << " has unique characters: " << result(isUniqueChars2(input[i])) << endl; 
  91. } 
  92.  
  93. return 0; 
  94. } 

1.2

  1. #ifndef __Question_1_2_h__ 
  2. #define __Question_1_2_h__ 
  3.  
  4. class Question1_2  
  5. { 
  6. public: 
  7. int run(); 
  8. void reverse(char* str); 
  9. };  
  10.  
  11. #endif // __Question_1_2_h__ 
  12. #include<iostream> 
  13. #include "Question1_2.h" 
  14.  
  15. using std::cout; 
  16. using std::endl; 
  17.  
  18. void Question1_2::reverse(char* str)  
  19. { 
  20. char *ptrEnd = str; 
  21. char temp; 
  22.  
  23. if (str)  
  24. { 
  25. while (*ptrEnd)  
  26. { 
  27. ptrEnd++; 
  28. } 
  29. ptrEnd--; 
  30.  
  31. while (str < ptrEnd)  
  32. { 
  33. temp = *str; 
  34. *str++ = *ptrEnd; 
  35. *ptrEnd-- = temp; 
  36. } 
  37. } 
  38. } 
  39.  
  40. int Question1_2::run()  
  41. { 
  42. char input[][10] = { "abcde", "cat" }; 
  43.  
  44. for (int i = 0; i < 2; i++)  
  45. { 
  46. cout << "reversing the string: " << input[i] << endl; 
  47. reverse(input[i]); 
  48. cout << "reverse of input string is " << input[i] << endl; 
  49. } 
  50.  
  51. return 0; 
  52. } 

1.3

  1. #ifndef __Question_1_3_B_h__ 
  2. #define __Question_1_3_B_h__ 
  3.  
  4. #include <string> 
  5.  
  6. using std::string; 
  7.  
  8. class Question1_3_B  
  9. { 
  10. public: 
  11. int run(); 
  12. bool permutation(const string& a, const string& b); 
  13. string result(bool value); 
  14. };  
  15.  
  16. #endif // __Question_1_3_B_h__ 
  17.  
  18. #include<iostream> 
  19. #include<string> 
  20. #include<algorithm> 
  21. #include "Question1_3_B.h" 
  22.  
  23. using namespace std; 
  24.  
  25. bool Question1_3_B::permutation(const string& a, const string& b)  
  26. { 
  27. if (a.length() != b.length())  
  28. { 
  29. return false; 
  30. } 
  31.  
  32. int ascii_set[256] = {0}; 
  33.  
  34. for (int i = 0; i < a.length(); i++)  
  35. { 
  36. int val = static_cast<int>(a[i]); 
  37. ascii_set[val]++; 
  38. } 
  39.  
  40. for (int i = 0; i < b.length(); i++)  
  41. { 
  42. int val = static_cast<int>(b[i]); 
  43.  
  44. if ((--ascii_set[val]) < 0)  
  45. { 
  46. return false; 
  47. } 
  48. } 
  49.  
  50. return true; 
  51. } 
  52.  
  53. string Question1_3_B::result(bool value)  
  54. { 
  55. if (value)  
  56. { 
  57. return "True"; 
  58. } 
  59.  
  60. return "False"; 
  61. } 
  62.  
  63. int Question1_3_B::run()  
  64. { 
  65. string a = "apple"; 
  66. string b = "papel"; 
  67.  
  68. cout << "Result for " << a << " and " << b << " is " << result(permutation(a, b)) << endl; 
  69.  
  70. return 0; 
  71. } 

1.4

  1. #ifndef __Question_1_4_h__ 
  2. #define __Question_1_4_h__ 
  3. #include <memory> 
  4.  
  5. class Question1_4  
  6. { 
  7. public: 
  8. int run(); 
  9. void replaceSpaces(std::unique_ptr<char[]>&, int length); 
  10. };  
  11.  
  12. #endif // __Question_1_4_h__ 
  13.  
  14. #include<iostream> 
  15. #include<memory> 
  16. #include<string> 
  17. #include "Question1_4.h" 
  18.  
  19. using namespace std; 
  20.  
  21. void Question1_4::replaceSpaces(unique_ptr<char[]> &str, int length) //char str[] 
  22. { 
  23. int newLength, spaceCount = 0; 
  24.  
  25. //count the number of spaces in the given string. 
  26. for (int i = 0; i < length; i++)  
  27. { 
  28. if (str[i] == ' ')  
  29. { 
  30. spaceCount++; 
  31. } 
  32. } 
  33.  
  34. //calculate new string size. 
  35. newLength = length + spaceCount * 2; 
  36. str[newLength] = '\0'; 
  37.  
  38. //copying the characters backwards and inserting %20 
  39. for (int i = length - 1; i >= 0; i--)  
  40. { 
  41. if (str[i] == ' ')  
  42. { 
  43. str[newLength - 1] = '0'; 
  44. str[newLength - 2] = '2'; 
  45. str[newLength - 3] = '%'; 
  46. newLength -= 3; 
  47. }  
  48. else 
  49. { 
  50. str[newLength - 1] = str[i]; 
  51. newLength--; 
  52. } 
  53. } 
  54. } 
  55.  
  56. int Question1_4::run()  
  57. { 
  58. string str = "abc d e f"; 
  59.  
  60. // Increasing length of the string to meet question requirement of 'true' length by using char array. (Note: using a unique_ptr here) 
  61. auto newStr = make_unique<char[]>(str.length() + 3 * 2 + 1); 
  62.  
  63. for (int i = 0; i < str.length(); i++)  
  64. { 
  65. newStr[i] = str[i]; 
  66. } 
  67.  
  68. cout << "Original string is " << str << endl; 
  69. replaceSpaces(newStr, str.length()); 
  70. cout << "New string with %20 is " << newStr.get() << endl; 
  71.  
  72. return 0; 
  73. } 

1.5

  1. #ifndef __Question_1_5_h__ 
  2. #define __Question_1_5_h__ 
  3.  
  4. #include <string> 
  5.  
  6. using std::string; 
  7.  
  8. class Question1_5  
  9. { 
  10. public: 
  11. int run(); 
  12. int stringToInt(const string& value); 
  13. string intToString(int value); 
  14. int countCompression(const string& str); 
  15.  
  16. /// C++ std::string is efficient and no need to use a "StringBuffer"-like structure. 
  17. string compressBetter(const string& str); 
  18. };  
  19.  
  20. #endif // __Question_1_5_h__ 
  21.  
  22. #include<iostream> 
  23. #include<string> 
  24. #include<sstream> 
  25. #include "Question1_5.h" 
  26.  
  27. using namespace std; 
  28.  
  29. int Question1_5::stringToInt(const string& value)  
  30. { 
  31. int temp; 
  32. stringstream(value) >> temp; 
  33.  
  34. return temp; 
  35. } 
  36.  
  37. string Question1_5::intToString(int value)  
  38. { 
  39. string temp; 
  40. stringstream convert; 
  41. convert << value; 
  42. temp = convert.str(); 
  43.  
  44. return temp; 
  45. } 
  46.  
  47. int Question1_5::countCompression(const string& str) 
  48. { 
  49. if (str.length() == 0)  
  50. { 
  51. return 0; 
  52. } 
  53.  
  54. char last = str.at(0); 
  55. int size = 0; 
  56. int count = 1; 
  57.  
  58. for (int i = 1; i < str.length(); i++)  
  59. { 
  60. if (str.at(i) == last)  
  61. { 
  62. count++; 
  63. } 
  64. else 
  65. { 
  66. last = str.at(i); 
  67. size = size + 1 + intToString(count).length(); 
  68. count = 1; 
  69. } 
  70. } 
  71. size = size + 1 + intToString(count).length(); 
  72.  
  73. return size; 
  74. } 
  75.  
  76. string Question1_5::compressBetter(const string& str)  
  77. { 
  78. int size = countCompression(str); 
  79.  
  80. if(size >= str.length())  
  81. { 
  82. return str; 
  83. } 
  84.  
  85. string newstr; 
  86. char last = str.at(0); 
  87. int count = 1; 
  88.  
  89. for (int i = 1; i < str.length(); i++)  
  90. { 
  91. if (str.at(i) == last)  
  92. { 
  93. count++; 
  94. } 
  95. else 
  96. { 
  97. newstr += last; 
  98. newstr.append(intToString(count)); 
  99. last = str.at(i); 
  100. count = 1; 
  101. } 
  102. } 
  103. newstr += last; 
  104. newstr.append(intToString(count)); 
  105.  
  106. return newstr; 
  107. } 
  108.  
  109. int Question1_5::run()  
  110. { 
  111. string str = "abbccccccde"; 
  112. string newstr = compressBetter(str); 
  113. cout << "Original string is " << str << endl; 
  114. cout << "Compressed string is " << newstr << endl; 
  115.  
  116. return 0; 
  117. } 

1.6

  1.  
  2. #ifndef __Question_1_6_h__ 
  3. #define __Question_1_6_h__ 
  4.  
  5. class Question1_6  
  6. { 
  7. public: 
  8. /** 
  9. * Since we can't provide variable matrix size in C++, we will do it "the c way" and will provide a 1-dimensional 
  10. * array 
  11. */ 
  12. void rotate(int* matrix, int n); 
  13. void printMatrix(int* matrix, int m, int n); 
  14. int run(); 
  15. }; 
  16.  
  17. #endif // __Question_1_6_h__ 
  18.  
  19. #include <iostream> 
  20. #include <memory> 
  21. #include "Question1_6.h" 
  22.  
  23. using namespace std; 
  24.  
  25. void Question1_6::rotate(int* matrix, int n)  
  26. { 
  27. for (int layer = 0; layer < n / 2; ++layer)  
  28. { 
  29. int first = layer; 
  30. int last = n - 1 - layer; 
  31.  
  32. for (int i = first; i < last; ++i)  
  33. { 
  34. int offset = i - first; 
  35. // save top 
  36. int top = matrix[first * n + i]; 
  37.  
  38. // left to top 
  39. matrix[first * n + i] = matrix[(last-offset) * n + first]; 
  40.  
  41. // bottom to left 
  42. matrix[(last-offset) * n + first] = matrix[last * n + (last-offset)]; 
  43.  
  44. // right to bottom 
  45. matrix[last * n + (last-offset)] = matrix[i * n + last]; 
  46.  
  47. // top to right 
  48. matrix[i * n + last] = top; 
  49. } 
  50. } 
  51. } 
  52.  
  53. void Question1_6::printMatrix(int* matrix, int m, int n)  
  54. { 
  55. for (int i = 0; i < m; ++i)  
  56. { 
  57. for (int j = 0; j < n; ++j)  
  58. { 
  59. cout << matrix[i * n + j] << " "; 
  60. } 
  61.  
  62. cout << endl; 
  63. } 
  64. } 
  65.  
  66. int Question1_6::run()  
  67. { 
  68. int matrix[][5] ={{1, 2, 3, 4, 5}, 
  69. {6, 7, 8, 9, 10}, 
  70. {11, 12, 13, 14, 15}, 
  71. {16, 17, 18, 19, 20}, 
  72. {21, 22, 23, 24, 25}}; 
  73. int* matrixPtr = (int*)matrix; 
  74.  
  75. cout << "original matrix is :" << endl; 
  76. printMatrix(matrixPtr, 5, 5); 
  77. rotate(matrixPtr, 5); 
  78. cout << "rotated matrix is: " << endl; 
  79. printMatrix(matrixPtr, 5, 5); 
  80.  
  81. return 0; 
  82. } 

1.7

  1. #ifndef __Question_1_7_h__ 
  2. #define __Question_1_7_h__ 
  3.  
  4. class Question1_7  
  5. { 
  6. public: 
  7. /** 
  8. * Since we can't provide variable matrix size in C++, we will do it "the c way" and will provide a 1-dimensional 
  9. * array 
  10. */ 
  11. void setZeros(int* matrix, int m, int n); 
  12. void printMatrix(int* matrix, int m, int n); 
  13. int run(); 
  14. }; 
  15.  
  16. #endif // __Question_1_7_h__ 
  17.  
  18. #include <iostream> 
  19. #include "Question1_7.h" 
  20.  
  21. using namespace std; 
  22.  
  23. void Question1_7::setZeros(int* matrix, int m, int n)  
  24. { 
  25. // Assuming M,N <= 32, we'll use a bit vector to represent whether a row/col should be set with zeros. 
  26. int m_rows = 0; 
  27. int m_cols = 0; 
  28.  
  29. for (int i = 0; i < m; ++i)  
  30. { 
  31. for (int j = 0; j < n; ++j)  
  32. { 
  33. if (matrix[i * n + j] == 0)  
  34. { 
  35. m_rows |= (1 << i); 
  36. m_cols |= (1 << j); 
  37. } 
  38. } 
  39. } 
  40.  
  41. for (int i = 0; i < m; ++i)  
  42. { 
  43. for (int j = 0; j < n; ++j)  
  44. { 
  45. if (((m_rows & (1 << i)) != 0) || ((m_cols & (1 << j)) != 0))  
  46. { 
  47. matrix[i * n + j] = 0; 
  48. } 
  49. } 
  50. } 
  51. } 
  52.  
  53. void Question1_7::printMatrix(int* matrix, int m, int n)  
  54. { 
  55. for (int i = 0; i < m; ++i) 
  56. { 
  57. for (int j = 0; j < n; ++j) 
  58. { 
  59. cout << matrix[i * n + j] << " "; 
  60. } 
  61.  
  62. cout << endl; 
  63. } 
  64. } 
  65.  
  66.  
  67. int Question1_7::run()  
  68. { 
  69. int matrix[4][5] ={{1, 2, 3, 4, 5}, 
  70. {6, 7, 8, 9, 10}, 
  71. {11, 12, 0, 14, 15}, 
  72. {16, 17, 18, 0, 20}}; 
  73. int* matrixPtr = (int*)matrix; 
  74. cout << "original matrix is :" << endl; 
  75. printMatrix(matrixPtr, 4, 5); 
  76.  
  77. setZeros(matrixPtr, 4, 5); 
  78. cout << "zeroed matrix is: " << endl; 
  79. printMatrix(matrixPtr, 4, 5); 
  80.  
  81. return 0; 
  82. } 

1.8

  1. #ifndef __Question_1_8_h__ 
  2. #define __Question_1_8_h__ 
  3.  
  4. #include <string> 
  5.  
  6. using std::string; 
  7.  
  8. class Question1_8  
  9. { 
  10. public: 
  11. string result(bool value); 
  12. bool isRotation(const string& s1, const string& s2); 
  13. int run(); 
  14. }; 
  15.  
  16. #endif // __Question_1_8_h__ 
  17.  
  18. #include<iostream> 
  19. #include<string> 
  20. #include "Question1_8.h" 
  21.  
  22. using namespace std; 
  23.  
  24. bool Question1_8::isRotation(const string& s1, const string& s2) 
  25. { 
  26. int len = s1.length(); 
  27.  
  28. if(len == s2.length() && len > 0)  
  29. { 
  30. string s1s1 = s1 + s1; 
  31. return s1s1.find(s2) != string::npos; 
  32. } 
  33.  
  34. return false; 
  35. } 
  36.  
  37. string Question1_8::result(bool value) 
  38. { 
  39. if (value)  
  40. { 
  41. return "True"; 
  42. } 
  43.  
  44. return "False"; 
  45. } 
  46.  
  47. int Question1_8::run()  
  48. { 
  49. string a = "apple"; 
  50. string b = "leapp"; 
  51. cout << "Checking if string: " << a << " is a rotation of string: " << b << ": " 
  52. << result(isRotation(a, b)) << endl; 
  53.  
  54. a = "james"; 
  55. b = "mesje"; 
  56. cout << "Checking if string: " << a << " is a rotation of string: " << b << ": " 
  57. << result(isRotation(a, b)) << endl; 
  58.  
  59. return 0; 
  60. }