java-谷歌面试题,求大神帮忙

问题描述

谷歌面试题,求大神帮忙

原题是这样的(后面我补充了中文解释):

Don't mind the map

After the trauma of Dr. Boolean's lab, the rabbits are eager to get back to their normal lives in a well-connected community, where they can visit each other frequently. Fortunately, the rabbits learned something about engineering as part of their escape from the lab. To get around their new warren fast, they built an elaborate subway system to connect their holes. Each station has the same number of outgoing subway lines (outgoing tracks), which are numbered.

Unfortunately, sections of warrens are very similar, so they can't tell where they are in the subway system. Their stations have system maps, but not an indicator showing which station the map is in. Needless to say, rabbits get lost in the subway system often. The rabbits adopted an interesting custom to account for this: Whenever they are lost, they take the subway lines in a particular order, and end up at a known station.

For example, say there were three stations A, B, and C, with two outgoing directions, and the stations were connected as follows

Line 1 from A, goes to B. Line 2 from A goes to C.
Line 1 from B, goes to A. Line 2 from B goes to C.
Line 1 from C, goes to B. Line 2 from C goes to A.

Now, suppose you are lost at one of the stations A, B, or C. Independent of where you are, if you take line 2, and then line 1, you always end up at station B. Having a path that takes everyone to the same place is called a meeting path.
We are interested in finding a meeting path which consists of a fixed set of instructions like, 'take line 1, then line 2,' etc. It is possible that you might visit a station multiple times. It is also possible that such a path might not exist. However, subway stations periodically close for maintenance. If a station is closed, then the paths that would normally go to that station, go to the next station in the same direction. As a special case, if the track still goes to the closed station after that rule, then it comes back to the originating station. Closing a station might allow for a meeting path where previously none existed. That is, if you have
A -> B -> C
and station B closes, then you'll have
A -> C
Alternately, if it was
A -> B -> B
then closing station B yields
A -> A

Write a function answer(subway) that returns one of:

-1 (minus one): If there is a meeting path without closing a station
The least index of the station to close that allows for a meeting path or
-2 (minus two): If even with closing 1 station, there is no meeting path.
subway will be a list of lists of integers such that subway[station][direction] = destination_station.

That is, the subway stations are numbered 0, 1, 2, and so on. The k^th element of subway (counting from 0) will give the list of stations directly reachable from station k.

The outgoing lines are numbered 0, 1, 2... The r^th element of the list for station k, gives the number of the station directly reachable by taking line r from station k.

Each element of subway will have the same number of elements (so, each station has the same number of outgoing lines), which will be between 1 and 5.

There will be at least 1 and no more than 50 stations.

For example, if
subway = [[2, 1], [2, 0], [3, 1], [1, 0]]
Then one could take the path [1, 0]. That is, from the starting station, take the second direction, then the first. If the first direction was the red line, and the second was the green line, you could phrase this as:
if you are lost, take the green line for 1 stop, then the red line for 1 stop.
So, consider following the directions starting at each station.
0 -> 1 -> 2.
1 -> 0 -> 2.
2 -> 1 -> 2.
3 -> 0 -> 2.
So, no matter the starting station, the path leads to station 2. Thus, for this subway, answer should return -1.

If
subway = [[1], [0]]
then no matter what path you take, you will always be at a different station than if you started elsewhere. If station 0 closed, that would leave you with
subway = [[0]]
So, in this case, answer would return 0 because there is no meeting path until you close station 0.

To illustrate closing stations,
subway = [[1,1],[2,2],[0,2]]
If station 2 is closed, then
station 1 direction 0 will follow station 2 direction 0 to station 0, which will then be its new destination.
station 1 direction 1 will follow station 2 direction 1 to station 2, but that station is closed, so it will get routed back to station 1, which will be its new destination. This yields
subway = [[1,1],[0,1]]

Languages

To provide a Python solution, edit solution.py
To provide a Java solution, edit solution.java

Test cases

Inputs:
(int) subway = [[2, 1], [2, 0], [3, 1], [1, 0]]
Output:
(int) -1

Inputs:
(int) subway = [[1, 2], [1, 1], [2, 2]]
Output:
(int) 1

大概意思就是,兔子在兔子窝(n <= 50)之间修了地铁,有k(k <= 5)条线路,每条线每一站都有出发,但是不一定每站都到,比如1号线可能是A->B, B->A, C->B; 2号线可能是A->C, B->C, C->A. 由于兔子窝长得都一样,所以兔子经常不知道自己究竟在哪一站,所以它们想了个办法,在任何一站,只要按照某种顺序乘坐地铁,最终都会到达某一站,比如上面的线路,只要按照先坐一号线,再坐二号线的顺序,无论在ABC哪一站出发,最后都会到C站。现在给出一种地铁线路,问存不存在这样一种顺序,满足按照这个顺序坐地铁,从任何一站出发都能到达某一站。以及如果有一站地铁关门维修了(如果某站关门了,那么到达这一站的车这一站不停,直接前往下一站,如果这一站的下一站还是自己,那么就在上一站循环),是否还存在这样一种顺序。

想了两天了,暴力解的话2^50严重超时,跪求大神帮忙

解决方案

solved, thanks

时间: 2025-01-08 07:28:11

java-谷歌面试题,求大神帮忙的相关文章

树形 递归-java 递归报错 求大神帮忙

问题描述 java 递归报错 求大神帮忙 private List<Post> getPostLower(List<Post> PostTops){ List<Post> postAll=new ArrayList<Post>(); // 上级 for(Post post:PostTops){ //查询到下级 List<Post> posts=basService.queryPostByParentId(post.getPostId()); //

java后台逻辑问题-求大神帮忙解释下这段代码。

问题描述 求大神帮忙解释下这段代码. 这是一个从表添加页面的代码.currentx是当前页数.我想问下 st st1 st2 st3是什么意思,就是split(:):这个方法我不是很清楚什么意思,还有下面的!ss.equals("t") t是什么. 传参什么的我晓得. @RequestMapping("/addProcess.do") public String addProcessMaintenance(String currentx, String ids, S

java规范excel电话号码 求大神帮忙

问题描述 例如excel中的原数据:row1,columA86010-28913333/25731234,13712340000row2,columA020-28345678,1371234-56-78/020281234-56-123row3,columA+86203456712,58344556,+86-137123-45678row4,columA021-28123456/010-3912345678想要规范成:row1,columA10-28913333row1,columB10-257

java kml-java生成kml文件,求大神帮忙!

问题描述 java生成kml文件,求大神帮忙! 拿到了经纬度信息,需要用经纬度信息生成kml文件,求大神指导java怎么写? 解决方案 参考这里:http://blog.csdn.net/hnyzwtf/article/details/51453693

dao-java继承问题,求大神帮忙解答

问题描述 java继承问题,求大神帮忙解答 service.impl里面是这么写的 public class ServiceImpl implements Service{ private Dao dao; public String getDao(){ return dao.getDao(); } } 下面是dao的代码 public interface Dao { public String getDao(); } 下面是dao.impl代码 public class DaoImpl imp

exception-java启动异常,求大神帮忙

问题描述 java启动异常,求大神帮忙 Exception sending context initialized event to listener instance of class com.ap.framework.core.spring.SpringContextLoaderListener org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Fa

关于java重写paint方法,求大神帮忙

问题描述 关于java重写paint方法,求大神帮忙 我能理解第一段模版函数,然后通过继承和重写方法,代入,为什么第二段代码,不需要带入父类方法,自己就跑起来了了,好像只要把paint重写了,系统自动跑, 还有中间通过屏幕监听的控制关闭的代码看不懂,为什么要这样写,老师说是匿名内部类,求大神讲解 解决方案 4444LJKHJHJHK'HJKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKJJJJJJJJJJJJJJ 解决方案二: 问题1:重新pa

java 上传图片到服务器上,在页面上不能显示,急急急,在线等,求大神帮忙!!!!!

问题描述 java 上传图片到服务器上,在页面上不能显示,急急急,在线等,求大神帮忙!!!!! java 上传图片到服务器上,图片上传成功,但是在页面展示时不能加载,路径没问题,手动将上传的图片改个名字后能正常加载 解决方案 最终的上传路径有没有特殊字符或者空格啥的,最好不要带特殊字符包括空格啥的. 解决方案二: 图片在内网服务器上A,部署在服务器B的应用要显示图片,受网络限制外网用户无法访问到图片,为了解决这个问题现将图片下载到服务器B上,现在服务器B上存在图片,但是不能正常加载,通过手动地对

界面-java 简易计算器,最后得数不能出现,求大神帮忙

问题描述 java 简易计算器,最后得数不能出现,求大神帮忙 (1)编写一个简易计算器程序,其界面如下图所示: (2)当按下"+"按钮时,两个数值文本框之间应显示"+"号,同时相加结果显示在第三个文本框内(如下图所示).类似处理"-"."*"和"/"按钮. 现在第二步能够出现加号 就是不能正确运算.如下代码,注释的地方为什么不能运行,该怎么做才能做到当按下加的按钮b1时,同时出现加号和得数?这里的tf和tf