链表结点类型定义: class Node { public: ; Node *next = nullptr; Node(int d) { data = d; } }; 快行指针(runner)技巧: 同时使用两个指针来迭代访问链表,其中一个比另一个超前一些.快指针比慢指针先行几步或者快指针与慢指针的速度呈一定的关系. dummy元素技巧: 在链表头部添加一个哑结点,通常可以简化首部或尾部的特殊情况的处理. 2.1 编写代码,移除未排序链表中的重复结点. 进阶:如果不得使用临时缓冲区,该怎么解决?…
1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.不允许使用额外的数据结构. 解答:这里假定字符集为ASCII码,可以与面试官沟通确认字符串使用的字符集.由于字符集是有限的,建立一个数组模拟的Hash表记录每个字符是否出现,线性扫描一次字符串即可,复杂度O(len(s)).如果字符集较大,需要考虑空间开销,则可以用bitset来实现. bool isUnique(string s) { ]; memset(record, , sizeof(record)); size_t size =…
推荐一本书<Cracking the code interview> Now in the 5th edition, Cracking the Coding Interview gives you the interview preparation you need to get the top software developer jobs. This is a deeply technical book and focuses on the software engineering ski…
导语 所有的编程练习都在牛客网OJ提交,链接: https://www.nowcoder.com/ta/cracking-the-coding-interview 第八章 面试考题 8.1 数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不相同.假设不允许使用额外的数据结构,又该如何处理? 题解:应该先clarify上面的字符串是 ASCII 还是 unicode,假设是 ASCII,我们可以直接用256个字母表来统计.用个vector<int> st(256, 0)即可.…
给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果两个链表如果相交,那么交点之后node都是共享(地址相同)的,因此最简单暴力的方法就是两个for循环,判断该链表的node是否属于另外一个链表.但是这个算法复杂度是O(length1 * length2).如果链表较长,这个复杂度有点高了. 当然也可以遍历其中某个链表,将node的地址存储hash…
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height. 1.Divide the array equally into left part and right part, the mid value will be the root. 2.Recall the function to the left and right part of the…
#include <iostream> #include <string> using namespace std; class linklist { private: class node { public: node(){} string data; node * next; }; node *first; int size; public: linklist() { first = new node; size = 0; } /***整表创建***/ void Create(…
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…