Cracking the coding interview】的更多相关文章

写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the coding interview>,来练练手,顺带复习一下自己的基础知识,一些常用的数据结构,偶然在某位大神的blog里看到其分享的文章,还有他所做的解答,感觉自己的解答远没有他的简洁,且其解题都会优先考虑其空间和时间复杂度.本系列的文章只介绍,我做题过程中,遇到的一些好的思想方法,当然我会贴出一些代码.在…
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最近开始重新捡起面试题,来练练手,让自己保持代码的感觉. 代码主要是c的,可以避免使用容器之类的封装.因为使用c的话更能触及细节,而这也正是面试题所要考察的.同时,尽量为每道题添加了单元测试的用例. 代码是在windows下编辑运行的,只能保证在windows下正常运行,因为windows下的c编译器…
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少会有一些问题,需要找一本数据结构的书恶补一下如何更加合理的设计节点. ? class TreeNode { public:     int treenum;       TreeNode** children;     int child_num;     int child_len;     in…
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to implement three stacks. 我的思路:一般堆栈的实现会利用一个数组,这里一个数组若实现3个堆栈,直接考虑把数组划分为3个部分,相当于3个独立的数组,所以就有以下的实现. 但是,这种实现方式的缺点在于均分了每个stack需要的space,但是事先无法确定每个stack是否需要更多的spac…
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解答,其中不包括第15章. 相关源码在此repo中可以找到:https://github.com/zhuli19901106/Cracking-the-Coding-Interview <Cracking the Coding Interview>——第18章:难题——题目13 <Cracki…
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版中文版里面有189道程序员面试题目及相应的解答. 书中大部分是编程题目, 并且配有相应的java程序. 我把书中的题目做了一遍, 并且记录下来,包含自己对问题的一些思路及看法,许多问题给出了两种以上的解答方案. 由于个人在学习Go语言,所以程序是用Go 1.13编写,所有的代码都托管在Github上: htt…
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构函数在自动调用的时候,不会调用基类的析构函数,这样就会造成资源未释放引起的内存泄漏. 代码: // 13.6 If a class is defined as base class, why must its destructor be declared "virtual"? // Ans…
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的求和有公式可循,只要把所有数都加起来就能知道缺少的m是几了.书本提供了一种比较高效的解法,我仔细读了以后觉得书上给的优化算法实际上需要额外的空间来支持,coding难度偏高,临场的话我估计挺难写出来的. 代码: // 5.7 Find the missing integer from 0 to n,…
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或其他算数运算符. 给定两个int A和B.请返回A+B的值 测试样例: 1,2 返回:3 答案和思路:xor是相加不进位.and得到每一个地方的进位.所以,用and<<1之后去与xor异或.不断递归. import java.util.*; public class UnusualAdd { pu…
#include <iostream> #include <cstdio> #include <vector> #include <stack> #define ls(x) (((x + 1)<<1) - 1) #define rs(x) ((x + 1)<<1) using namespace std; ; typedef struct Node{ struct Node *left; struct Node *right; int…
1. Arrays and Strings 1.1 Hash Tables 哈希表,简单的说就是由一个数组和一个hash函数组成实现key/value映射并且能高效的查找的数据结构.最简单的想法就是将hash(key)做为数组的下标(index)来存取. 但是为了防止hash的冲突(collisions),数组的大小必须设置得足够大,因此上面这种简单的实现在实际中是不可取的. 实际上,哈希表是一个固定大小的数组,数组的每个元素是一个链表(单向或双向)的头指针.每个元素被存放在hash(key)%…
文章的缘由可以参考此篇文章.目前完成了第二章,代码放在github上,地址在此.问题的描述都在对应的代码文件中.其他的章节仍在在进行中. 如果代码有问题,欢迎指正,谢谢. yetuweiba…
2.2 链表中倒数第k个结点 输入一个链表,输出该链表中倒数第k个结点. 思路:快慢指针(error: 判断是否有可行解,否则返回null, while, if 后加空格) /* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode FindKthToTail(ListNode…
第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,True代表所有字符全都不同,False代表存在相同的字符.保证字符串中的字符为ASCII字符.字符串的长度小于等于3000. 测试样例: "aeiou" 返回:True "BarackObama" 返回:False 思路:(1) 两个for循环,比较后面的是否相同 O(…
1.Determine if a string has all unique characters learn: 为了减少空间利用率,其比较优秀的算法一般都适用位操作 返回值的命名方法,我们需要学习 String 类型作为输入参数,怎么样写比较节省空间与时间,特别是比较长且需要频繁调用的时候 输入为空的时候,不要忘记写不同输入情况下 需要考虑情况的列表 注意String 中的数据不一定是Ascii 2.Reverse a C-style String learn:   对于输入的参数要判别是否有…
给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果两个链表如果相交,那么交点之后node都是共享(地址相同)的,因此最简单暴力的方法就是两个for循环,判断该链表的node是否属于另外一个链表.但是这个算法复杂度是O(length1 * length2).如果链表较长,这个复杂度有点高了. 当然也可以遍历其中某个链表,将node的地址存储hash…
原版内容转自:CTCI面试系列——谷歌面试官经典作品 | 快课网 此系列为C#实现版本 谷歌面试官经典作品(CTCI)目录   1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除字符串中重复字符 1.8 利用已知函数判断字符串是否为另一字符串的子串 2.1 从链表中移除重复结点 2.2 实现一个算法从一个单链表中返回倒数第n个元素 2.3 给定链表中间某结点指针,删除链表中该结点 2.4 求由两个链表结点组成的数之和 2.5 给定一个循环链表,实现一个算法返回这个环的开始结…
//原文: // // A circus is designing a tower routine consisting of people standing atop one another's shoulders. For practical and aesthetic reasons, each person must be both shorter and lighter than the person below him or her. Given the heights and we…
//原文: // // Given a matrix in which each row and each column is sorted, write a method to find an element in it. // 从最右一排,从上到下,小于右上角的元素则下降,大于右上角的元素则左移找:第一个相等的元素. #include <iostream> using namespace std; int search(int **p, int sizex, int sizey, int…
//Given a sorted array of n integers that has been rotated an unknown number of times, give an O(log n) algorithm that finds an element in the array. You may assume that the array was originally sorted in increasing order. // //EXAMPLE: // //Input: f…
//原文: // // You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order. // // 译文: // // A和B是两个有序数组(假设为递增序列),而且A的长度足以放下A和B中所有的元素, 写一个函数将数组B融入数组A,并使其有序. #include <ios…
#include <iostream> #include <vector> using namespace std; void mswap(int &a, int &b) { int c = a; a = b; b = c; } void print(int *a, int size) { for (int i = 0; i<size ; i++) { cout<<a[i]<<" "; } } //Start at…
//An array A[1-n] contains all the integers from 0 to n except for one number which is missing. In this problem, //we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, //and the only operation…
//Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g., bit 0 and bit 1 are swapped, bit 2 and bit 3 are swapped, etc). #include <iostream> #include <vector> using namespace std; int getNum1(int N)…
#include <iostream> #include <vector> using namespace std; int getNum1(int N) { int num=0; for (int i=0; i<32; i++) { int t = N&1; if (t == 1) { num ++; } N = N>>1; } return num; } void print(int p) { vector<int> v; for (int…
//原文: // // You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1 // // 译文: // // 有两棵很大的二叉树:T1有上百万个结点,T2有上百个结点.写程序判断T2是否为T1的子树. 除了暴力法,没想到更好的..暂时放这…
//原文: // // Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree. // // 译文: // // 写程序在一棵二叉树中找到两个结点的第…
//Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you'll have D linked lists). // // 译文: // // 给定一棵二叉查找树,设计算法,将每一层的所有结点构建为一个链表(也就是说, 如果树有D层,那么你将构建出D个链表…
//Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one. // // 译文: // // 实现一个函数检查一棵树是否平衡.对于这个问题而言,…
#include <iostream> #include <string> using namespace std; class tree { public: tree() { //root = create(); root = NULL; } /***输入扩展层次遍历序列,#表示该节点为空***/ tree(char *s) { root = NULL; if (s == NULL) { return; } int size = strlen(s); create(&ro…