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. public: 

  7. int run(); 

  8. bool isUniqueChars(const string& str); 

  9. bool isUniqueChars2(const string& str); 

  10. string result(bool value); 

  11. };  


  12. #endif // __Question_1_1_h__ 


  13. #include<iostream> 

  14. #include<string> 

  15. #include "Question1_1.h" 

  16. using namespace std; 


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



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



  19. return false; 




  20. unsigned int checker = 0; 


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



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


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



  24. return false; 




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




  26. return true; 




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



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



  29. return false; 




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


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



  32. int value = str[i]; 


  33. if (ascii_set[value])  



  34. return false; 




  35. ascii_set[value] = true;  




  36. return true; 




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

  38. string Question1_1::result(bool value) 



  39. if (value)  



  40. return "True"; 




  41. return "False"; 




  42. int Question1_1::run()  



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


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



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

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




  47. return 0; 



1.2

  1. #ifndef __Question_1_2_h__ 

  2. #define __Question_1_2_h__ 


  3. class Question1_2  



  4. public: 

  5. int run(); 

  6. void reverse(char* str); 

  7. };  


  8. #endif // __Question_1_2_h__ 

  9. #include<iostream> 

  10. #include "Question1_2.h" 


  11. using std::cout; 

  12. using std::endl; 


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



  14. char *ptrEnd = str; 

  15. char temp; 


  16. if (str)  



  17. while (*ptrEnd)  



  18. ptrEnd++; 



  19. ptrEnd--; 


  20. while (str < ptrEnd)  



  21. temp = *str; 

  22. *str++ = *ptrEnd; 

  23. *ptrEnd-- = temp; 








  24. int Question1_2::run()  



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


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



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

  28. reverse(input[i]); 

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




  30. return 0; 



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. public: 

  7. int run(); 

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

  9. string result(bool value); 

  10. };  


  11. #endif // __Question_1_3_B_h__ 


  12. #include<iostream> 

  13. #include<string> 

  14. #include<algorithm> 

  15. #include "Question1_3_B.h" 


  16. using namespace std; 


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



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



  19. return false; 




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


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



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

  23. ascii_set[val]++; 




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



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


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



  27. return false; 






  28. return true; 




  29. string Question1_3_B::result(bool value)  



  30. if (value)  



  31. return "True"; 




  32. return "False"; 




  33. int Question1_3_B::run()  



  34. string a = "apple"; 

  35. string b = "papel"; 


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


  37. return 0; 



1.4

  1. #ifndef __Question_1_4_h__ 

  2. #define __Question_1_4_h__ 

  3. #include <memory> 


  4. class Question1_4  



  5. public: 

  6. int run(); 

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

  8. };  


  9. #endif // __Question_1_4_h__ 


  10. #include<iostream> 

  11. #include<memory> 

  12. #include<string> 

  13. #include "Question1_4.h" 


  14. using namespace std; 


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



  16. int newLength, spaceCount = 0; 


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

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



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



  20. spaceCount++; 






  21. //calculate new string size. 

  22. newLength = length + spaceCount * 2; 

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


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

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



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



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

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

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

  30. newLength -= 3; 

  31. }  

  32. else 



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

  34. newLength--; 








  35. int Question1_4::run()  



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


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

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


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



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




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

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

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


  44. return 0; 



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. public: 

  7. int run(); 

  8. int stringToInt(const string& value); 

  9. string intToString(int value); 

  10. int countCompression(const string& str); 


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

  12. string compressBetter(const string& str); 

  13. };  


  14. #endif // __Question_1_5_h__ 


  15. #include<iostream> 

  16. #include<string> 

  17. #include<sstream> 

  18. #include "Question1_5.h" 


  19. using namespace std; 


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



  21. int temp; 

  22. stringstream(value) >> temp; 


  23. return temp; 




  24. string Question1_5::intToString(int value)  



  25. string temp; 

  26. stringstream convert; 

  27. convert << value; 

  28. temp = convert.str(); 


  29. return temp; 




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



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



  32. return 0; 




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

  34. int size = 0; 

  35. int count = 1; 


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



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



  38. count++; 



  39. else 



  40. last = str.at(i); 

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

  42. count = 1; 





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


  44. return size; 




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



  46. int size = countCompression(str); 


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



  48. return str; 




  49. string newstr; 

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

  51. int count = 1; 


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



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



  54. count++; 



  55. else 



  56. newstr += last; 

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

  58. last = str.at(i); 

  59. count = 1; 





  60. newstr += last; 

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


  62. return newstr; 




  63. int Question1_5::run()  



  64. string str = "abbccccccde"; 

  65. string newstr = compressBetter(str); 

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

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


  68. return 0; 



1.6


  1. #ifndef __Question_1_6_h__ 

  2. #define __Question_1_6_h__ 


  3. class Question1_6  



  4. public: 

  5. /** 

  6. * Since we can't provide variable matrix size in C++, we will do it "the c way" and will provide a 1-dimensional 

  7. * array 

  8. */ 

  9. void rotate(int* matrix, int n); 

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

  11. int run(); 

  12. }; 


  13. #endif // __Question_1_6_h__ 


  14. #include <iostream> 

  15. #include <memory> 

  16. #include "Question1_6.h" 


  17. using namespace std; 


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



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



  20. int first = layer; 

  21. int last = n - 1 - layer; 


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



  23. int offset = i - first; 

  24. // save top 

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


  26. // left to top 

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


  28. // bottom to left 

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


  30. // right to bottom 

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


  32. // top to right 

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








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



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



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



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




  38. cout << endl; 






  39. int Question1_6::run()  



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

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

  42. {11, 12, 13, 14, 15}, 

  43. {16, 17, 18, 19, 20}, 

  44. {21, 22, 23, 24, 25}}; 

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


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

  47. printMatrix(matrixPtr, 5, 5); 

  48. rotate(matrixPtr, 5); 

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

  50. printMatrix(matrixPtr, 5, 5); 


  51. return 0; 



1.7

  1. #ifndef __Question_1_7_h__ 

  2. #define __Question_1_7_h__ 


  3. class Question1_7  



  4. public: 

  5. /** 

  6. * Since we can't provide variable matrix size in C++, we will do it "the c way" and will provide a 1-dimensional 

  7. * array 

  8. */ 

  9. void setZeros(int* matrix, int m, int n); 

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

  11. int run(); 

  12. }; 


  13. #endif // __Question_1_7_h__ 


  14. #include <iostream> 

  15. #include "Question1_7.h" 


  16. using namespace std; 


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



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

  19. int m_rows = 0; 

  20. int m_cols = 0; 


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



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



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



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

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








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



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



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



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










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



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



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



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




  34. cout << endl; 







  35. int Question1_7::run()  



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

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

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

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

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

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

  42. printMatrix(matrixPtr, 4, 5); 


  43. setZeros(matrixPtr, 4, 5); 

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

  45. printMatrix(matrixPtr, 4, 5); 


  46. return 0; 



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. public: 

  7. string result(bool value); 

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

  9. int run(); 

  10. }; 


  11. #endif // __Question_1_8_h__ 


  12. #include<iostream> 

  13. #include<string> 

  14. #include "Question1_8.h" 


  15. using namespace std; 


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



  17. int len = s1.length(); 


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



  19. string s1s1 = s1 + s1; 

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




  21. return false; 




  22. string Question1_8::result(bool value) 



  23. if (value)  



  24. return "True"; 




  25. return "False"; 




  26. int Question1_8::run()  



  27. string a = "apple"; 

  28. string b = "leapp"; 

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

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


  31. a = "james"; 

  32. b = "mesje"; 

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

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


  35. return 0; 



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. python3 第十七章 - sequence(序列)

    之前我们在讲for循环语句时就提到过序列,那么什么是序列(sequence)? 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 —— 它的索引(位置),第一个索引是0,第二个索引 ...

  2. synchronized内存可见性理解

    一.背景 最近在看<Java并发编程实战>这本书,看到共享变量的可见性,其中说到"加锁的含义不仅仅局限于互斥行为,还包括内存可见性". 我对于内存可见性第一反应是vol ...

  3. maven插件本地化安装

    mvn install:install-file -Dfile="D:\maven\repository\com\tc\itfarm-api\1.0.0-SNAPSHOT\itfarm-ap ...

  4. IO (二)

    1 字符流的缓冲区 缓冲区的出现提高了对数据的读写效率. 对应的类: BufferedWriter BufferedReader 缓冲区要结合流才能使用. 在流的基础上对流的功能进行了增强. 2 Bu ...

  5. css盒子居中定位问题

    在HTML中,div盒子的居中要通过外边距margin和width来控制,首先确定盒子的宽度,然后确定盒子方位并将其平移便可使盒子移到固定位置. <div id="divpic&quo ...

  6. poj 1423 打表/斯特林公式

    对于n位数的计算,我们可以采用(int)log10(n) + 1的方法得到n的位数 第一种方法: 对于n!位数的计算,log10(n!) = log10(1) + log10(2) + ... + l ...

  7. 精通libGDX游戏开发-RPG实战-欢迎来到RPG的世界

    欢迎来到RPG的世界 本章我会快速的使用tiled这样的瓷砖地图工具,来带领大家创造所设想的世界. 创建并编辑瓷砖地图 瓷砖地图(tile-based map)是广泛应用于各种游戏类型的地图格式,li ...

  8. React 16.3来了:带着全新的Context API

    文章概览 React在版本16.3-alpha里引入了新的Context API,社区一片期待之声.我们先通过简单的例子,看下新的Context API长啥样,然后再简单探讨下新的API的意义. 文中 ...

  9. Android+TensorFlow+CNN+MNIST 手写数字识别实现

    Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...

  10. BZOJ 1969: [Ahoi2005]LANE 航线规划 [树链剖分 时间倒流]

    题意: 一张图,删除边,求两点之间的割边数量.保证任意时刻图连通 任求一棵生成树,只有树边可能是割边 时间倒流,加入一条边,就是两点路径上的边都不可能是割边,区间覆盖... 然后本题需要把边哈希一下, ...