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 ...
随机推荐
- HeadFirst Ruby 第九章总结 mixins & modules
前言 如果想要复用 method, 可用的方法是针对 Class 的 inheritance,但是, inheritance has its limitations,它的缺点有: 只能 inhert ...
- InitializingBean和DisposableBean
InitializingBean 记住一点:InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的子类,在初始化bea ...
- FetchType.LAZY和FetchType.EAGER什么区别
1.FetchType.LAZY:懒加载,加载一个实体时,定义懒加载的属性不会马上从数据库中加载. 2.FetchType.EAGER:急加载,加载一个实体时,定义急加载的属性会立即从数据库中加载. ...
- Shell脚本中的并发(转)
转自http://blog.csdn.net/wangtaoking1/article/details/9838571 主要记录一下Shell脚本中的命令的并发和串行执行. 默认的情况下,Shell脚 ...
- 集合 (set)
set 是一个无序不重复的元素集,集合跟字典是无序的,不支持索引 创建集合: 第一种方式:通过{ }创建 >>> num={1,2,3,4,3,3,1} >>> n ...
- 第一阶段——站立会议总结DAY03
昨天忘记发了,补充... 1.昨天做了什么:继续做界面的设计,使其更加美观. 2.今天准备做什么:准备将点击按钮时跳转到一个修改密码界面. 3.遇到的困难:此次第一阶段的任务是一个框架,不知道可否实现 ...
- CRM函数CRM_ORDER_MAINTAIN封装
抬头结构:ZCRM_MAINTAIN_H flag header: 项目表:ZCRM_MAINTAIN_I 合作伙伴:ZCRM_MAINTAIN_P price:ZCRM_MAINTAIN_PR FU ...
- fastjson如何指定字段不序列化
fastjson是一款由阿里巴巴提供的性能出色的json序列化与反序列化库,而且使用很方便,我们可以使用JSON.toJSONString(object)将一个对象序列化为json格式,但是如果我们不 ...
- MapReduce--平均分,最高,低分以及及格率的计算
MapReduce--平均分,最高,低分以及及格率的计算 计算班级的平均分,以及个人的最高最低分,以及每个班级的及格率. 来先看一下我的数据. 时间 班级 姓名 科目 成绩 20180501 1708 ...
- 浅谈MVVM
MVVM 模式将 Presenter 改名为 ViewModel,基本上与 MVP 模式完全一致. 唯一的区别是,它采用双向绑定(data-binding):View的变动,自动反映在 ViewMod ...