Cracking The Coding Interview 2.5
这题的思想来自于http://hawstein.com/posts/2.5.html,重新实现了一下
用hash来记录循环的起点
//Given a circular linked list, implement an algorithm which returns node at the beginning of the loop. //DEFINITION //Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list. //EXAMPLE //Input: A -> B -> C -> D -> E -> C [the same C as earlier] #include <iostream>
#include <map>
using namespace std; typedef struct node{
int data;
node *next;
}node; node* init(int a[], int n, int m){
node *head, *p, *q;
for(int i=0; i<n; ++i){
node *nd = new node();
nd->data = a[i];
if(i==m) q = nd;
if(i==0){
head = p = nd;
continue;
}
p->next = nd;
p = nd;
}
p->next = q;
return head;
} map<node *, bool> st;
node * getLoopStart(node *head)
{
node *p =head->next;
while(p)
{
if (st[p] != true)
{
st[p] = true;
p=p->next;
}
else
return p;
}
return p;
} int main(){
int n = 10, m = 9;// m<n
int a[] =
{
3, 2, 1, 3, 5, 6, 2, 6, 3, 1
};
node *head = init(a, n, m);
//node *p = loopstart(head); node *p2 = getLoopStart(head);
if(p2)
cout<<p2->data<<endl;
return 0;
}
Cracking The Coding Interview 2.5的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- 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. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- (转)C# Stopwatch详解
C# Stopwatch类 命名空间:System.Diagnostics; namespace System.Diagnostics { // 提供一组方法和属性,可用于准确地测量运行时间. pub ...
- 停止学习框架(Stop Learning Frameworks)
https://www.cnblogs.com/strick/p/10161733.html
- 20180821ImportContactFromExcel
Excel创建vcf文件,借助百度云助手导入Iphone6Plus Sub CreateContractList() Set Wb = Application.ThisWorkbook FilePat ...
- ssh登陆被拒?(云:使用云的网页版远程登陆) 不好用。
设定的密
- Confluence 6 让一个空间可以公众访问
如果你希望将一个空间分享给没有登录 Confluence 的用户(匿名用户)可以访问的话,你需要将这个空间标记为 公开(public). 让一个空间可以公开的访问的话,你就不能选择如何对这个空间进行 ...
- Confluence 6 空间权限和链接到相关的空间
空间权限 每一个空间将会创建一个默认的权限.创建空间的用户将会自动具有空间管理员(space admin)的权限,这个的意思是你可以为其他用户和用户组赋予空间访问和管理的权限. 请查看 Space P ...
- 2.4 UML类图
类图定义 类class的定义 具有相同属性.操作.方法.关系或者行为的一组对象的描述符 类是真实世界事物的抽象 问题领域的类:在对系统建模时,将会涉及到如何识别业务系统中的事物,这些事物构 成了整个业 ...
- jquery 根据自定义属性选择
<div myattr="test">text</div> 使用$("div[myattr='test']")进行选择
- ACM-选人问题(救济金发放)
n(n<20)个人站成一圈,逆时针编号为1-n.有两个官员,A从1开始逆时针数,B从n开 始顺时针数.在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个 官员停在同一个人上) ...
- ADG配置(主备库环境)
@font-face { font-family: "Courier New"; }@font-face { font-family: "宋体"; }@font ...