首页 > 技术文章 > crack the coding interview

forest-wow 2017-04-25 11:05 原文

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. public
  10. int run()
  11. bool isUniqueChars(const string& str)
  12. bool isUniqueChars2(const string& str)
  13. string result(bool value)
  14. };  
  15.  
  16. #endif // __Question_1_1_h__ 
  17.  
  18. #include<iostream> 
  19. #include<string> 
  20. #include "Question1_1.h" 
  21. using namespace std
  22.  
  23. bool Question1_1::isUniqueChars(const string& str) 

  24. if (str.length() > 256)  

  25. return false

  26.  
  27. unsigned int checker = 0
  28.  
  29. for (int i = 0; i < str.length(); ++i)  

  30. int value = str[i] - 'a'
  31.  
  32. if ((checker & (1 << value)) != 0

  33. return false

  34.  
  35. checker |= (1 << value); 

  36.  
  37. return true

  38.  
  39. bool Question1_1::isUniqueChars2(const string& str)  

  40. if (str.length() > 256)  

  41. return false

  42.  
  43. bool ascii_set[256] = { false }; 
  44.  
  45. for (int i = 0; i < str.length(); ++i)  

  46. int value = str[i]; 
  47.  
  48. if (ascii_set[value])  

  49. return false

  50.  
  51. ascii_set[value] = true;  

  52.  
  53. return true

  54.  
  55. /* Function to print true and false */ 
  56. string Question1_1::result(bool value) 

  57. if (value)  

  58. return "True"

  59.  
  60. return "False"

  61.  
  62. int Question1_1::run()  

  63. string input[] ={"abcde", "aba"}; 
  64.  
  65. for (int i = 0; i < 2; i++)  

  66. cout << input[i] << " has unique characters: " << result(isUniqueChars(input[i])) << endl; 
  67. cout << input[i] << " has unique characters: " << result(isUniqueChars2(input[i])) << endl; 

  68.  
  69. return 0

1.2

  1. #ifndef __Question_1_2_h__ 
  2. #define __Question_1_2_h__ 
  3.  
  4. class Question1_2  

  5. public
  6. int run()
  7. void reverse(char* str)
  8. };  
  9.  
  10. #endif // __Question_1_2_h__ 
  11. #include<iostream> 
  12. #include "Question1_2.h" 
  13.  
  14. using std::cout
  15. using std::endl; 
  16.  
  17. void Question1_2::reverse(char* str)  

  18. char *ptrEnd = str; 
  19. char temp; 
  20.  
  21. if (str)  

  22. while (*ptrEnd)  

  23. ptrEnd++; 

  24. ptrEnd--; 
  25.  
  26. while (str < ptrEnd)  

  27. temp = *str; 
  28. *str++ = *ptrEnd; 
  29. *ptrEnd-- = temp; 



  30.  
  31. int Question1_2::run()  

  32. char input[][10] = { "abcde", "cat" }; 
  33.  
  34. for (int i = 0; i < 2; i++)  

  35. cout << "reversing the string: " << input[i] << endl; 
  36. reverse(input[i]); 
  37. cout << "reverse of input string is " << input[i] << endl; 

  38.  
  39. return 0

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. public
  10. int run()
  11. bool permutation(const string& a, const string& b)
  12. string result(bool value)
  13. };  
  14.  
  15. #endif // __Question_1_3_B_h__ 
  16.  
  17. #include<iostream> 
  18. #include<string> 
  19. #include<algorithm> 
  20. #include "Question1_3_B.h" 
  21.  
  22. using namespace std
  23.  
  24. bool Question1_3_B::permutation(const string& a, const string& b)  

  25. if (a.length() != b.length())  

  26. return false

  27.  
  28. int ascii_set[256] = {0}; 
  29.  
  30. for (int i = 0; i < a.length(); i++)  

  31. int val = static_cast<int>(a[i]); 
  32. ascii_set[val]++; 

  33.  
  34. for (int i = 0; i < b.length(); i++)  

  35. int val = static_cast<int>(b[i]); 
  36.  
  37. if ((--ascii_set[val]) < 0)  

  38. return false


  39.  
  40. return true

  41.  
  42. string Question1_3_B::result(bool value)  

  43. if (value)  

  44. return "True"

  45.  
  46. return "False"

  47.  
  48. int Question1_3_B::run()  

  49. string a = "apple"
  50. string b = "papel"
  51.  
  52. cout << "Result for " << a << " and " << b << " is " << result(permutation(a, b)) << endl; 
  53.  
  54. return 0

1.4

  1. #ifndef __Question_1_4_h__ 
  2. #define __Question_1_4_h__ 
  3. #include <memory> 
  4.  
  5. class Question1_4  

  6. public
  7. int run()
  8. void replaceSpaces(std::unique_ptr<char[]>&, int length)
  9. };  
  10.  
  11. #endif // __Question_1_4_h__ 
  12.  
  13. #include<iostream> 
  14. #include<memory> 
  15. #include<string> 
  16. #include "Question1_4.h" 
  17.  
  18. using namespace std
  19.  
  20. void Question1_4::replaceSpaces(unique_ptr<char[]> &str, int length) //char str[] 

  21. int newLength, spaceCount = 0
  22.  
  23. //count the number of spaces in the given string. 
  24. for (int i = 0; i < length; i++)  

  25. if (str[i] == ' ')  

  26. spaceCount++; 


  27.  
  28. //calculate new string size. 
  29. newLength = length + spaceCount * 2
  30. str[newLength] = '\0'
  31.  
  32. //copying the characters backwards and inserting %20 
  33. for (int i = length - 1; i >= 0; i--)  

  34. if (str[i] == ' ')  

  35. str[newLength - 1] = '0'
  36. str[newLength - 2] = '2'
  37. str[newLength - 3] = '%'
  38. newLength -= 3
  39. }  
  40. else 

  41. str[newLength - 1] = str[i]; 
  42. newLength--; 



  43.  
  44. int Question1_4::run()  

  45. string str = "abc d e f"
  46.  
  47. // Increasing length of the string to meet question requirement of 'true' length by using char array. (Note: using a unique_ptr here) 
  48. auto newStr = make_unique<char[]>(str.length() + 3 * 2 + 1); 
  49.  
  50. for (int i = 0; i < str.length(); i++)  

  51. newStr[i] = str[i]; 

  52.  
  53. cout << "Original string is " << str << endl; 
  54. replaceSpaces(newStr, str.length()); 
  55. cout << "New string with %20 is " << newStr.get() << endl; 
  56.  
  57. return 0

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. public
  10. int run()
  11. int stringToInt(const string& value)
  12. string intToString(int value)
  13. int countCompression(const string& str)
  14.  
  15. /// C++ std::string is efficient and no need to use a "StringBuffer"-like structure. 
  16. string compressBetter(const string& str)
  17. };  
  18.  
  19. #endif // __Question_1_5_h__ 
  20.  
  21. #include<iostream> 
  22. #include<string> 
  23. #include<sstream> 
  24. #include "Question1_5.h" 
  25.  
  26. using namespace std
  27.  
  28. int Question1_5::stringToInt(const string& value)  

  29. int temp; 
  30. stringstream(value) >> temp; 
  31.  
  32. return temp; 

  33.  
  34. string Question1_5::intToString(int value)  

  35. string temp; 
  36. stringstream convert; 
  37. convert << value; 
  38. temp = convert.str(); 
  39.  
  40. return temp; 

  41.  
  42. int Question1_5::countCompression(const string& str) 

  43. if (str.length() == 0)  

  44. return 0

  45.  
  46. char last = str.at(0); 
  47. int size = 0
  48. int count = 1
  49.  
  50. for (int i = 1; i < str.length(); i++)  

  51. if (str.at(i) == last)  

  52. count++; 

  53. else 

  54. last = str.at(i); 
  55. size = size + 1 + intToString(count).length(); 
  56. count = 1


  57. size = size + 1 + intToString(count).length(); 
  58.  
  59. return size; 

  60.  
  61. string Question1_5::compressBetter(const string& str)  

  62. int size = countCompression(str); 
  63.  
  64. if(size >= str.length())  

  65. return str; 

  66.  
  67. string newstr; 
  68. char last = str.at(0); 
  69. int count = 1
  70.  
  71. for (int i = 1; i < str.length(); i++)  

  72. if (str.at(i) == last)  

  73. count++; 

  74. else 

  75. newstr += last; 
  76. newstr.append(intToString(count)); 
  77. last = str.at(i); 
  78. count = 1


  79. newstr += last; 
  80. newstr.append(intToString(count)); 
  81.  
  82. return newstr; 

  83.  
  84. int Question1_5::run()  

  85. string str = "abbccccccde"
  86. string newstr = compressBetter(str); 
  87. cout << "Original string is " << str << endl; 
  88. cout << "Compressed string is " << newstr << endl; 
  89.  
  90. return 0

1.6

  1.  
  2. #ifndef __Question_1_6_h__ 
  3. #define __Question_1_6_h__ 
  4.  
  5. class Question1_6  

  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 rotate(int* matrix, int n)
  12. void printMatrix(int* matrix, int m, int n)
  13. int run()
  14. }; 
  15.  
  16. #endif // __Question_1_6_h__ 
  17.  
  18. #include <iostream> 
  19. #include <memory> 
  20. #include "Question1_6.h" 
  21.  
  22. using namespace std
  23.  
  24. void Question1_6::rotate(int* matrix, int n)  

  25. for (int layer = 0; layer < n / 2; ++layer)  

  26. int first = layer; 
  27. int last = n - 1 - layer; 
  28.  
  29. for (int i = first; i < last; ++i)  

  30. int offset = i - first; 
  31. // save top 
  32. int top = matrix[first * n + i]; 
  33.  
  34. // left to top 
  35. matrix[first * n + i] = matrix[(last-offset) * n + first]; 
  36.  
  37. // bottom to left 
  38. matrix[(last-offset) * n + first] = matrix[last * n + (last-offset)]; 
  39.  
  40. // right to bottom 
  41. matrix[last * n + (last-offset)] = matrix[i * n + last]; 
  42.  
  43. // top to right 
  44. matrix[i * n + last] = top; 



  45.  
  46. void Question1_6::printMatrix(int* matrix, int m, int n)  

  47. for (int i = 0; i < m; ++i)  

  48. for (int j = 0; j < n; ++j)  

  49. cout << matrix[i * n + j] << " "

  50.  
  51. cout << endl; 


  52.  
  53. int Question1_6::run()  

  54. int matrix[][5] ={{1, 2, 3, 4, 5}, 
  55. {6, 7, 8, 9, 10}, 
  56. {11, 12, 13, 14, 15}, 
  57. {16, 17, 18, 19, 20}, 
  58. {21, 22, 23, 24, 25}}; 
  59. int* matrixPtr = (int*)matrix; 
  60.  
  61. cout << "original matrix is :" << endl; 
  62. printMatrix(matrixPtr, 5, 5); 
  63. rotate(matrixPtr, 5); 
  64. cout << "rotated matrix is: " << endl; 
  65. printMatrix(matrixPtr, 5, 5); 
  66.  
  67. return 0

1.7

  1. #ifndef __Question_1_7_h__ 
  2. #define __Question_1_7_h__ 
  3.  
  4. class Question1_7  

  5. public
  6. /** 
  7. * Since we can't provide variable matrix size in C++, we will do it "the c way" and will provide a 1-dimensional 
  8. * array 
  9. */ 
  10. void setZeros(int* matrix, int m, int n)
  11. void printMatrix(int* matrix, int m, int n)
  12. int run()
  13. }; 
  14.  
  15. #endif // __Question_1_7_h__ 
  16.  
  17. #include <iostream> 
  18. #include "Question1_7.h" 
  19.  
  20. using namespace std
  21.  
  22. void Question1_7::setZeros(int* matrix, int m, int n)  

  23. // Assuming M,N <= 32, we'll use a bit vector to represent whether a row/col should be set with zeros. 
  24. int m_rows = 0
  25. int m_cols = 0
  26.  
  27. for (int i = 0; i < m; ++i)  

  28. for (int j = 0; j < n; ++j)  

  29. if (matrix[i * n + j] == 0)  

  30. m_rows |= (1 << i); 
  31. m_cols |= (1 << j); 



  32.  
  33. for (int i = 0; i < m; ++i)  

  34. for (int j = 0; j < n; ++j)  

  35. if (((m_rows & (1 << i)) != 0) || ((m_cols & (1 << j)) != 0))  

  36. matrix[i * n + j] = 0




  37.  
  38. void Question1_7::printMatrix(int* matrix, int m, int n)  

  39. for (int i = 0; i < m; ++i) 

  40. for (int j = 0; j < n; ++j) 

  41. cout << matrix[i * n + j] << " "

  42.  
  43. cout << endl; 


  44.  
  45.  
  46. int Question1_7::run()  

  47. int matrix[4][5] ={{1, 2, 3, 4, 5}, 
  48. {6, 7, 8, 9, 10}, 
  49. {11, 12, 0, 14, 15}, 
  50. {16, 17, 18, 0, 20}}; 
  51. int* matrixPtr = (int*)matrix; 
  52. cout << "original matrix is :" << endl; 
  53. printMatrix(matrixPtr, 4, 5); 
  54.  
  55. setZeros(matrixPtr, 4, 5); 
  56. cout << "zeroed matrix is: " << endl; 
  57. printMatrix(matrixPtr, 4, 5); 
  58.  
  59. return 0

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. public
  10. string result(bool value)
  11. bool isRotation(const string& s1, const string& s2)
  12. int run()
  13. }; 
  14.  
  15. #endif // __Question_1_8_h__ 
  16.  
  17. #include<iostream> 
  18. #include<string> 
  19. #include "Question1_8.h" 
  20.  
  21. using namespace std
  22.  
  23. bool Question1_8::isRotation(const string& s1, const string& s2) 

  24. int len = s1.length(); 
  25.  
  26. if(len == s2.length() && len > 0)  

  27. string s1s1 = s1 + s1; 
  28. return s1s1.find(s2) != string::npos; 

  29.  
  30. return false

  31.  
  32. string Question1_8::result(bool value) 

  33. if (value)  

  34. return "True"

  35.  
  36. return "False"

  37.  
  38. int Question1_8::run()  

  39. string a = "apple"
  40. string b = "leapp"
  41. cout << "Checking if string: " << a << " is a rotation of string: " << b << ": " 
  42. << result(isRotation(a, b)) << endl; 
  43.  
  44. a = "james"
  45. b = "mesje"
  46. cout << "Checking if string: " << a << " is a rotation of string: " << b << ": " 
  47. << result(isRotation(a, b)) << endl; 
  48.  
  49. return 0

推荐阅读