Coursera-Algorithms-4 编程作业: Queues

Coursera-Algorithms-4

Github仓库:

https://github.com/shuhai65/Coursera-Algorithms-4

Coursera: Algorithms I & II

https://www.coursera.org/learn/algorithms-part1

https://www.coursera.org/learn/algorithms-part2

编程作业: Queues

Deque用链表做即可,就是一个双向队列,模仿栈和队列的实现。

RandomizedQueue随机队列比较推荐用数组做,我这里写的是用链表实现的,关于用数组实现的我就不贴了,可以参考https://www.cnblogs.com/mingyueanyao/p/10088467.html中的实现。

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Deque<Item> implements Iterable<Item> {

    private final Node<Item> head;
    private final Node<Item> tail;
    // private Node prev = null;
    private int n;

    private static class Node<Item> {
        private Item item;
        private Deque.Node<Item> next;
        private Deque.Node<Item> prev;

        private Node() {
        }
    }

    // construct an empty deque
    public Deque() {
        n = 0;
        head = new Node<>();
        tail = new Node<>();

        head.item = null;
        tail.item = null;
        head.prev = null;
        tail.prev = head;
        head.next = tail;
        tail.next = null;
    }

    // is the deque empty?
    public boolean isEmpty() {
        return n == 0;
    }

    // return the number of items on the deque
    public int size() {
        return n;
    }

    // add the item to the front
    public void addFirst(Item item) {
        if (item == null) throw new IllegalArgumentException("item is null");
        Node<Item> oldFirst = head.next;
        Node<Item> first = new Node<>();
        first.item = item;
        first.next = oldFirst;
        first.prev = head;
        oldFirst.prev = first;
        head.next = first;
        n++;
    }

    // add the item to the back
    public void addLast(Item item) {
        if (item == null) throw new IllegalArgumentException("item is null");
        Node<Item> oldLast = tail.prev;
        Node<Item> last = new Node<>();
        last.item = item;
        last.next = tail;
        last.prev = oldLast;
        tail.prev = last;
        oldLast.next = last;
        n++;
    }

    // remove and return the item from the front
    public Item removeFirst() {
        if (isEmpty()) throw new NoSuchElementException("the deque is empty.");
        Node<Item> oldFirst = head.next;
        Node<Item> first = oldFirst.next;
        Item fistItem = oldFirst.item;
        head.next = first;
        first.prev = head;
        n--;
        return fistItem;
    }

    // remove and return the item from the back
    public Item removeLast() {
        if (isEmpty()) throw new NoSuchElementException("the deque is empty.");
        Node<Item> oldLast = tail.prev;
        Node<Item> last = oldLast.prev;
        Item item = oldLast.item;
        tail.prev = last;
        last.next = tail;
        n--;
        return item;
    }

    // return an iterator over items in order from front to back
    public Iterator<Item> iterator() {
        return new LinkedIterator(this.head);
    }

    private class LinkedIterator implements Iterator<Item> {
        private Deque.Node<Item> current;

        public LinkedIterator(Deque.Node<Item> head) {
            this.current = head.next;
        }

        public boolean hasNext() {
            return this.current != tail;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

        public Item next() {
            if (!this.hasNext()) {
                throw new NoSuchElementException();
            } else {
                Item item = this.current.item;
                this.current = this.current.next;
                return item;
            }
        }
    }

    // unit testing (required)
    public static void main(String[] args) {
        Deque<String> deque = new Deque<String>();
        while (!StdIn.isEmpty()) {
            String item = StdIn.readString();
            deque.addFirst(item);
        }
        StdOut.println("size of deque = " + deque.size());
        for (String s : deque) {
            StdOut.println(s);
        }
    }

}
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdRandom;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class RandomizedQueue<Item> implements Iterable<Item> {
    private final Node<Item> head;
    private final Node<Item> tail;
    private int n;

    private static class Node<Item> {
        private Item item;
        private RandomizedQueue.Node<Item> next;
        private RandomizedQueue.Node<Item> prev;

        private Node() {
        }
    }

    // construct an empty randomized queue
    public RandomizedQueue() {
        n = 0;
        head = new RandomizedQueue.Node<>();
        tail = new RandomizedQueue.Node<>();

        head.item = null;
        tail.item = null;
        head.prev = null;
        tail.prev = head;
        head.next = tail;
        tail.next = null;
    }

    // is the randomized queue empty?
    public boolean isEmpty() {
        return n == 0;
    }

    // return the number of items on the randomized queue
    public int size() {
        return n;
    }

    // add the item
    public void enqueue(Item item) {
        if (item == null) throw new IllegalArgumentException("item is null");
        Node<Item> oldLast = tail.prev;
        Node<Item> last = new Node<>();
        last.item = item;
        last.next = tail;
        last.prev = oldLast;
        tail.prev = last;
        oldLast.next = last;
        n++;
    }

    // remove and return a random item
    public Item dequeue() {
        if (isEmpty()) throw new NoSuchElementException("the deque is empty.");
        Node<Item> temp = head.next;
        int r = StdRandom.uniform(0, n);
        for (int i = 0; i < r; i++) {
            temp = temp.next;
        }
        Item randomItem = temp.item;
        Node<Item> preTemp = temp.prev;
        Node<Item> nextTemp = temp.next;
        preTemp.next = nextTemp;
        nextTemp.prev = preTemp;
        n--;
        return randomItem;
    }

    // return a random item (but do not remove it)
    public Item sample() {
        if (isEmpty()) throw new NoSuchElementException("the deque is empty.");
        Node<Item> temp = head.next;
        for (int i = 0; i < StdRandom.uniform(0, n); i++) {
            temp = temp.next;
        }
        return temp.item;
    }

    // return an independent iterator over items in random order
    public Iterator<Item> iterator() {
        return new LinkedIterator(this.head);
    }


    private class LinkedIterator implements Iterator<Item> {
        private RandomizedQueue.Node<Item> current;

        public LinkedIterator(RandomizedQueue.Node<Item> head) {

            this.current = head.next;
            RandomizedQueue.Node<Item> loopNode = current;
            for (int i = 0; i < n; i++) {
                int r = i + StdRandom.uniform(n - i);
                swapItem(i, r, loopNode);
                loopNode = current.next;
            }

        }

        public boolean hasNext() {
            return this.current != tail;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

        private void swapItem(int i, int random, RandomizedQueue.Node<Item> loopNode) {
            RandomizedQueue.Node<Item> chosen = loopNode;
            Item temp;
            RandomizedQueue.Node<Item> chooser = loopNode;
            for (int j = i; j <= n; j++) {
                if (j == random) {
                    chosen = loopNode;
                    break;
                }
                loopNode = loopNode.next;
            }
            temp = chosen.item;
            chosen.item = chooser.item;
            chooser.item = temp;
        }

        public Item next() {
            if (!this.hasNext()) {
                throw new NoSuchElementException();
            } else {
                Item item = this.current.item;
                this.current = this.current.next;
                return item;
            }
        }
    }

    // unit testing (required)
    public static void main(String[] args) {
        int k = Integer.parseInt(args[0]);
        RandomizedQueue<String> test = new RandomizedQueue<>();
        for (int i = 0; i < k; i++) {
            test.enqueue(StdIn.readString());
        }
        int n = k + 1;
        while (!StdIn.isEmpty()) {
            String item = StdIn.readString();
            if (StdRandom.uniform(n) < k) {
                test.dequeue();
                test.enqueue(item);
            }
        }
        for (String s : test
        ) {
            System.out.print(s + " ");
        }
        System.out.println();
        for (String s : test
        ) {
            System.out.print(s + " ");
        }
    }
}

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdRandom;

public class Permutation {
    public static void main(String[] args) {
        int k = Integer.parseInt(args[0]);
        RandomizedQueue<String> test = new RandomizedQueue<>();
        for (int i = 0; i < k; i++) {
            test.enqueue(StdIn.readString());
        }
        int n = k+1;
        while (!StdIn.isEmpty()) {
            String item = StdIn.readString();
            if (StdRandom.uniform(n) < k) {
                test.dequeue();
                test.enqueue(item);
            }
        }
        for (String s : test
        ) {
            System.out.println(s);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Coursera-ml-andrewng-notes-master.zip是一个包含Andrew Ng的机器学习课程笔记和代码的压缩包。这门课程是由斯坦福大学提供的计算机科学和人工智能实验室(CSAIL)的教授Andrew Ng教授开设的,旨在通过深入浅出的方式介绍机器学习的基础概念,包括监督学习、无监督学习、逻辑回归、神经网络等等。 这个压缩包中的笔记和代码可以帮助机器学习初学者更好地理解和应用所学的知识。笔记中包含了课程中涉及到的各种公式、算法和概念的详细解释,同时也包括了编程作业的指导和解答。而代码部分包含了课程中使用的MATLAB代码,以及Python代码的实现。 这个压缩包对机器学习爱好者和学生来说是一个非常有用的资源,能够让他们深入了解机器学习的基础,并掌握如何运用这些知识去解决实际问题。此外,这个压缩包还可以作为教师和讲师的教学资源,帮助他们更好地传授机器学习的知识和技能。 ### 回答2: coursera-ml-andrewng-notes-master.zip 是一个 Coursera Machine Learning 课程的笔记和教材的压缩包,由学生或者讲师编写。这个压缩包中包括了 Andrew Ng 教授在 Coursera 上发布的 Machine Learning 课程的全部讲义、练习题和答案等相关学习材料。 Machine Learning 课程是一个介绍机器学习的课程,它包括了许多重要的机器学习算法和理论,例如线性回归、神经网络、决策树、支持向量机等。这个课程的目标是让学生了解机器学习的方法,学习如何使用机器学习来解决实际问题,并最终构建自己的机器学习系统。 这个压缩包中包含的所有学习材料都是免费的,每个人都可以从 Coursera 的网站上免费获取。通过学习这个课程,你将学习到机器学习的基础知识和核心算法,掌握机器学习的实际应用技巧,以及学会如何处理不同种类的数据和问题。 总之,coursera-ml-andrewng-notes-master.zip 是一个非常有用的学习资源,它可以帮助人们更好地学习、理解和掌握机器学习的知识和技能。无论你是机器学习初学者还是资深的机器学习专家,它都将是一个重要的参考工具。 ### 回答3: coursera-ml-andrewng-notes-master.zip是一份具有高价值的文件,其中包含了Andrew Ng在Coursera上开授的机器学习课程的笔记。这份课程笔记可以帮助学习者更好地理解掌握机器学习技术和方法,提高在机器学习领域的实践能力。通过这份文件,学习者可以学习到机器学习的算法、原理和应用,其中包括线性回归、逻辑回归、神经网络、支持向量机、聚类、降维等多个内容。同时,这份笔记还提供了很多代码实现和模板,学习者可以通过这些实例来理解、运用和进一步深入研究机器学习技术。 总的来说,coursera-ml-andrewng-notes-master.zip对于想要深入学习和掌握机器学习技术和方法的学习者来说是一份不可多得的资料,对于企业中从事机器学习相关工作的从业人员来说也是进行技能提升或者知识更新的重要资料。因此,对于机器学习领域的学习者和从业人员来说,学习并掌握coursera-ml-andrewng-notes-master.zip所提供的知识和技能是非常有价值的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值