crack the coding interview

answer
c++

1.1

  1. #ifndef __Question_1_1_h__ 

  2. #define __Question_1_1_h__ 


  3. #include <string> 


  4. using std::string; 


  5. class Question1_1  

  6. { 

  7. public: 

  8. int run(); 

  9. bool isUniqueChars(const string& str); 

  10. bool isUniqueChars2(const string& str); 

  11. string result(bool value); 

  12. };  


  13. #endif // __Question_1_1_h__ 


  14. #include<iostream> 

  15. #include<string> 

  16. #include "Question1_1.h" 

  17. using namespace std; 


  18. bool Question1_1::isUniqueChars(const string& str) 

  19. { 

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

  21. { 

  22. return false; 

  23. } 


  24. unsigned int checker = 0; 


  25. for (int i = 0; i < str.length(); ++i)  

  26. { 

  27. int value = str[i] - 'a'; 


  28. if ((checker & (1 << value)) != 0) 

  29. { 

  30. return false; 

  31. } 


  32. checker |= (1 << value); 

  33. } 


  34. return true; 

  35. } 


  36. bool Question1_1::isUniqueChars2(const string& str)  

  37. { 

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

  39. { 

  40. return false; 

  41. } 


  42. bool ascii_set[256] = { false }; 


  43. for (int i = 0; i < str.length(); ++i)  

  44. { 

  45. int value = str[i]; 


  46. if (ascii_set[value])  

  47. { 

  48. return false; 

  49. } 


  50. ascii_set[value] = true;  

  51. } 


  52. return true; 

  53. } 


  54. /* Function to print true and false */ 

  55. string Question1_1::result(bool value) 

  56. { 

  57. if (value)  

  58. { 

  59. return "True"; 

  60. } 


  61. return "False"; 

  62. } 


  63. int Question1_1::run()  

  64. { 

  65. string input[] ={"abcde", "aba"}; 


  66. for (int i = 0; i < 2; i++)  

  67. { 

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

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

  70. } 


  71. return 0; 

  72. } 

1.2

  1. #ifndef __Question_1_2_h__ 

  2. #define __Question_1_2_h__ 


  3. class Question1_2  

  4. { 

  5. public: 

  6. int run(); 

  7. void reverse(char* str); 

  8. };  


  9. #endif // __Question_1_2_h__ 

  10. #include<iostream> 

  11. #include "Question1_2.h" 


  12. using std::cout; 

  13. using std::endl; 


  14. void Question1_2::reverse(char* str)  

  15. { 

  16. char *ptrEnd = str; 

  17. char temp; 


  18. if (str)  

  19. { 

  20. while (*ptrEnd)  

  21. { 

  22. ptrEnd++; 

  23. } 

  24. ptrEnd--; 


  25. while (str < ptrEnd)  

  26. { 

  27. temp = *str; 

  28. *str++ = *ptrEnd; 

  29. *ptrEnd-- = temp; 

  30. } 

  31. } 

  32. } 


  33. int Question1_2::run()  

  34. { 

  35. char input[][10] = { "abcde", "cat" }; 


  36. for (int i = 0; i < 2; i++)  

  37. { 

  38. cout << "reversing the string: " << input[i] << endl; 

  39. reverse(input[i]); 

  40. cout << "reverse of input string is " << input[i] << endl; 

  41. } 


  42. return 0; 

  43. } 

1.3

  1. #ifndef __Question_1_3_B_h__ 

  2. #define __Question_1_3_B_h__ 


  3. #include <string> 


  4. using std::string; 


  5. class Question1_3_B  

  6. { 

  7. public: 

  8. int run(); 

  9. bool permutation(const string& a, const string& b); 

  10. string result(bool value); 

  11. };  


  12. #endif // __Question_1_3_B_h__ 


  13. #include<iostream> 

  14. #include<string> 

  15. #include<algorithm> 

  16. #include "Question1_3_B.h" 


  17. using namespace std; 


  18. bool Question1_3_B::permutation(const string& a, const string& b)  

  19. { 

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

  21. { 

  22. return false; 

  23. } 


  24. int ascii_set[256] = {0}; 


  25. for (int i = 0; i < a.length(); i++)  

  26. { 

  27. int val = static_cast<int>(a[i]); 

  28. ascii_set[val]++; 

  29. } 


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

  31. { 

  32. int val = static_cast<int>(b[i]); 


  33. if ((--ascii_set[val]) < 0)  

  34. { 

  35. return false; 

  36. } 

  37. } 


  38. return true; 

  39. } 


  40. string Question1_3_B::result(bool value)  

  41. { 

  42. if (value)  

  43. { 

  44. return "True"; 

  45. } 


  46. return "False"; 

  47. } 


  48. int Question1_3_B::run()  

  49. { 

  50. string a = "apple"; 

  51. string b = "papel"; 


  52. cout << "Result for " << a << " and " << b << " is " << result(permutation(a, b)) << endl; 


  53. return 0; 

  54. } 

1.4

  1. #ifndef __Question_1_4_h__ 

  2. #define __Question_1_4_h__ 

  3. #include <memory> 


  4. class Question1_4  

  5. { 

  6. public: 

  7. int run(); 

  8. void replaceSpaces(std::unique_ptr<char[]>&, int length); 

  9. };  


  10. #endif // __Question_1_4_h__ 


  11. #include<iostream> 

  12. #include<memory> 

  13. #include<string> 

  14. #include "Question1_4.h" 


  15. using namespace std; 


  16. void Question1_4::replaceSpaces(unique_ptr<char[]> &str, int length) //char str[] 

  17. { 

  18. int newLength, spaceCount = 0; 


  19. //count the number of spaces in the given string. 

  20. for (int i = 0; i < length; i++)  

  21. { 

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

  23. { 

  24. spaceCount++; 

  25. } 

  26. } 


  27. //calculate new string size. 

  28. newLength = length + spaceCount * 2; 

  29. str[newLength] = '\0'; 


  30. //copying the characters backwards and inserting %20 

  31. for (int i = length - 1; i >= 0; i--)  

  32. { 

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

  34. { 

  35. str[newLength - 1] = '0'; 

  36. str[newLength - 2] = '2'; 

  37. str[newLength - 3] = '%'; 

  38. newLength -= 3; 

  39. }  

  40. else 

  41. { 

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

  43. newLength--; 

  44. } 

  45. } 

  46. } 


  47. int Question1_4::run()  

  48. { 

  49. string str = "abc d e f"; 


  50. // Increasing length of the string to meet question requirement of 'true' length by using char array. (Note: using a unique_ptr here) 

  51. auto newStr = make_unique<char[]>(str.length() + 3 * 2 + 1); 


  52. for (int i = 0; i < str.length(); i++)  

  53. { 

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

  55. } 


  56. cout << "Original string is " << str << endl; 

  57. replaceSpaces(newStr, str.length()); 

  58. cout << "New string with %20 is " << newStr.get() << endl; 


  59. return 0; 

  60. } 

1.5

  1. #ifndef __Question_1_5_h__ 

  2. #define __Question_1_5_h__ 


  3. #include <string> 


  4. using std::string; 


  5. class Question1_5  

  6. { 

  7. public: 

  8. int run(); 

  9. int stringToInt(const string& value); 

  10. string intToString(int value); 

  11. int countCompression(const string& str); 


  12. /// C++ std::string is efficient and no need to use a "StringBuffer"-like structure. 

  13. string compressBetter(const string& str); 

  14. };  


  15. #endif // __Question_1_5_h__ 


  16. #include<iostream> 

  17. #include<string> 

  18. #include<sstream> 

  19. #include "Question1_5.h" 


  20. using namespace std; 


  21. int Question1_5::stringToInt(const string& value)  

  22. { 

  23. int temp; 

  24. stringstream(value) >> temp; 


  25. return temp; 

  26. } 


  27. string Question1_5::intToString(int value)  

  28. { 

  29. string temp; 

  30. stringstream convert; 

  31. convert << value; 

  32. temp = convert.str(); 


  33. return temp; 

  34. } 


  35. int Question1_5::countCompression(const string& str) 

  36. { 

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

  38. { 

  39. return 0; 

  40. } 


  41. char last = str.at(0); 

  42. int size = 0; 

  43. int count = 1; 


  44. for (int i = 1; i < str.length(); i++)  

  45. { 

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

  47. { 

  48. count++; 

  49. } 

  50. else 

  51. { 

  52. last = str.at(i); 

  53. size = size + 1 + intToString(count).length(); 

  54. count = 1; 

  55. } 

  56. } 

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


  58. return size; 

  59. } 


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

  61. { 

  62. int size = countCompression(str); 


  63. if(size >= str.length())  

  64. { 

  65. return str; 

  66. } 


  67. string newstr; 

  68. char last = str.at(0); 

  69. int count = 1; 


  70. for (int i = 1; i < str.length(); i++)  

  71. { 

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

  73. { 

  74. count++; 

  75. } 

  76. else 

  77. { 

  78. newstr += last; 

  79. newstr.append(intToString(count)); 

  80. last = str.at(i); 

  81. count = 1; 

  82. } 

  83. } 

  84. newstr += last; 

  85. newstr.append(intToString(count)); 


  86. return newstr; 

  87. } 


  88. int Question1_5::run()  

  89. { 

  90. string str = "abbccccccde"; 

  91. string newstr = compressBetter(str); 

  92. cout << "Original string is " << str << endl; 

  93. cout << "Compressed string is " << newstr << endl; 


  94. return 0; 

  95. } 

1.6


  1. #ifndef __Question_1_6_h__ 

  2. #define __Question_1_6_h__ 


  3. class Question1_6  

  4. { 

  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 rotate(int* matrix, int n); 

  11. void printMatrix(int* matrix, int m, int n); 

  12. int run(); 

  13. }; 


  14. #endif // __Question_1_6_h__ 


  15. #include <iostream> 

  16. #include <memory> 

  17. #include "Question1_6.h" 


  18. using namespace std; 


  19. void Question1_6::rotate(int* matrix, int n)  

  20. { 

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

  22. { 

  23. int first = layer; 

  24. int last = n - 1 - layer; 


  25. for (int i = first; i < last; ++i)  

  26. { 

  27. int offset = i - first; 

  28. // save top 

  29. int top = matrix[first * n + i]; 


  30. // left to top 

  31. matrix[first * n + i] = matrix[(last-offset) * n + first]; 


  32. // bottom to left 

  33. matrix[(last-offset) * n + first] = matrix[last * n + (last-offset)]; 


  34. // right to bottom 

  35. matrix[last * n + (last-offset)] = matrix[i * n + last]; 


  36. // top to right 

  37. matrix[i * n + last] = top; 

  38. } 

  39. } 

  40. } 


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

  42. { 

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

  44. { 

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

  46. { 

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

  48. } 


  49. cout << endl; 

  50. } 

  51. } 


  52. int Question1_6::run()  

  53. { 

  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. cout << "original matrix is :" << endl; 

  61. printMatrix(matrixPtr, 5, 5); 

  62. rotate(matrixPtr, 5); 

  63. cout << "rotated matrix is: " << endl; 

  64. printMatrix(matrixPtr, 5, 5); 


  65. return 0; 

  66. } 

1.7

  1. #ifndef __Question_1_7_h__ 

  2. #define __Question_1_7_h__ 


  3. class Question1_7  

  4. { 

  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. #endif // __Question_1_7_h__ 


  15. #include <iostream> 

  16. #include "Question1_7.h" 


  17. using namespace std; 


  18. void Question1_7::setZeros(int* matrix, int m, int n)  

  19. { 

  20. // Assuming M,N <= 32, we'll use a bit vector to represent whether a row/col should be set with zeros. 

  21. int m_rows = 0; 

  22. int m_cols = 0; 


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

  24. { 

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

  26. { 

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

  28. { 

  29. m_rows |= (1 << i); 

  30. m_cols |= (1 << j); 

  31. } 

  32. } 

  33. } 


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

  35. { 

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

  37. { 

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

  39. { 

  40. matrix[i * n + j] = 0; 

  41. } 

  42. } 

  43. } 

  44. } 


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

  46. { 

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

  48. { 

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

  50. { 

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

  52. } 


  53. cout << endl; 

  54. } 

  55. } 



  56. int Question1_7::run()  

  57. { 

  58. int matrix[4][5] ={{1, 2, 3, 4, 5}, 

  59. {6, 7, 8, 9, 10}, 

  60. {11, 12, 0, 14, 15}, 

  61. {16, 17, 18, 0, 20}}; 

  62. int* matrixPtr = (int*)matrix; 

  63. cout << "original matrix is :" << endl; 

  64. printMatrix(matrixPtr, 4, 5); 


  65. setZeros(matrixPtr, 4, 5); 

  66. cout << "zeroed matrix is: " << endl; 

  67. printMatrix(matrixPtr, 4, 5); 


  68. return 0; 

  69. } 

1.8

  1. #ifndef __Question_1_8_h__ 

  2. #define __Question_1_8_h__ 


  3. #include <string> 


  4. using std::string; 


  5. class Question1_8  

  6. { 

  7. public: 

  8. string result(bool value); 

  9. bool isRotation(const string& s1, const string& s2); 

  10. int run(); 

  11. }; 


  12. #endif // __Question_1_8_h__ 


  13. #include<iostream> 

  14. #include<string> 

  15. #include "Question1_8.h" 


  16. using namespace std; 


  17. bool Question1_8::isRotation(const string& s1, const string& s2) 

  18. { 

  19. int len = s1.length(); 


  20. if(len == s2.length() && len > 0)  

  21. { 

  22. string s1s1 = s1 + s1; 

  23. return s1s1.find(s2) != string::npos; 

  24. } 


  25. return false; 

  26. } 


  27. string Question1_8::result(bool value) 

  28. { 

  29. if (value)  

  30. { 

  31. return "True"; 

  32. } 


  33. return "False"; 

  34. } 


  35. int Question1_8::run()  

  36. { 

  37. string a = "apple"; 

  38. string b = "leapp"; 

  39. cout << "Checking if string: " << a << " is a rotation of string: " << b << ": " 

  40. << result(isRotation(a, b)) << endl; 


  41. a = "james"; 

  42. b = "mesje"; 

  43. cout << "Checking if string: " << a << " is a rotation of string: " << b << ": " 

  44. << result(isRotation(a, b)) << endl; 


  45. return 0; 

  46. } 

crack the coding interview的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. 转:Top 10 Algorithms for Coding Interview

    The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...

  3. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  4. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  5. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  6. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  7. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  8. Python Coding Interview

    Python Coding Interview Python Advanced Use enumerate() to iterate over both indices and values Debu ...

  9. whiteboard & coding interview practice

    whiteboard & coding interview practice 白板 & 面试 & 编码练习 Algorithm https://www.freecodecamp ...

随机推荐

  1. 使用WinDbg获取SSDT函数表对应的索引再计算得出地址

    当从Ring3进入Ring0的时候会将所需要的SSDT索引放入到寄存器EAX中去,所以我们这里通过EAX的内容得到函数在SSDT中的索引号,然后计算出它的地址首先打开WinDbug,我们以函数ZwQu ...

  2. visual studio 中无法删除项目或者文件

    vs 2012添加新项目或者类库后,里边的class文件,我不想要,就把它删除.但是删除的时候,报如下图的错误,我删除新建的项目或类库的时候,也报类似的错误,怎么解决? window系统是新安装的.也 ...

  3. arduino扩展IO与M74HC595B芯片的使用,挪车电话提示牌的设计

    2018-01-0915:39:24 视频连接 首先arduino中shiftOUT()函数的定义与说明! shiftOut()描述将一个数据的一个字节一位一位的移出.从最高有效位(最左边)或最低有效 ...

  4. linux下安装python3

    不建议卸载python2 可能会导致系统内其他软件无法使用 1.下载 wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0a1.tar.x ...

  5. grails项目中(DB的相关操作)

    grails项目中(DB的相关操作) save:保存Domain对象的数据到对应的库表中(可能是insert也可能是update) findBy: 动态方法,查找并返回第一条记录,方法名可以变化 eg ...

  6. thinkphp5踩坑之部署到服务器模板不存在

    一个项目部署到Linux服务器上去的时候,发现某些模板竟然会报错说"模板不存在:/Application/Admin/-.", 解决方法:网上有说是因为使用$this->fe ...

  7. Android自定义View的套路

    一.自定义View的流程 1.属性设置 在styles.xml中设置控件属性,如果你想直接harcode可以忽略这步 <!--name为声明的"属性集合"名,可以随便取,但是 ...

  8. Tomcat(五):nginx/httpd + tomcat及负载均衡tomcat

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  9. Java语言程序设计基础篇第10版第5章习题答案

    5.1 1 public class Demo { public static void main(String[] args) { // 创建一个输入对象 java.util.Scanner inp ...

  10. ldconfig几个需要注意的地方

    1. 往/lib和/usr/lib里面加东西,是不用修改/etc/ld.so.conf的,但是完了之后要调一下ldconfig,不然这个library会找不到 2. 想往上面两个目录以外加东西的时候, ...