【问题标题】:Check whether LinkedList is an SubList检查 LinkedList 是否为 SubList
【发布时间】:2014-02-03 21:43:57
【问题描述】:
public class CharNode {
    private char _data;
    private CharNode _next;
    public CharNode(char dat, CharNode n) {
        _data = dat;
        _next = n;
    }
    public char getData() {
       return _data;
    }
    public CharNode getNext() {
        return _next;
    }
    public void setData(char d) {
       _data = d;
    }
    public void setNext(CharNode node) {
       _next = node;
     }
}

public class CharList {
    private CharNode _head;
    public CharList( ) {
        _head = null;
    }
    public CharList (CharNode node) {
        _head = node;
    }

    public int subList(IntList list)
    {
        int count = 0;
        IntNode listNode = _head;
        IntNode otherListNode = list._head; ;

        while (listNode != null)
        {
            if (otherListNode == null)
                otherListNode = list._head;
            if (listNode.getValue() == otherListNode.getValue())
            {
                listNode = listNode.getNext();
                otherListNode = otherListNode.getNext();
                if (otherListNode == null)
                    count++;
            }
            else
            {
                listNode = listNode.getNext();
                otherListNode = list._head;
            }              
        }

        return count;
    }
}

我需要编写函数public int subList (CharList list) 来获取list 并返回此列表存在的次数。 例如,如果我的列表是 a b c d a b g e 并且作为参数接收的列表是 a b 它将返回 2。 该方法应尽可能高效。

目前我的问题是我不知道如何同时遍历两个列表以比较值

【问题讨论】:

  • 您对哪个部分有问题?找到任何子列表?计算子列表?让它变得高效?
  • 如果元素是char,为什么不直接使用字符串和基本的字符串方法呢? IE。在“aaaabcca”中搜索“ab”
  • 我们不会为您做这一切。到目前为止,您尝试过什么?
  • 我不知道为了比较值而同时对两个列表进行热循环

标签: java


【解决方案1】:

同时遍历两个列表:

public bool equalsSubList(CharNode other) {
  CharNode node1 = this;  
  CharNode node2 = other;  
  while (node1 != null && node2 != null) { 
    // compare data from both nodes
    node1 = node1.getNext();
    node2 = node2.getNext();
  }
  // return true if all nodes where compared and are equal
}

对于完整的解决方案,您必须循环遍历您的列表一次,而另一个列表的次数与匹配次数一样多。让我们举个例子,a b c d a b g ea b 相比:

other |   this
      |
a b   |   a b c d a b g e 
^     |   ^ (match a, go to next in both lists)
a b   |   a b c d a b g e   |
  ^   |     ^ (match a b, counter is 1, go to next this, restart other)
a b   |   a b c d a b g e
^     |       ^ (does not match a, go to next this, restart other)
a b   |   a b c d a b g e
^     |         ^ (does not match a, go to next this, restart other)
a b   |   a b c d a b g e
^     |           ^ (match a, go to next in both lists)
a b   |   a b c d a b g e
  ^   |             ^ (match a b, counter is 2, go to next this, restart other)
a b   |   a b c d a b g e
^     |               ^ (does not match a, go to next this, restart other)
a b   |   a b c d a b g e
^     |                 ^ (does not match a, go to next this, restart other)

【讨论】:

  • 谢谢,在这种情况下,我将一次又一次地遍历列表,我怎样才能只遍历我的列表一次? (O(n))
  • 不应该是CharNodeCharListgetData()而不是IntNodeIntListgetValue()吗?要检查您的解决方案是否良好,请尝试使用不同的测试场景对其进行调试,并将程序与所需的行为进行比较。确保您创建单元测试来执行此操作!
  • 谢谢,顺便说一句,我的解决方案是 O(n) ?
  • 是的,如果您遵循上述行为,它将是 O(n)。 stackoverflow.com/questions/1909307/what-does-on-mean
猜你喜欢
  • 2013-04-10
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
相关资源
最近更新 更多