crack the coding interview
crack the coding interview
1.1
- #ifndef __Question_1_1_h__
- #define __Question_1_1_h__
- #include <string>
- using std::string;
- class Question1_1
- {
- public:
- int run();
- bool isUniqueChars(const string& str);
- bool isUniqueChars2(const string& str);
- string result(bool value);
- };
- #endif // __Question_1_1_h__
- #include<iostream>
- #include<string>
- #include "Question1_1.h"
- using namespace std;
- bool Question1_1::isUniqueChars(const string& str)
- {
- if (str.length() > 256)
- {
- return false;
- }
- unsigned int checker = 0;
- for (int i = 0; i < str.length(); ++i)
- {
- int value = str[i] - 'a';
- if ((checker & (1 << value)) != 0)
- {
- return false;
- }
- checker |= (1 << value);
- }
- return true;
- }
- bool Question1_1::isUniqueChars2(const string& str)
- {
- if (str.length() > 256)
- {
- return false;
- }
- bool ascii_set[256] = { false };
- for (int i = 0; i < str.length(); ++i)
- {
- int value = str[i];
- if (ascii_set[value])
- {
- return false;
- }
- ascii_set[value] = true;
- }
- return true;
- }
- /* Function to print true and false */
- string Question1_1::result(bool value)
- {
- if (value)
- {
- return "True";
- }
- return "False";
- }
- int Question1_1::run()
- {
- string input[] ={"abcde", "aba"};
- for (int i = 0; i < 2; i++)
- {
- cout << input[i] << " has unique characters: " << result(isUniqueChars(input[i])) << endl;
- cout << input[i] << " has unique characters: " << result(isUniqueChars2(input[i])) << endl;
- }
- return 0;
- }
1.2
- #ifndef __Question_1_2_h__
- #define __Question_1_2_h__
- class Question1_2
- {
- public:
- int run();
- void reverse(char* str);
- };
- #endif // __Question_1_2_h__
- #include<iostream>
- #include "Question1_2.h"
- using std::cout;
- using std::endl;
- void Question1_2::reverse(char* str)
- {
- char *ptrEnd = str;
- char temp;
- if (str)
- {
- while (*ptrEnd)
- {
- ptrEnd++;
- }
- ptrEnd--;
- while (str < ptrEnd)
- {
- temp = *str;
- *str++ = *ptrEnd;
- *ptrEnd-- = temp;
- }
- }
- }
- int Question1_2::run()
- {
- char input[][10] = { "abcde", "cat" };
- for (int i = 0; i < 2; i++)
- {
- cout << "reversing the string: " << input[i] << endl;
- reverse(input[i]);
- cout << "reverse of input string is " << input[i] << endl;
- }
- return 0;
- }
1.3
- #ifndef __Question_1_3_B_h__
- #define __Question_1_3_B_h__
- #include <string>
- using std::string;
- class Question1_3_B
- {
- public:
- int run();
- bool permutation(const string& a, const string& b);
- string result(bool value);
- };
- #endif // __Question_1_3_B_h__
- #include<iostream>
- #include<string>
- #include<algorithm>
- #include "Question1_3_B.h"
- using namespace std;
- bool Question1_3_B::permutation(const string& a, const string& b)
- {
- if (a.length() != b.length())
- {
- return false;
- }
- int ascii_set[256] = {0};
- for (int i = 0; i < a.length(); i++)
- {
- int val = static_cast<int>(a[i]);
- ascii_set[val]++;
- }
- for (int i = 0; i < b.length(); i++)
- {
- int val = static_cast<int>(b[i]);
- if ((--ascii_set[val]) < 0)
- {
- return false;
- }
- }
- return true;
- }
- string Question1_3_B::result(bool value)
- {
- if (value)
- {
- return "True";
- }
- return "False";
- }
- int Question1_3_B::run()
- {
- string a = "apple";
- string b = "papel";
- cout << "Result for " << a << " and " << b << " is " << result(permutation(a, b)) << endl;
- return 0;
- }
1.4
- #ifndef __Question_1_4_h__
- #define __Question_1_4_h__
- #include <memory>
- class Question1_4
- {
- public:
- int run();
- void replaceSpaces(std::unique_ptr<char[]>&, int length);
- };
- #endif // __Question_1_4_h__
- #include<iostream>
- #include<memory>
- #include<string>
- #include "Question1_4.h"
- using namespace std;
- void Question1_4::replaceSpaces(unique_ptr<char[]> &str, int length) //char str[]
- {
- int newLength, spaceCount = 0;
- //count the number of spaces in the given string.
- for (int i = 0; i < length; i++)
- {
- if (str[i] == ' ')
- {
- spaceCount++;
- }
- }
- //calculate new string size.
- newLength = length + spaceCount * 2;
- str[newLength] = '\0';
- //copying the characters backwards and inserting %20
- for (int i = length - 1; i >= 0; i--)
- {
- if (str[i] == ' ')
- {
- str[newLength - 1] = '0';
- str[newLength - 2] = '2';
- str[newLength - 3] = '%';
- newLength -= 3;
- }
- else
- {
- str[newLength - 1] = str[i];
- newLength--;
- }
- }
- }
- int Question1_4::run()
- {
- string str = "abc d e f";
- // Increasing length of the string to meet question requirement of 'true' length by using char array. (Note: using a unique_ptr here)
- auto newStr = make_unique<char[]>(str.length() + 3 * 2 + 1);
- for (int i = 0; i < str.length(); i++)
- {
- newStr[i] = str[i];
- }
- cout << "Original string is " << str << endl;
- replaceSpaces(newStr, str.length());
- cout << "New string with %20 is " << newStr.get() << endl;
- return 0;
- }
1.5
- #ifndef __Question_1_5_h__
- #define __Question_1_5_h__
- #include <string>
- using std::string;
- class Question1_5
- {
- public:
- int run();
- int stringToInt(const string& value);
- string intToString(int value);
- int countCompression(const string& str);
- /// C++ std::string is efficient and no need to use a "StringBuffer"-like structure.
- string compressBetter(const string& str);
- };
- #endif // __Question_1_5_h__
- #include<iostream>
- #include<string>
- #include<sstream>
- #include "Question1_5.h"
- using namespace std;
- int Question1_5::stringToInt(const string& value)
- {
- int temp;
- stringstream(value) >> temp;
- return temp;
- }
- string Question1_5::intToString(int value)
- {
- string temp;
- stringstream convert;
- convert << value;
- temp = convert.str();
- return temp;
- }
- int Question1_5::countCompression(const string& str)
- {
- if (str.length() == 0)
- {
- return 0;
- }
- char last = str.at(0);
- int size = 0;
- int count = 1;
- for (int i = 1; i < str.length(); i++)
- {
- if (str.at(i) == last)
- {
- count++;
- }
- else
- {
- last = str.at(i);
- size = size + 1 + intToString(count).length();
- count = 1;
- }
- }
- size = size + 1 + intToString(count).length();
- return size;
- }
- string Question1_5::compressBetter(const string& str)
- {
- int size = countCompression(str);
- if(size >= str.length())
- {
- return str;
- }
- string newstr;
- char last = str.at(0);
- int count = 1;
- for (int i = 1; i < str.length(); i++)
- {
- if (str.at(i) == last)
- {
- count++;
- }
- else
- {
- newstr += last;
- newstr.append(intToString(count));
- last = str.at(i);
- count = 1;
- }
- }
- newstr += last;
- newstr.append(intToString(count));
- return newstr;
- }
- int Question1_5::run()
- {
- string str = "abbccccccde";
- string newstr = compressBetter(str);
- cout << "Original string is " << str << endl;
- cout << "Compressed string is " << newstr << endl;
- return 0;
- }
1.6
- #ifndef __Question_1_6_h__
- #define __Question_1_6_h__
- class Question1_6
- {
- public:
- /**
- * Since we can't provide variable matrix size in C++, we will do it "the c way" and will provide a 1-dimensional
- * array
- */
- void rotate(int* matrix, int n);
- void printMatrix(int* matrix, int m, int n);
- int run();
- };
- #endif // __Question_1_6_h__
- #include <iostream>
- #include <memory>
- #include "Question1_6.h"
- using namespace std;
- void Question1_6::rotate(int* matrix, int n)
- {
- for (int layer = 0; layer < n / 2; ++layer)
- {
- int first = layer;
- int last = n - 1 - layer;
- for (int i = first; i < last; ++i)
- {
- int offset = i - first;
- // save top
- int top = matrix[first * n + i];
- // left to top
- matrix[first * n + i] = matrix[(last-offset) * n + first];
- // bottom to left
- matrix[(last-offset) * n + first] = matrix[last * n + (last-offset)];
- // right to bottom
- matrix[last * n + (last-offset)] = matrix[i * n + last];
- // top to right
- matrix[i * n + last] = top;
- }
- }
- }
- void Question1_6::printMatrix(int* matrix, int m, int n)
- {
- for (int i = 0; i < m; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- cout << matrix[i * n + j] << " ";
- }
- cout << endl;
- }
- }
- int Question1_6::run()
- {
- int matrix[][5] ={{1, 2, 3, 4, 5},
- {6, 7, 8, 9, 10},
- {11, 12, 13, 14, 15},
- {16, 17, 18, 19, 20},
- {21, 22, 23, 24, 25}};
- int* matrixPtr = (int*)matrix;
- cout << "original matrix is :" << endl;
- printMatrix(matrixPtr, 5, 5);
- rotate(matrixPtr, 5);
- cout << "rotated matrix is: " << endl;
- printMatrix(matrixPtr, 5, 5);
- return 0;
- }
1.7
- #ifndef __Question_1_7_h__
- #define __Question_1_7_h__
- class Question1_7
- {
- public:
- /**
- * Since we can't provide variable matrix size in C++, we will do it "the c way" and will provide a 1-dimensional
- * array
- */
- void setZeros(int* matrix, int m, int n);
- void printMatrix(int* matrix, int m, int n);
- int run();
- };
- #endif // __Question_1_7_h__
- #include <iostream>
- #include "Question1_7.h"
- using namespace std;
- void Question1_7::setZeros(int* matrix, int m, int n)
- {
- // Assuming M,N <= 32, we'll use a bit vector to represent whether a row/col should be set with zeros.
- int m_rows = 0;
- int m_cols = 0;
- for (int i = 0; i < m; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- if (matrix[i * n + j] == 0)
- {
- m_rows |= (1 << i);
- m_cols |= (1 << j);
- }
- }
- }
- for (int i = 0; i < m; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- if (((m_rows & (1 << i)) != 0) || ((m_cols & (1 << j)) != 0))
- {
- matrix[i * n + j] = 0;
- }
- }
- }
- }
- void Question1_7::printMatrix(int* matrix, int m, int n)
- {
- for (int i = 0; i < m; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- cout << matrix[i * n + j] << " ";
- }
- cout << endl;
- }
- }
- int Question1_7::run()
- {
- int matrix[4][5] ={{1, 2, 3, 4, 5},
- {6, 7, 8, 9, 10},
- {11, 12, 0, 14, 15},
- {16, 17, 18, 0, 20}};
- int* matrixPtr = (int*)matrix;
- cout << "original matrix is :" << endl;
- printMatrix(matrixPtr, 4, 5);
- setZeros(matrixPtr, 4, 5);
- cout << "zeroed matrix is: " << endl;
- printMatrix(matrixPtr, 4, 5);
- return 0;
- }
1.8
- #ifndef __Question_1_8_h__
- #define __Question_1_8_h__
- #include <string>
- using std::string;
- class Question1_8
- {
- public:
- string result(bool value);
- bool isRotation(const string& s1, const string& s2);
- int run();
- };
- #endif // __Question_1_8_h__
- #include<iostream>
- #include<string>
- #include "Question1_8.h"
- using namespace std;
- bool Question1_8::isRotation(const string& s1, const string& s2)
- {
- int len = s1.length();
- if(len == s2.length() && len > 0)
- {
- string s1s1 = s1 + s1;
- return s1s1.find(s2) != string::npos;
- }
- return false;
- }
- string Question1_8::result(bool value)
- {
- if (value)
- {
- return "True";
- }
- return "False";
- }
- int Question1_8::run()
- {
- string a = "apple";
- string b = "leapp";
- cout << "Checking if string: " << a << " is a rotation of string: " << b << ": "
- << result(isRotation(a, b)) << endl;
- a = "james";
- b = "mesje";
- cout << "Checking if string: " << a << " is a rotation of string: " << b << ": "
- << result(isRotation(a, b)) << endl;
- return 0;
- }
crack the coding interview的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- 转:Top 10 Algorithms for Coding Interview
The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Python Coding Interview
Python Coding Interview Python Advanced Use enumerate() to iterate over both indices and values Debu ...
- whiteboard & coding interview practice
whiteboard & coding interview practice 白板 & 面试 & 编码练习 Algorithm https://www.freecodecamp ...
随机推荐
- python3 第十七章 - sequence(序列)
之前我们在讲for循环语句时就提到过序列,那么什么是序列(sequence)? 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 —— 它的索引(位置),第一个索引是0,第二个索引 ...
- synchronized内存可见性理解
一.背景 最近在看<Java并发编程实战>这本书,看到共享变量的可见性,其中说到"加锁的含义不仅仅局限于互斥行为,还包括内存可见性". 我对于内存可见性第一反应是vol ...
- maven插件本地化安装
mvn install:install-file -Dfile="D:\maven\repository\com\tc\itfarm-api\1.0.0-SNAPSHOT\itfarm-ap ...
- IO (二)
1 字符流的缓冲区 缓冲区的出现提高了对数据的读写效率. 对应的类: BufferedWriter BufferedReader 缓冲区要结合流才能使用. 在流的基础上对流的功能进行了增强. 2 Bu ...
- css盒子居中定位问题
在HTML中,div盒子的居中要通过外边距margin和width来控制,首先确定盒子的宽度,然后确定盒子方位并将其平移便可使盒子移到固定位置. <div id="divpic&quo ...
- poj 1423 打表/斯特林公式
对于n位数的计算,我们可以采用(int)log10(n) + 1的方法得到n的位数 第一种方法: 对于n!位数的计算,log10(n!) = log10(1) + log10(2) + ... + l ...
- 精通libGDX游戏开发-RPG实战-欢迎来到RPG的世界
欢迎来到RPG的世界 本章我会快速的使用tiled这样的瓷砖地图工具,来带领大家创造所设想的世界. 创建并编辑瓷砖地图 瓷砖地图(tile-based map)是广泛应用于各种游戏类型的地图格式,li ...
- React 16.3来了:带着全新的Context API
文章概览 React在版本16.3-alpha里引入了新的Context API,社区一片期待之声.我们先通过简单的例子,看下新的Context API长啥样,然后再简单探讨下新的API的意义. 文中 ...
- Android+TensorFlow+CNN+MNIST 手写数字识别实现
Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...
- BZOJ 1969: [Ahoi2005]LANE 航线规划 [树链剖分 时间倒流]
题意: 一张图,删除边,求两点之间的割边数量.保证任意时刻图连通 任求一棵生成树,只有树边可能是割边 时间倒流,加入一条边,就是两点路径上的边都不可能是割边,区间覆盖... 然后本题需要把边哈希一下, ...