列车组成的最佳解决方案

时间:2017-03-31 07:50:43

标签: java

最近我回答了我的在线面试测试,其中我被问到以下问题

  

通过从左侧和右侧安装和拆卸货车来构建TrainComposition。

     

例如,如果我们首先从左侧安装货车7,然后再从左侧安装货车13,我们得到两辆货车的组合(从左到右为13和7)。现在可以从右侧分离的第一辆货车是7,第一辆可以从左侧拆卸的货车是13.实施一个模拟这个问题的TrainComposition。

我使用双向链表来解决问题

class Node

{
    protected int data;
    protected Node next, prev;
    /* Constructor */
    public Node()
    {
        next = null;
        prev = null;
        data = 0;
    }

    /* Constructor */

    public Node(int d, Node n, Node p)
    {
        data = d;
        next = n;
        prev = p;
    }

    /* Function to set link to next node */
    public void setLinkNext(Node n) {
        next = n;
    }
    /* Function to set link to previous node */
    public void setLinkPrev(Node p) {
        prev = p;
    }

    /* Funtion to get link to next node */

    public Node getLinkNext() {
        return next;
    }

    /* Function to get link to previous node */

    public Node getLinkPrev() {
        return prev;
    }

    /* Function to set data to node */
    public void setData(int d) {
        data = d;
    }

    /* Function to get data from node */

    public int getData() {
        return data;
    }
}

public class SortedSearch {
    protected Node start;
    protected Node end;
    public int size;
    /* Constructor */
    public SortedSearch()
    {
        start = null;
        end = null;
        size = 0;
    }
    public boolean isEmpty()
    {
        return start == null;
    }

    public int getSize()
    {
        return size;
    }

    public void attachWagonFromLeft(int wagonId) {
        Node nptr = new Node(wagonId, null, null);
        if (start == null)
        {
            start = nptr;
            end = start;
        }
        else
        {
            start.setLinkPrev(nptr);
            nptr.setLinkNext(start);
            start = nptr;
        }
        size++;
    }
    public void attachWagonFromRight(int wagonId) {
        Node nptr = new Node(wagonId, null, null);
        if (start == null)
        {
            start = nptr;
            end = start;
        }
        else
        {
            nptr.setLinkPrev(end);
            end.setLinkNext(nptr);
            end = nptr;
        }
        size++;
    }
    public int detachWagonFromLeft() {
        int value=0;
        if (size == 1)
        {
            value = start.getData();
            start = null;
            end = null;
            size = 0;
            return value;

        }
        value = start.getData();
        start = start.getLinkNext();
        start.setLinkPrev(null);
        size--;
        return value;
    }

    public int detachWagonFromRight() {
        int value=0;
            value = end.getData();
            end = end.getLinkPrev();
            end.setLinkNext(null);
            size-- ;
            return value;
    }

    public static void main(String[] args) {
        SortedSearch tree = new SortedSearch();
        tree.attachWagonFromLeft(7);
        tree.attachWagonFromLeft(13);
        tree.attachWagonFromLeft(12);
        tree.attachWagonFromLeft(10);
        tree.attachWagonFromLeft(6);
        tree.attachWagonFromLeft(4);
        tree.attachWagonFromLeft(3);
        tree.attachWagonFromLeft(2);
        System.out.println(tree.detachWagonFromRight()); // 7
        System.out.println(tree.detachWagonFromRight()); // 13
        System.out.println(tree.detachWagonFromRight()); // 7
        System.out.println(tree.detachWagonFromRight()); // 13
        System.out.println(tree.detachWagonFromRight()); // 7
        System.out.println(tree.detachWagonFromRight()); // 13

    }
}

并进行相应的测试。但是当提交它时说内部测试用例失败了,答案标记错了。 能告诉你哪个是这个问题的最佳解决方案。

5 个答案:

答案 0 :(得分:5)

为什么不使用这个简单的实现?

private static class TrainComposition {

  private final Deque<Integer> wagons = new LinkedList<>();

  public void attachLeft(int wagonNumber) {
    wagons.addFirst(wagonNumber);
  }

  public void attachRight(int wagonNumber) {
    wagons.addLast(wagonNumber);
  }

  public void detachLeft() {
    if (!wagons.isEmpty()) {
      wagons.removeFirst(); // Alternative if exception should not be bubbled up: wagons.pollFirst()
    } else {
      throw new IndexOutOfBoundsException("No wagons available");
    }
  }

  public void detachRight() {
    if (!wagons.isEmpty()) {
      wagons.removeLast(); // Alternative if exception should not be bubbled up: wagons.pollLast()
    } else {
      throw new IndexOutOfBoundsException("No wagons available");
    }
  }
}

答案 1 :(得分:3)

似乎detachWagonFromRight缺少detachWagonFromLeft所拥有的检查:

public int detachWagonFromLeft() {
    int value=0;
    if (size == 1)
    {
        value = start.getData();
        start = null;
        end = null;
        size = 0;
        return value;

    }
    value = start.getData();
    start = start.getLinkNext();
    start.setLinkPrev(null);
    size--;
    return value;
}

public int detachWagonFromRight() {
    int value=0;
        value = end.getData();
        end = end.getLinkPrev();
        end.setLinkNext(null);
        size-- ;
        return value;
}

所以当您从右侧移除最后一辆货车时,start仍然指向它

答案 2 :(得分:0)

看起来有点“长”,你的解决方案。我将定义一个名为“train”的对象,它有一个object.value = number_of_wagons(列表)。然后定义2个方法:

  • train.attach(site,number):使用append命令或insert命令从所需的一侧附加新号码
  • train.detach(site):删除列表中的元素,并可能打印出数字。

然而,根据我的理解(问题的描述也不是很详细,所以我不知道应该是什么样的预期答案)你只能从一侧附加货车,因为发动机在另一侧);)< / p>

答案 3 :(得分:0)

我希望您一切都好,因为我遇到了问题,我对C#的解决方案如下:

using System;
using System.Collections.Generic;

public class TrainComposition
{
    public List <int> wagon = new List <int> ();

    public void AttachWagonFromLeft(int wagonId)
    {
        wagon.Add(wagonId);
        //throw new NotImplementedException("Waiting to be implemented.");
    }

    public void AttachWagonFromRight(int wagonId)
    {
        wagon.Insert(0, wagonId);
        //throw new NotImplementedException("Waiting to be implemented.");
    }

    public int DetachWagonFromLeft()
    {
        int elem = 0;
        int indexValue = 0;

        elem = wagon[wagon.Count - 1]; // Get the value of the last element

        indexValue = wagon.LastIndexOf(elem); //Get the index the last value

        wagon.RemoveAt(indexValue);// This will remove the part at index 

        return elem;
        //throw new NotImplementedException("Waiting to be implemented.");
    }

    public int DetachWagonFromRight()
    {
        int elem = 0;

        elem = wagon[0];

        wagon.RemoveAt(0);

        return elem;
        //throw new NotImplementedException("Waiting to be implemented.");
    }

    public static void Main(string[] args)
    {
        TrainComposition tree = new TrainComposition();
        tree.AttachWagonFromLeft(7);
        tree.AttachWagonFromLeft(13);
        Console.WriteLine(tree.DetachWagonFromRight()); // 7 
        Console.WriteLine(tree.DetachWagonFromLeft()); // 13
    }
}

答案 4 :(得分:-3)

import java.util.*;
public class TrainComposition {
    LinkedList<Integer> wagons = new LinkedList<Integer>();
  public static void main(String[] args) {



    TrainComposition tree = new TrainComposition();
    tree.attachWagonFromLeft(7);
    tree.attachWagonFromLeft(13);
    System.out.println(tree.detachWagonFromRight()); // 7
    System.out.println(tree.detachWagonFromLeft()); // 13
}

public void attachWagonFromLeft(int wagonId) {
    wagons.addFirst(wagonId);
   // throw new UnsupportedOperationException("Waiting to be implemented.");
}

public void attachWagonFromRight(int wagonId) {
    wagons.addLast(wagonId);
   // throw new UnsupportedOperationException("Waiting to be implemented.");
}

public int detachWagonFromLeft() {
    if (!wagons.isEmpty()) {
  return wagons.removeFirst();

   } else {
  throw new IndexOutOfBoundsException("No wagons available");
}
}    
 public int detachWagonFromRight() {
    if (!wagons.isEmpty()) {
  return wagons.removeLast();

   } else {
  throw new IndexOutOfBoundsException("No wagons available");
}

}