📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: subzero2
跳转到指定楼层
上一主题 下一主题
收起左侧

Amazon OA2 面经

 
🔗
HuaZhe 2016-9-21 03:34:42 | 只看该作者
全局:
第三题大概这样可以吗???谢谢楼主了!
  1. public class solution {
  2.         public static void main (String[] args) {
  3.                 Connection[] citys = new Connection[10];
  4.                 citys[0] = new Connection("A","B",6);
  5.                 citys[1] = new Connection("A","D",1);
  6.                 citys[2] = new Connection("A","E",5);
  7.                 citys[3] = new Connection("B","C",3);
  8.                 citys[4] = new Connection("B","D",5);
  9.                 citys[5] = new Connection("C","D",6);
  10.                 citys[6] = new Connection("C","F",6);
  11.                 citys[7] = new Connection("D","F",4);
  12.                 citys[8] = new Connection("D","E",5);
  13.                 citys[9] = new Connection("E","F",2);
  14.                 ArrayList<Connection> list = new ArrayList<Connection> ();
  15.                 for (Connection temp : citys) {
  16.                         list.add(temp);
  17.                 }
  18.                
  19.                 for (Connection temp : findPath(list)){
  20.                         temp.printConnection();
  21.                 }
  22.                
  23.         }
  24.        
  25.         public static List<Connection> findPath (List<Connection> list) {
  26.                 ArrayList<Connection> result = new ArrayList<Connection> ();
  27.                 ArrayList<String> cityTree = new ArrayList<String> ();        //to Maintain the cities have been traversed.

  28.                 while (!list.isEmpty()) {
  29.                         //find the minimum cost to the city from cityTree among the result.
  30.                         Connection temp = findMin(list, cityTree);
  31.                         if (temp == null) {// we finished
  32.                                 break;
  33.                         }
  34.                         list.remove(temp);        //remove the current minimum cost from grand set
  35.                         cityTree.add(temp.city1);
  36.                         cityTree.add(temp.city2);
  37.                         result.add(temp);
  38.                 }
  39.                 //override compare make it alphabet order
  40.                 Comparator<Connection> cmp = new Comparator<Connection>(){
  41.                         public int compare(Connection c1, Connection c2) {
  42.                                 if (c1.city1.equals(c2.city1)) {
  43.                                         return c1.city2.compareTo(c2.city2);
  44.                                 }
  45.                                 return c1.city1.compareTo(c2.city1);
  46.                         }};
  47.                 result.sort(cmp);
  48.                
  49.                 return result;
  50.         }
  51.        
  52.         //find the minimum cost between the city we traversed and we did not traverse.
  53.         public static Connection findMin(List<Connection> list, ArrayList<String> cityTree) {
  54.                 Connection result = null;
  55.                 int minCost = Integer.MAX_VALUE;        //minimum cost
  56.                
  57.                 for (Connection con : list) {
  58.                         if (con.cost <= minCost) {
  59.                                 //if none of the city we traversed
  60.                                 //or the connection is connected to either city from cityTree.
  61.                                 if ((cityTree.contains(con.city1) && !cityTree.contains(con.city2)) ||
  62.                                                 cityTree.contains(con.city2) && !cityTree.contains(con.city1)) {
  63.                                         minCost = con.cost;
  64.                                         result = con;
  65.                                 }
  66.                                 if (cityTree.isEmpty()) {
  67.                                         minCost = con.cost;
  68.                                         result = con;
  69.                                 }
  70.                         }
  71.                 }
  72.                
  73.                 return result;
  74.         }
  75.        

  76. }
复制代码

  1. public class Connection {
  2.         String city1;
  3.         String city2;
  4.         int cost;
  5.         Connection (String city1, String city2, int cost) {
  6.                 this.city1 = city1;
  7.                 this.city2 = city2;
  8.                 this.cost = cost;
  9.         }
  10.        
  11.         public void printConnection () {
  12.                 System.out.println("{ " + this.city1 + " , " + this.city2 + "} " + this.cost);
  13.         }
  14. }
复制代码
回复

使用道具 举报

🔗
香蕉万岁 2016-9-22 05:22:28 | 只看该作者
本楼:
全局:
感谢楼主
回复

使用道具 举报

🔗
joker8116 2016-9-22 12:23:35 | 只看该作者
全局:
楼主 video怎么样了~
回复

使用道具 举报

🔗
xwjjjw 2016-9-23 10:20:17 | 只看该作者
全局:
请问楼主定了什么时候video?
回复

使用道具 举报

🔗
xiaoling 2016-9-24 06:47:06 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
xwjjjw 2016-9-24 08:11:36 | 只看该作者
全局:
xiaoling 发表于 2016-9-24 06:47
请问楼主是用C++的吗?  输入输出 是vector还是其他形式的?

我用java的,c++应该是你说的那样
回复

使用道具 举报

🔗
xiaoling 2016-9-25 06:48:50 | 只看该作者
全局:
xwjjjw 发表于 2016-9-24 08:11
我用java的,c++应该是你说的那样

听说c++都给的指针  你有同学用c++做过吗  想知道输入到底是怎么样的   如果那数组指针做参数的话  会同时传一个数组长度吗 不然没法做啊
回复

使用道具 举报

🔗
xwjjjw 2016-9-25 09:36:44 | 只看该作者
全局:
xiaoling 发表于 2016-9-25 06:48
听说c++都给的指针  你有同学用c++做过吗  想知道输入到底是怎么样的   如果那数组指针做参数的话  会同 ...

额,不好意思,身边都是java...
回复

使用道具 举报

🔗
HuaZhe 2016-9-28 04:47:28 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
xwjjjw 2016-9-28 04:56:31 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表