-1

问题 4:给定一个整数数组,将其转换为每个节点包含一个序列的链表。

Sample Input : [1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30,31,32]
Sample Linked List : [1] -> [3,4,5] -> [8,9] -> [11] -> [13,14,15,16]->[20]->[23]->[30,31,32]

这个问题似乎很简单,但回答起来有点困难。任何人都可以在不使用Collectionor的情况下用 Java 编写上述代码LinkedList吗?

下面是检测序列的代码(可能有帮助)。

class Sequence {
    public static String detectSequence(int seq[]) {
        String result = "";
        for (int i = 0; i < seq.length - 1; i++) {
            if (seq[i + 1] == seq[i] + 1) {
                result += seq[i] + " ";
                if (i != seq.length - 2) {
                    if (seq[i + 1] != seq[i + 2] - 1) {
                        result += seq[i + 1];
                    }
                }
            } else {
                result += " ";
            }
        }
        if (seq[seq.length - 1] == seq[seq.length - 2] + 1) {
            result += seq[seq.length - 1];
        }
        return result;
    }
}
class Question1 {
    public static void main(String[] cla) {
        int seqArray[] = {
            4, 1, 2, 3, 4, 5, 8, 4, 7, 4, 5, 6, 7, 7, 7, 7, 7, 10, 11, 13, 1, 2, 3, 4
        };
        String res = Sequence.detectSequence(seqArray);
        System.out.println(res);
    }
}
4

2 回答 2

0

只是给你一个开始...

public YourLinkedList splitToSequences(int[] array) {
    YourLinkedList list = new YourLinkedList();

    if(array.length > 0) {
        YourSequence sequence = new YourSequence();
        int currentNumber;
        int lastNumber = array[0];
        sequence.add(lastNumber);

        for(int index = 1; index < array.length; index++) {
            currentNumber = array[index];
            if(currentNumber != lastNumber + 1) { // curentNumber breaks the sequence
                list.add(sequence);               // save the old sequence to list 
                sequence = new YourSequence();    // and start a new one
            }
            sequence.add(currentNumber);
        }

        list.add(sequence);
    }
    return list;
}

现在去找出你的链表和序列类并做打印的东西......

链表的简约实现

public class MyLinkedList<T1> {
    private MyLinkedListItem<T1> first = null;
    private MyLinkedListItem<T1> last = null;

    public MyLinkedList() {
    }

    public void add(T1 item) {
        MyLinkedListItem<T1> newItem = new MyLinkedListItem<T1>(item);
        if (first == null) {
            first = newItem;
        } else {
            last.setNext(newItem);
        }
        last = newItem;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();

        if(first != null) {
            MyLinkedListItem<T1> current = first;

            while(current.hasNext()) {
                buffer.append(current.toString());
                buffer.append(" -> ");
                current = current.getNext();
            }

            buffer.append(current.toString());
        }

        return buffer.toString();
    }

    private class MyLinkedListItem<T2> {
        private T2 data;
        private MyLinkedListItem<T2> next = null;

        public MyLinkedListItem(T2 data) {
            this.data = data;
        }

        public boolean hasNext() {
            return next != null;
        }

        public MyLinkedListItem<T2> getNext() {
            return next;
        }

        public void setNext(MyLinkedListItem<T2> next) {
            this.next = next;
        }

        @Override
        public String toString() {
            return data.toString();
        }
    }
}
于 2013-05-03T08:23:24.267 回答
0

首先,可以使用迭代器编写将数组拆分为多个块的代码,以避免将此算法与创建链表的代码混合。然后有一种方法可以以函数的方式实现一个简单的链表(所以整个链表是不可变的)。

显然,这段代码不可读但很有趣!

import java.util.Arrays;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.IntStream;

public class LinkedConsecutiveInts {
  private static Iterator<int[]> iterator(int[] array) {
    return new Iterator<int[]>() {
      private int index;

      @Override
      public boolean hasNext() {
       return index < array.length;
      }

      @Override
      public int[] next() {
        int first = array[index];
        int value = first + 1;
        int i = 1;
        for(; index + i < array.length && array[index + i] == value++; i++) {
          // empty
        }
        index += i;
        return IntStream.range(first, first + i).toArray();
      }
    };
  }

  interface Seq<T> {
    void forEach(Consumer<? super T> consumer);

    default <U> Seq<U> map(Function<? super T, ? extends U> mapper) {
      return consumer -> forEach(e -> consumer.accept(mapper.apply(e)));
    }

    default String joining(String separator) {
      StringBuilder builder = new StringBuilder();
      forEach(e -> builder.append(e).append(separator));
      if (builder.length() != 0) {
        builder.setLength(builder.length() - separator.length());
      }
      return builder.toString();
    }

    static <T> Seq<T> from(Iterator<? extends T> it) {
      if (!it.hasNext()) {
        return consumer -> { /* empty */ };
      }
      T element = it.next();
      Seq<T> next = from(it);
      return consumer -> { consumer.accept(element); next.forEach(consumer); };
    }
  }

  public static void main(String[] args) {
    int[] values = { 1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30, 31, 32 };

    Seq<int[]> seq = Seq.from(iterator(values)); 
    System.out.println(seq.map(Arrays::toString).joining(" -> "));
  }
}
于 2015-12-18T22:30:35.090 回答